blob: c1f2212f4279542a7435a4c210c063d524aa4687 [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -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>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
45#define __force
46#define __bitwise
47#define __user
48#include <sound/asound.h>
49
50#include <tinyalsa/asoundlib.h>
51
52#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070053#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070054
55static inline int param_is_mask(int p)
56{
57 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
58 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
59}
60
61static inline int param_is_interval(int p)
62{
63 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
64 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
65}
66
67static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
68{
69 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
70}
71
72static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
73{
74 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
75}
76
77static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
78{
79 if (bit >= SNDRV_MASK_MAX)
80 return;
81 if (param_is_mask(n)) {
82 struct snd_mask *m = param_to_mask(p, n);
83 m->bits[0] = 0;
84 m->bits[1] = 0;
85 m->bits[bit >> 5] |= (1 << (bit & 31));
86 }
87}
88
89static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
90{
91 if (param_is_interval(n)) {
92 struct snd_interval *i = param_to_interval(p, n);
93 i->min = val;
94 }
95}
96
Simon Wilson43544882012-10-31 12:52:39 -070097static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
98{
99 if (param_is_interval(n)) {
100 struct snd_interval *i = param_to_interval(p, n);
101 return i->min;
102 }
103 return 0;
104}
105
106static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
107{
108 if (param_is_interval(n)) {
109 struct snd_interval *i = param_to_interval(p, n);
110 return i->max;
111 }
112 return 0;
113}
114
Simon Wilson79d39652011-05-25 13:44:23 -0700115static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
116{
117 if (param_is_interval(n)) {
118 struct snd_interval *i = param_to_interval(p, n);
119 i->min = val;
120 i->max = val;
121 i->integer = 1;
122 }
123}
124
Liam Girdwood6be28f12011-10-13 12:59:51 -0700125static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
126{
127 if (param_is_interval(n)) {
128 struct snd_interval *i = param_to_interval(p, n);
129 if (i->integer)
130 return i->max;
131 }
132 return 0;
133}
134
Simon Wilson79d39652011-05-25 13:44:23 -0700135static void param_init(struct snd_pcm_hw_params *p)
136{
137 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700138
Simon Wilson79d39652011-05-25 13:44:23 -0700139 memset(p, 0, sizeof(*p));
140 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
141 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
142 struct snd_mask *m = param_to_mask(p, n);
143 m->bits[0] = ~0;
144 m->bits[1] = ~0;
145 }
146 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
147 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
148 struct snd_interval *i = param_to_interval(p, n);
149 i->min = 0;
150 i->max = ~0;
151 }
Simon Wilson43544882012-10-31 12:52:39 -0700152 p->rmask = ~0U;
153 p->cmask = 0;
154 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700155}
156
157#define PCM_ERROR_MAX 128
158
159struct pcm {
160 int fd;
161 unsigned int flags;
162 int running:1;
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530163 int prepared:1;
Simon Wilson79d39652011-05-25 13:44:23 -0700164 int underruns;
165 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700166 unsigned int boundary;
Simon Wilson79d39652011-05-25 13:44:23 -0700167 char error[PCM_ERROR_MAX];
168 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700169 struct snd_pcm_mmap_status *mmap_status;
170 struct snd_pcm_mmap_control *mmap_control;
171 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700172 void *mmap_buffer;
173 unsigned int noirq_frames_per_msec;
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530174 long pcm_delay;
David Wagner4cddf192014-04-02 15:12:54 +0200175 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700176};
177
Simon Wilson851aa5c2011-05-30 21:18:26 -0700178unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700179{
180 return pcm->buffer_size;
181}
182
Taylor Holbertonbb402602016-08-03 10:15:46 -0400183int pcm_get_file_descriptor(struct pcm *pcm)
184{
185 return pcm->fd;
186}
187
Simon Wilson79d39652011-05-25 13:44:23 -0700188const char* pcm_get_error(struct pcm *pcm)
189{
190 return pcm->error;
191}
192
David Wagner4cddf192014-04-02 15:12:54 +0200193unsigned int pcm_get_subdevice(struct pcm *pcm)
194{
195 return pcm->subdevice;
196}
197
Simon Wilson79d39652011-05-25 13:44:23 -0700198static int oops(struct pcm *pcm, int e, const char *fmt, ...)
199{
200 va_list ap;
201 int sz;
202
203 va_start(ap, fmt);
204 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
205 va_end(ap);
206 sz = strlen(pcm->error);
207
208 if (errno)
209 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
210 ": %s", strerror(e));
211 return -1;
212}
213
Simon Wilsonbc03b622011-06-15 17:19:01 -0700214static unsigned int pcm_format_to_alsa(enum pcm_format format)
215{
216 switch (format) {
217 case PCM_FORMAT_S32_LE:
218 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500219 case PCM_FORMAT_S8:
220 return SNDRV_PCM_FORMAT_S8;
221 case PCM_FORMAT_S24_LE:
222 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700223 default:
224 case PCM_FORMAT_S16_LE:
225 return SNDRV_PCM_FORMAT_S16_LE;
226 };
227}
228
Simon Wilson7136cf72013-07-17 10:30:35 -0700229unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700230{
231 switch (format) {
232 case PCM_FORMAT_S32_LE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700233 case PCM_FORMAT_S24_LE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700234 return 32;
235 default:
236 case PCM_FORMAT_S16_LE:
237 return 16;
238 };
239}
240
Liam Girdwood6be28f12011-10-13 12:59:51 -0700241unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
242{
243 return bytes / (pcm->config.channels *
244 (pcm_format_to_bits(pcm->config.format) >> 3));
245}
246
247unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
248{
249 return frames * pcm->config.channels *
250 (pcm_format_to_bits(pcm->config.format) >> 3);
251}
252
Eric Laurent40b018e2011-06-18 10:10:23 -0700253static int pcm_sync_ptr(struct pcm *pcm, int flags) {
254 if (pcm->sync_ptr) {
255 pcm->sync_ptr->flags = flags;
256 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
257 return -1;
258 }
259 return 0;
260}
261
262static int pcm_hw_mmap_status(struct pcm *pcm) {
263
264 if (pcm->sync_ptr)
265 return 0;
266
267 int page_size = sysconf(_SC_PAGE_SIZE);
268 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
269 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
270 if (pcm->mmap_status == MAP_FAILED)
271 pcm->mmap_status = NULL;
272 if (!pcm->mmap_status)
273 goto mmap_error;
274
275 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
276 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
277 if (pcm->mmap_control == MAP_FAILED)
278 pcm->mmap_control = NULL;
279 if (!pcm->mmap_control) {
280 munmap(pcm->mmap_status, page_size);
281 pcm->mmap_status = NULL;
282 goto mmap_error;
283 }
284 pcm->mmap_control->avail_min = 1;
285
286 return 0;
287
288mmap_error:
289
290 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
291 if (!pcm->sync_ptr)
292 return -ENOMEM;
293 pcm->mmap_status = &pcm->sync_ptr->s.status;
294 pcm->mmap_control = &pcm->sync_ptr->c.control;
295 pcm->mmap_control->avail_min = 1;
296 pcm_sync_ptr(pcm, 0);
297
298 return 0;
299}
300
301static void pcm_hw_munmap_status(struct pcm *pcm) {
302 if (pcm->sync_ptr) {
303 free(pcm->sync_ptr);
304 pcm->sync_ptr = NULL;
305 } else {
306 int page_size = sysconf(_SC_PAGE_SIZE);
307 if (pcm->mmap_status)
308 munmap(pcm->mmap_status, page_size);
309 if (pcm->mmap_control)
310 munmap(pcm->mmap_control, page_size);
311 }
312 pcm->mmap_status = NULL;
313 pcm->mmap_control = NULL;
314}
315
Liam Girdwood6be28f12011-10-13 12:59:51 -0700316static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700317 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700318 unsigned int frames)
319{
320 int size_bytes = pcm_frames_to_bytes(pcm, frames);
321 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
322 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
323
324 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700325 if (pcm->flags & PCM_IN)
326 memcpy(buf + src_offset_bytes,
327 (char*)pcm->mmap_buffer + pcm_offset_bytes,
328 size_bytes);
329 else
330 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
331 buf + src_offset_bytes,
332 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700333 return 0;
334}
335
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700336static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700337 unsigned int offset, unsigned int size)
338{
339 void *pcm_areas;
340 int commit;
341 unsigned int pcm_offset, frames, count = 0;
342
343 while (size > 0) {
344 frames = size;
345 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700346 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700347 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
348 if (commit < 0) {
349 oops(pcm, commit, "failed to commit %d frames\n", frames);
350 return commit;
351 }
352
353 offset += commit;
354 count += commit;
355 size -= commit;
356 }
357 return count;
358}
359
Eric Laurent40b018e2011-06-18 10:10:23 -0700360int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
361 struct timespec *tstamp)
362{
363 int frames;
364 int rc;
365 snd_pcm_uframes_t hw_ptr;
366
367 if (!pcm_is_ready(pcm))
368 return -1;
369
370 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
371 if (rc < 0)
372 return -1;
373
Eric Laurent7db48582011-11-17 11:47:59 -0800374 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
375 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800376 return -1;
377
Eric Laurent40b018e2011-06-18 10:10:23 -0700378 *tstamp = pcm->mmap_status->tstamp;
379 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
380 return -1;
381
382 hw_ptr = pcm->mmap_status->hw_ptr;
383 if (pcm->flags & PCM_IN)
384 frames = hw_ptr - pcm->mmap_control->appl_ptr;
385 else
386 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
387
388 if (frames < 0)
389 return -1;
390
391 *avail = (unsigned int)frames;
392
393 return 0;
394}
395
Mark Brown6bbe77a2012-02-10 22:07:09 +0000396int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700397{
398 struct snd_xferi x;
399
400 if (pcm->flags & PCM_IN)
401 return -EINVAL;
402
Mark Brown6bbe77a2012-02-10 22:07:09 +0000403 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700404 x.frames = count / (pcm->config.channels *
405 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700406
407 for (;;) {
408 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530409 int prepare_error = pcm_prepare(pcm);
410 if (prepare_error)
411 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700412 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
413 return oops(pcm, errno, "cannot write initial data");
414 pcm->running = 1;
415 return 0;
416 }
417 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530418 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700419 pcm->running = 0;
420 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700421 /* we failed to make our window -- try to restart if we are
422 * allowed to do so. Otherwise, simply allow the EPIPE error to
423 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700424 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700425 if (pcm->flags & PCM_NORESTART)
426 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700427 continue;
428 }
429 return oops(pcm, errno, "cannot write stream data");
430 }
431 return 0;
432 }
433}
434
435int pcm_read(struct pcm *pcm, void *data, unsigned int count)
436{
437 struct snd_xferi x;
438
439 if (!(pcm->flags & PCM_IN))
440 return -EINVAL;
441
442 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700443 x.frames = count / (pcm->config.channels *
444 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700445
446 for (;;) {
447 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700448 if (pcm_start(pcm) < 0) {
449 fprintf(stderr, "start error");
450 return -errno;
451 }
Simon Wilson79d39652011-05-25 13:44:23 -0700452 }
453 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530454 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700455 pcm->running = 0;
456 if (errno == EPIPE) {
457 /* we failed to make our window -- try to restart */
458 pcm->underruns++;
459 continue;
460 }
461 return oops(pcm, errno, "cannot read stream data");
462 }
463 return 0;
464 }
465}
466
467static struct pcm bad_pcm = {
468 .fd = -1,
469};
470
Simon Wilson43544882012-10-31 12:52:39 -0700471struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
472 unsigned int flags)
473{
474 struct snd_pcm_hw_params *params;
475 char fn[256];
476 int fd;
477
478 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
479 flags & PCM_IN ? 'c' : 'p');
480
481 fd = open(fn, O_RDWR);
482 if (fd < 0) {
483 fprintf(stderr, "cannot open device '%s'\n", fn);
484 goto err_open;
485 }
486
487 params = calloc(1, sizeof(struct snd_pcm_hw_params));
488 if (!params)
489 goto err_calloc;
490
491 param_init(params);
492 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
493 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
494 goto err_hw_refine;
495 }
496
497 close(fd);
498
499 return (struct pcm_params *)params;
500
501err_hw_refine:
502 free(params);
503err_calloc:
504 close(fd);
505err_open:
506 return NULL;
507}
508
509void pcm_params_free(struct pcm_params *pcm_params)
510{
511 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
512
513 if (params)
514 free(params);
515}
516
517static int pcm_param_to_alsa(enum pcm_param param)
518{
519 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700520 case PCM_PARAM_ACCESS:
521 return SNDRV_PCM_HW_PARAM_ACCESS;
522 case PCM_PARAM_FORMAT:
523 return SNDRV_PCM_HW_PARAM_FORMAT;
524 case PCM_PARAM_SUBFORMAT:
525 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700526 case PCM_PARAM_SAMPLE_BITS:
527 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
528 break;
529 case PCM_PARAM_FRAME_BITS:
530 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
531 break;
532 case PCM_PARAM_CHANNELS:
533 return SNDRV_PCM_HW_PARAM_CHANNELS;
534 break;
535 case PCM_PARAM_RATE:
536 return SNDRV_PCM_HW_PARAM_RATE;
537 break;
538 case PCM_PARAM_PERIOD_TIME:
539 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
540 break;
541 case PCM_PARAM_PERIOD_SIZE:
542 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
543 break;
544 case PCM_PARAM_PERIOD_BYTES:
545 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
546 break;
547 case PCM_PARAM_PERIODS:
548 return SNDRV_PCM_HW_PARAM_PERIODS;
549 break;
550 case PCM_PARAM_BUFFER_TIME:
551 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
552 break;
553 case PCM_PARAM_BUFFER_SIZE:
554 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
555 break;
556 case PCM_PARAM_BUFFER_BYTES:
557 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
558 break;
559 case PCM_PARAM_TICK_TIME:
560 return SNDRV_PCM_HW_PARAM_TICK_TIME;
561 break;
562
563 default:
564 return -1;
565 }
566}
567
Andy Hungad807622014-03-10 18:08:15 -0700568struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
569 enum pcm_param param)
570{
571 int p;
572 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
573 if (params == NULL) {
574 return NULL;
575 }
576
577 p = pcm_param_to_alsa(param);
578 if (p < 0 || !param_is_mask(p)) {
579 return NULL;
580 }
581
582 return (struct pcm_mask *)param_to_mask(params, p);
583}
584
Simon Wilson43544882012-10-31 12:52:39 -0700585unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
586 enum pcm_param param)
587{
588 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
589 int p;
590
591 if (!params)
592 return 0;
593
594 p = pcm_param_to_alsa(param);
595 if (p < 0)
596 return 0;
597
598 return param_get_min(params, p);
599}
600
601unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
602 enum pcm_param param)
603{
604 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
605 int p;
606
607 if (!params)
608 return 0;
609
610 p = pcm_param_to_alsa(param);
611 if (p < 0)
612 return 0;
613
614 return param_get_max(params, p);
615}
616
Simon Wilson79d39652011-05-25 13:44:23 -0700617int pcm_close(struct pcm *pcm)
618{
619 if (pcm == &bad_pcm)
620 return 0;
621
Eric Laurent40b018e2011-06-18 10:10:23 -0700622 pcm_hw_munmap_status(pcm);
623
Liam Girdwood6be28f12011-10-13 12:59:51 -0700624 if (pcm->flags & PCM_MMAP) {
625 pcm_stop(pcm);
626 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
627 }
628
Simon Wilson79d39652011-05-25 13:44:23 -0700629 if (pcm->fd >= 0)
630 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530631 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700632 pcm->running = 0;
633 pcm->buffer_size = 0;
634 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700635 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700636 return 0;
637}
638
Simon Wilson1bd580f2011-06-02 15:58:41 -0700639struct pcm *pcm_open(unsigned int card, unsigned int device,
640 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700641{
Simon Wilson79d39652011-05-25 13:44:23 -0700642 struct pcm *pcm;
643 struct snd_pcm_info info;
644 struct snd_pcm_hw_params params;
645 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700646 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700647 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700648
649 pcm = calloc(1, sizeof(struct pcm));
650 if (!pcm || !config)
651 return &bad_pcm; /* TODO: could support default config here */
652
653 pcm->config = *config;
654
Simon Wilson1bd580f2011-06-02 15:58:41 -0700655 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
656 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700657
658 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700659 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700660 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700661 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700662 return pcm;
663 }
664
665 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700666 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700667 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700668 }
David Wagner4cddf192014-04-02 15:12:54 +0200669 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700670
671 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700672 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
673 pcm_format_to_alsa(config->format));
674 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
675 SNDRV_PCM_SUBFORMAT_STD);
676 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
677 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
678 pcm_format_to_bits(config->format));
679 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
680 pcm_format_to_bits(config->format) * config->channels);
681 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
682 config->channels);
683 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
684 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
685
Liam Girdwood6be28f12011-10-13 12:59:51 -0700686 if (flags & PCM_NOIRQ) {
687
688 if (!(flags & PCM_MMAP)) {
689 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
690 goto fail;
691 }
692
693 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
694 pcm->noirq_frames_per_msec = config->rate / 1000;
695 }
696
697 if (flags & PCM_MMAP)
698 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
699 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
700 else
701 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
702 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
703
Simon Wilson79d39652011-05-25 13:44:23 -0700704 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
705 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700706 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700707 }
708
Liam Girdwood6be28f12011-10-13 12:59:51 -0700709 /* get our refined hw_params */
710 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
711 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
712 pcm->buffer_size = config->period_count * config->period_size;
713
714 if (flags & PCM_MMAP) {
715 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
716 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
717 if (pcm->mmap_buffer == MAP_FAILED) {
718 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
719 pcm_frames_to_bytes(pcm, pcm->buffer_size));
720 goto fail_close;
721 }
722 }
723
724
Simon Wilson79d39652011-05-25 13:44:23 -0700725 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700726 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700727 sparams.period_step = 1;
728 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700729
Eric Laurent93e7b672012-08-22 16:18:14 -0700730 if (!config->start_threshold) {
731 if (pcm->flags & PCM_IN)
732 pcm->config.start_threshold = sparams.start_threshold = 1;
733 else
734 pcm->config.start_threshold = sparams.start_threshold =
735 config->period_count * config->period_size / 2;
736 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700737 sparams.start_threshold = config->start_threshold;
738
Liam Girdwood6be28f12011-10-13 12:59:51 -0700739 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800740 if (!config->stop_threshold) {
741 if (pcm->flags & PCM_IN)
742 pcm->config.stop_threshold = sparams.stop_threshold =
743 config->period_count * config->period_size * 10;
744 else
745 pcm->config.stop_threshold = sparams.stop_threshold =
746 config->period_count * config->period_size;
747 }
John Grossman3bb114a2011-07-21 10:59:55 -0700748 else
749 sparams.stop_threshold = config->stop_threshold;
750
Simon Wilson79d39652011-05-25 13:44:23 -0700751 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
752 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700753 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700754 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700755
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600756 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700757 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700758
759 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
760 oops(pcm, errno, "cannot set sw params");
761 goto fail;
762 }
763
Eric Laurent40b018e2011-06-18 10:10:23 -0700764 rc = pcm_hw_mmap_status(pcm);
765 if (rc < 0) {
766 oops(pcm, rc, "mmap status failed");
767 goto fail;
768 }
769
Glenn Kasten81012402013-08-22 15:11:48 -0700770#ifdef SNDRV_PCM_IOCTL_TTSTAMP
771 if (pcm->flags & PCM_MONOTONIC) {
772 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
773 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
774 if (rc < 0) {
775 oops(pcm, rc, "cannot set timestamp type");
776 goto fail;
777 }
778 }
779#endif
780
Simon Wilson79d39652011-05-25 13:44:23 -0700781 pcm->underruns = 0;
782 return pcm;
783
784fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700785 if (flags & PCM_MMAP)
786 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
787fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700788 close(pcm->fd);
789 pcm->fd = -1;
790 return pcm;
791}
792
793int pcm_is_ready(struct pcm *pcm)
794{
795 return pcm->fd >= 0;
796}
Simon Wilsond6458e62011-06-21 14:58:11 -0700797
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530798int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700799{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530800 if (pcm->prepared)
801 return 0;
802
Simon Wilsond6458e62011-06-21 14:58:11 -0700803 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
804 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700805
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530806 pcm->prepared = 1;
807 return 0;
808}
809
810int pcm_start(struct pcm *pcm)
811{
812 int prepare_error = pcm_prepare(pcm);
813 if (prepare_error)
814 return prepare_error;
815
Liam Girdwood6be28f12011-10-13 12:59:51 -0700816 if (pcm->flags & PCM_MMAP)
817 pcm_sync_ptr(pcm, 0);
818
Simon Wilsond6458e62011-06-21 14:58:11 -0700819 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
820 return oops(pcm, errno, "cannot start channel");
821
Liam Girdwood6be28f12011-10-13 12:59:51 -0700822 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700823 return 0;
824}
825
826int pcm_stop(struct pcm *pcm)
827{
828 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
829 return oops(pcm, errno, "cannot stop channel");
830
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530831 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700832 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700833 return 0;
834}
835
Liam Girdwood6be28f12011-10-13 12:59:51 -0700836static inline int pcm_mmap_playback_avail(struct pcm *pcm)
837{
838 int avail;
839
840 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
841
842 if (avail < 0)
843 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +0800844 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700845 avail -= pcm->boundary;
846
847 return avail;
848}
849
850static inline int pcm_mmap_capture_avail(struct pcm *pcm)
851{
852 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
853 if (avail < 0)
854 avail += pcm->boundary;
855 return avail;
856}
857
858static inline int pcm_mmap_avail(struct pcm *pcm)
859{
860 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
861 if (pcm->flags & PCM_IN)
862 return pcm_mmap_capture_avail(pcm);
863 else
864 return pcm_mmap_playback_avail(pcm);
865}
866
867static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
868{
869 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
870 appl_ptr += frames;
871
872 /* check for boundary wrap */
873 if (appl_ptr > pcm->boundary)
874 appl_ptr -= pcm->boundary;
875 pcm->mmap_control->appl_ptr = appl_ptr;
876}
877
878int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
879 unsigned int *frames)
880{
881 unsigned int continuous, copy_frames, avail;
882
883 /* return the mmap buffer */
884 *areas = pcm->mmap_buffer;
885
886 /* and the application offset in frames */
887 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
888
889 avail = pcm_mmap_avail(pcm);
890 if (avail > pcm->buffer_size)
891 avail = pcm->buffer_size;
892 continuous = pcm->buffer_size - *offset;
893
894 /* we can only copy frames if the are availabale and continuos */
895 copy_frames = *frames;
896 if (copy_frames > avail)
897 copy_frames = avail;
898 if (copy_frames > continuous)
899 copy_frames = continuous;
900 *frames = copy_frames;
901
902 return 0;
903}
904
905int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
906{
907 /* update the application pointer in userspace and kernel */
908 pcm_mmap_appl_forward(pcm, frames);
909 pcm_sync_ptr(pcm, 0);
910
911 return frames;
912}
913
914int pcm_avail_update(struct pcm *pcm)
915{
916 pcm_sync_ptr(pcm, 0);
917 return pcm_mmap_avail(pcm);
918}
919
920int pcm_state(struct pcm *pcm)
921{
922 int err = pcm_sync_ptr(pcm, 0);
923 if (err < 0)
924 return err;
925
926 return pcm->mmap_status->state;
927}
928
929int pcm_wait(struct pcm *pcm, int timeout)
930{
931 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700932 int err;
933
934 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +0100935 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700936
937 do {
938 /* let's wait for avail or timeout */
939 err = poll(&pfd, 1, timeout);
940 if (err < 0)
941 return -errno;
942
943 /* timeout ? */
944 if (err == 0)
945 return 0;
946
947 /* have we been interrupted ? */
948 if (errno == -EINTR)
949 continue;
950
951 /* check for any errors */
952 if (pfd.revents & (POLLERR | POLLNVAL)) {
953 switch (pcm_state(pcm)) {
954 case PCM_STATE_XRUN:
955 return -EPIPE;
956 case PCM_STATE_SUSPENDED:
957 return -ESTRPIPE;
958 case PCM_STATE_DISCONNECTED:
959 return -ENODEV;
960 default:
961 return -EIO;
962 }
963 }
964 /* poll again if fd not ready for IO */
965 } while (!(pfd.revents & (POLLIN | POLLOUT)));
966
967 return 1;
968}
969
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700970int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700971{
972 int err = 0, frames, avail;
973 unsigned int offset = 0, count;
974
975 if (bytes == 0)
976 return 0;
977
978 count = pcm_bytes_to_frames(pcm, bytes);
979
980 while (count > 0) {
981
982 /* get the available space for writing new frames */
983 avail = pcm_avail_update(pcm);
984 if (avail < 0) {
985 fprintf(stderr, "cannot determine available mmap frames");
986 return err;
987 }
988
989 /* start the audio if we reach the threshold */
990 if (!pcm->running &&
991 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
992 if (pcm_start(pcm) < 0) {
993 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
994 (unsigned int)pcm->mmap_status->hw_ptr,
995 (unsigned int)pcm->mmap_control->appl_ptr,
996 avail);
997 return -errno;
998 }
999 }
1000
1001 /* sleep until we have space to write new frames */
1002 if (pcm->running &&
1003 (unsigned int)avail < pcm->mmap_control->avail_min) {
1004 int time = -1;
1005
1006 if (pcm->flags & PCM_NOIRQ)
1007 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1008 / pcm->noirq_frames_per_msec;
1009
1010 err = pcm_wait(pcm, time);
1011 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301012 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001013 pcm->running = 0;
1014 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1015 (unsigned int)pcm->mmap_status->hw_ptr,
1016 (unsigned int)pcm->mmap_control->appl_ptr,
1017 avail);
1018 pcm->mmap_control->appl_ptr = 0;
1019 return err;
1020 }
1021 continue;
1022 }
1023
1024 frames = count;
1025 if (frames > avail)
1026 frames = avail;
1027
1028 if (!frames)
1029 break;
1030
1031 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001032 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001033 if (frames < 0) {
1034 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1035 (unsigned int)pcm->mmap_status->hw_ptr,
1036 (unsigned int)pcm->mmap_control->appl_ptr,
1037 avail);
1038 return frames;
1039 }
1040
1041 offset += frames;
1042 count -= frames;
1043 }
1044
Liam Girdwood6be28f12011-10-13 12:59:51 -07001045 return 0;
1046}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001047
1048int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1049{
1050 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1051 return -ENOSYS;
1052
1053 return pcm_mmap_transfer(pcm, (void *)data, count);
1054}
1055
1056int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1057{
1058 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1059 return -ENOSYS;
1060
1061 return pcm_mmap_transfer(pcm, data, count);
1062}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301063
1064long pcm_get_delay(struct pcm *pcm)
1065{
1066 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1067 return -1;
1068
1069 return pcm->pcm_delay;
1070}