blob: 20bb7f066e866862965e8cbbed2a1ff2750b3c7f [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
183const char* pcm_get_error(struct pcm *pcm)
184{
185 return pcm->error;
186}
187
David Wagner4cddf192014-04-02 15:12:54 +0200188unsigned int pcm_get_subdevice(struct pcm *pcm)
189{
190 return pcm->subdevice;
191}
192
Simon Wilson79d39652011-05-25 13:44:23 -0700193static int oops(struct pcm *pcm, int e, const char *fmt, ...)
194{
195 va_list ap;
196 int sz;
197
198 va_start(ap, fmt);
199 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
200 va_end(ap);
201 sz = strlen(pcm->error);
202
203 if (errno)
204 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
205 ": %s", strerror(e));
206 return -1;
207}
208
Simon Wilsonbc03b622011-06-15 17:19:01 -0700209static unsigned int pcm_format_to_alsa(enum pcm_format format)
210{
211 switch (format) {
212 case PCM_FORMAT_S32_LE:
213 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500214 case PCM_FORMAT_S8:
215 return SNDRV_PCM_FORMAT_S8;
216 case PCM_FORMAT_S24_LE:
217 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700218 default:
219 case PCM_FORMAT_S16_LE:
220 return SNDRV_PCM_FORMAT_S16_LE;
221 };
222}
223
Simon Wilson7136cf72013-07-17 10:30:35 -0700224unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700225{
226 switch (format) {
227 case PCM_FORMAT_S32_LE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700228 case PCM_FORMAT_S24_LE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700229 return 32;
230 default:
231 case PCM_FORMAT_S16_LE:
232 return 16;
233 };
234}
235
Liam Girdwood6be28f12011-10-13 12:59:51 -0700236unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
237{
238 return bytes / (pcm->config.channels *
239 (pcm_format_to_bits(pcm->config.format) >> 3));
240}
241
242unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
243{
244 return frames * pcm->config.channels *
245 (pcm_format_to_bits(pcm->config.format) >> 3);
246}
247
Eric Laurent40b018e2011-06-18 10:10:23 -0700248static int pcm_sync_ptr(struct pcm *pcm, int flags) {
249 if (pcm->sync_ptr) {
250 pcm->sync_ptr->flags = flags;
251 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
252 return -1;
253 }
254 return 0;
255}
256
257static int pcm_hw_mmap_status(struct pcm *pcm) {
258
259 if (pcm->sync_ptr)
260 return 0;
261
262 int page_size = sysconf(_SC_PAGE_SIZE);
263 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
264 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
265 if (pcm->mmap_status == MAP_FAILED)
266 pcm->mmap_status = NULL;
267 if (!pcm->mmap_status)
268 goto mmap_error;
269
270 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
271 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
272 if (pcm->mmap_control == MAP_FAILED)
273 pcm->mmap_control = NULL;
274 if (!pcm->mmap_control) {
275 munmap(pcm->mmap_status, page_size);
276 pcm->mmap_status = NULL;
277 goto mmap_error;
278 }
279 pcm->mmap_control->avail_min = 1;
280
281 return 0;
282
283mmap_error:
284
285 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
286 if (!pcm->sync_ptr)
287 return -ENOMEM;
288 pcm->mmap_status = &pcm->sync_ptr->s.status;
289 pcm->mmap_control = &pcm->sync_ptr->c.control;
290 pcm->mmap_control->avail_min = 1;
291 pcm_sync_ptr(pcm, 0);
292
293 return 0;
294}
295
296static void pcm_hw_munmap_status(struct pcm *pcm) {
297 if (pcm->sync_ptr) {
298 free(pcm->sync_ptr);
299 pcm->sync_ptr = NULL;
300 } else {
301 int page_size = sysconf(_SC_PAGE_SIZE);
302 if (pcm->mmap_status)
303 munmap(pcm->mmap_status, page_size);
304 if (pcm->mmap_control)
305 munmap(pcm->mmap_control, page_size);
306 }
307 pcm->mmap_status = NULL;
308 pcm->mmap_control = NULL;
309}
310
Liam Girdwood6be28f12011-10-13 12:59:51 -0700311static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700312 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700313 unsigned int frames)
314{
315 int size_bytes = pcm_frames_to_bytes(pcm, frames);
316 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
317 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
318
319 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700320 if (pcm->flags & PCM_IN)
321 memcpy(buf + src_offset_bytes,
322 (char*)pcm->mmap_buffer + pcm_offset_bytes,
323 size_bytes);
324 else
325 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
326 buf + src_offset_bytes,
327 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700328 return 0;
329}
330
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700331static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700332 unsigned int offset, unsigned int size)
333{
334 void *pcm_areas;
335 int commit;
336 unsigned int pcm_offset, frames, count = 0;
337
338 while (size > 0) {
339 frames = size;
340 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700341 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700342 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
343 if (commit < 0) {
344 oops(pcm, commit, "failed to commit %d frames\n", frames);
345 return commit;
346 }
347
348 offset += commit;
349 count += commit;
350 size -= commit;
351 }
352 return count;
353}
354
Eric Laurent40b018e2011-06-18 10:10:23 -0700355int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
356 struct timespec *tstamp)
357{
358 int frames;
359 int rc;
360 snd_pcm_uframes_t hw_ptr;
361
362 if (!pcm_is_ready(pcm))
363 return -1;
364
365 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
366 if (rc < 0)
367 return -1;
368
Eric Laurent7db48582011-11-17 11:47:59 -0800369 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
370 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800371 return -1;
372
Eric Laurent40b018e2011-06-18 10:10:23 -0700373 *tstamp = pcm->mmap_status->tstamp;
374 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
375 return -1;
376
377 hw_ptr = pcm->mmap_status->hw_ptr;
378 if (pcm->flags & PCM_IN)
379 frames = hw_ptr - pcm->mmap_control->appl_ptr;
380 else
381 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
382
383 if (frames < 0)
384 return -1;
385
386 *avail = (unsigned int)frames;
387
388 return 0;
389}
390
Mark Brown6bbe77a2012-02-10 22:07:09 +0000391int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700392{
393 struct snd_xferi x;
394
395 if (pcm->flags & PCM_IN)
396 return -EINVAL;
397
Mark Brown6bbe77a2012-02-10 22:07:09 +0000398 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700399 x.frames = count / (pcm->config.channels *
400 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700401
402 for (;;) {
403 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530404 int prepare_error = pcm_prepare(pcm);
405 if (prepare_error)
406 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700407 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
408 return oops(pcm, errno, "cannot write initial data");
409 pcm->running = 1;
410 return 0;
411 }
412 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530413 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700414 pcm->running = 0;
415 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700416 /* we failed to make our window -- try to restart if we are
417 * allowed to do so. Otherwise, simply allow the EPIPE error to
418 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700419 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700420 if (pcm->flags & PCM_NORESTART)
421 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700422 continue;
423 }
424 return oops(pcm, errno, "cannot write stream data");
425 }
426 return 0;
427 }
428}
429
430int pcm_read(struct pcm *pcm, void *data, unsigned int count)
431{
432 struct snd_xferi x;
433
434 if (!(pcm->flags & PCM_IN))
435 return -EINVAL;
436
437 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700438 x.frames = count / (pcm->config.channels *
439 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700440
441 for (;;) {
442 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700443 if (pcm_start(pcm) < 0) {
444 fprintf(stderr, "start error");
445 return -errno;
446 }
Simon Wilson79d39652011-05-25 13:44:23 -0700447 }
448 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530449 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700450 pcm->running = 0;
451 if (errno == EPIPE) {
452 /* we failed to make our window -- try to restart */
453 pcm->underruns++;
454 continue;
455 }
456 return oops(pcm, errno, "cannot read stream data");
457 }
458 return 0;
459 }
460}
461
462static struct pcm bad_pcm = {
463 .fd = -1,
464};
465
Simon Wilson43544882012-10-31 12:52:39 -0700466struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
467 unsigned int flags)
468{
469 struct snd_pcm_hw_params *params;
470 char fn[256];
471 int fd;
472
473 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
474 flags & PCM_IN ? 'c' : 'p');
475
476 fd = open(fn, O_RDWR);
477 if (fd < 0) {
478 fprintf(stderr, "cannot open device '%s'\n", fn);
479 goto err_open;
480 }
481
482 params = calloc(1, sizeof(struct snd_pcm_hw_params));
483 if (!params)
484 goto err_calloc;
485
486 param_init(params);
487 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
488 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
489 goto err_hw_refine;
490 }
491
492 close(fd);
493
494 return (struct pcm_params *)params;
495
496err_hw_refine:
497 free(params);
498err_calloc:
499 close(fd);
500err_open:
501 return NULL;
502}
503
504void pcm_params_free(struct pcm_params *pcm_params)
505{
506 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
507
508 if (params)
509 free(params);
510}
511
512static int pcm_param_to_alsa(enum pcm_param param)
513{
514 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700515 case PCM_PARAM_ACCESS:
516 return SNDRV_PCM_HW_PARAM_ACCESS;
517 case PCM_PARAM_FORMAT:
518 return SNDRV_PCM_HW_PARAM_FORMAT;
519 case PCM_PARAM_SUBFORMAT:
520 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700521 case PCM_PARAM_SAMPLE_BITS:
522 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
523 break;
524 case PCM_PARAM_FRAME_BITS:
525 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
526 break;
527 case PCM_PARAM_CHANNELS:
528 return SNDRV_PCM_HW_PARAM_CHANNELS;
529 break;
530 case PCM_PARAM_RATE:
531 return SNDRV_PCM_HW_PARAM_RATE;
532 break;
533 case PCM_PARAM_PERIOD_TIME:
534 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
535 break;
536 case PCM_PARAM_PERIOD_SIZE:
537 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
538 break;
539 case PCM_PARAM_PERIOD_BYTES:
540 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
541 break;
542 case PCM_PARAM_PERIODS:
543 return SNDRV_PCM_HW_PARAM_PERIODS;
544 break;
545 case PCM_PARAM_BUFFER_TIME:
546 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
547 break;
548 case PCM_PARAM_BUFFER_SIZE:
549 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
550 break;
551 case PCM_PARAM_BUFFER_BYTES:
552 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
553 break;
554 case PCM_PARAM_TICK_TIME:
555 return SNDRV_PCM_HW_PARAM_TICK_TIME;
556 break;
557
558 default:
559 return -1;
560 }
561}
562
Andy Hungad807622014-03-10 18:08:15 -0700563struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
564 enum pcm_param param)
565{
566 int p;
567 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
568 if (params == NULL) {
569 return NULL;
570 }
571
572 p = pcm_param_to_alsa(param);
573 if (p < 0 || !param_is_mask(p)) {
574 return NULL;
575 }
576
577 return (struct pcm_mask *)param_to_mask(params, p);
578}
579
Simon Wilson43544882012-10-31 12:52:39 -0700580unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
581 enum pcm_param param)
582{
583 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
584 int p;
585
586 if (!params)
587 return 0;
588
589 p = pcm_param_to_alsa(param);
590 if (p < 0)
591 return 0;
592
593 return param_get_min(params, p);
594}
595
596unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
597 enum pcm_param param)
598{
599 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
600 int p;
601
602 if (!params)
603 return 0;
604
605 p = pcm_param_to_alsa(param);
606 if (p < 0)
607 return 0;
608
609 return param_get_max(params, p);
610}
611
Simon Wilson79d39652011-05-25 13:44:23 -0700612int pcm_close(struct pcm *pcm)
613{
614 if (pcm == &bad_pcm)
615 return 0;
616
Eric Laurent40b018e2011-06-18 10:10:23 -0700617 pcm_hw_munmap_status(pcm);
618
Liam Girdwood6be28f12011-10-13 12:59:51 -0700619 if (pcm->flags & PCM_MMAP) {
620 pcm_stop(pcm);
621 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
622 }
623
Simon Wilson79d39652011-05-25 13:44:23 -0700624 if (pcm->fd >= 0)
625 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530626 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700627 pcm->running = 0;
628 pcm->buffer_size = 0;
629 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700630 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700631 return 0;
632}
633
Simon Wilson1bd580f2011-06-02 15:58:41 -0700634struct pcm *pcm_open(unsigned int card, unsigned int device,
635 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700636{
Simon Wilson79d39652011-05-25 13:44:23 -0700637 struct pcm *pcm;
638 struct snd_pcm_info info;
639 struct snd_pcm_hw_params params;
640 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700641 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700642 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700643
644 pcm = calloc(1, sizeof(struct pcm));
645 if (!pcm || !config)
646 return &bad_pcm; /* TODO: could support default config here */
647
648 pcm->config = *config;
649
Simon Wilson1bd580f2011-06-02 15:58:41 -0700650 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
651 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700652
653 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700654 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700655 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700656 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700657 return pcm;
658 }
659
660 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700661 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700662 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700663 }
David Wagner4cddf192014-04-02 15:12:54 +0200664 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700665
666 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -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
Liam Girdwood6be28f12011-10-13 12:59:51 -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 Wilson79d39652011-05-25 13:44:23 -0700699 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
700 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700701 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700702 }
703
Liam Girdwood6be28f12011-10-13 12:59:51 -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 Wilson79d39652011-05-25 13:44:23 -0700720 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700721 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700722 sparams.period_step = 1;
723 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700724
Eric Laurent93e7b672012-08-22 16:18:14 -0700725 if (!config->start_threshold) {
726 if (pcm->flags & PCM_IN)
727 pcm->config.start_threshold = sparams.start_threshold = 1;
728 else
729 pcm->config.start_threshold = sparams.start_threshold =
730 config->period_count * config->period_size / 2;
731 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700732 sparams.start_threshold = config->start_threshold;
733
Liam Girdwood6be28f12011-10-13 12:59:51 -0700734 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800735 if (!config->stop_threshold) {
736 if (pcm->flags & PCM_IN)
737 pcm->config.stop_threshold = sparams.stop_threshold =
738 config->period_count * config->period_size * 10;
739 else
740 pcm->config.stop_threshold = sparams.stop_threshold =
741 config->period_count * config->period_size;
742 }
John Grossman3bb114a2011-07-21 10:59:55 -0700743 else
744 sparams.stop_threshold = config->stop_threshold;
745
Simon Wilson79d39652011-05-25 13:44:23 -0700746 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
747 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700748 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700749 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700750
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600751 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700752 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700753
754 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
755 oops(pcm, errno, "cannot set sw params");
756 goto fail;
757 }
758
Eric Laurent40b018e2011-06-18 10:10:23 -0700759 rc = pcm_hw_mmap_status(pcm);
760 if (rc < 0) {
761 oops(pcm, rc, "mmap status failed");
762 goto fail;
763 }
764
Glenn Kasten81012402013-08-22 15:11:48 -0700765#ifdef SNDRV_PCM_IOCTL_TTSTAMP
766 if (pcm->flags & PCM_MONOTONIC) {
767 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
768 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
769 if (rc < 0) {
770 oops(pcm, rc, "cannot set timestamp type");
771 goto fail;
772 }
773 }
774#endif
775
Simon Wilson79d39652011-05-25 13:44:23 -0700776 pcm->underruns = 0;
777 return pcm;
778
779fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700780 if (flags & PCM_MMAP)
781 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
782fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700783 close(pcm->fd);
784 pcm->fd = -1;
785 return pcm;
786}
787
788int pcm_is_ready(struct pcm *pcm)
789{
790 return pcm->fd >= 0;
791}
Simon Wilsond6458e62011-06-21 14:58:11 -0700792
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530793int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700794{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530795 if (pcm->prepared)
796 return 0;
797
Simon Wilsond6458e62011-06-21 14:58:11 -0700798 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
799 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700800
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530801 pcm->prepared = 1;
802 return 0;
803}
804
805int pcm_start(struct pcm *pcm)
806{
807 int prepare_error = pcm_prepare(pcm);
808 if (prepare_error)
809 return prepare_error;
810
Liam Girdwood6be28f12011-10-13 12:59:51 -0700811 if (pcm->flags & PCM_MMAP)
812 pcm_sync_ptr(pcm, 0);
813
Simon Wilsond6458e62011-06-21 14:58:11 -0700814 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
815 return oops(pcm, errno, "cannot start channel");
816
Liam Girdwood6be28f12011-10-13 12:59:51 -0700817 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700818 return 0;
819}
820
821int pcm_stop(struct pcm *pcm)
822{
823 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
824 return oops(pcm, errno, "cannot stop channel");
825
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530826 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700827 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700828 return 0;
829}
830
Liam Girdwood6be28f12011-10-13 12:59:51 -0700831static inline int pcm_mmap_playback_avail(struct pcm *pcm)
832{
833 int avail;
834
835 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
836
837 if (avail < 0)
838 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +0800839 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700840 avail -= pcm->boundary;
841
842 return avail;
843}
844
845static inline int pcm_mmap_capture_avail(struct pcm *pcm)
846{
847 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
848 if (avail < 0)
849 avail += pcm->boundary;
850 return avail;
851}
852
853static inline int pcm_mmap_avail(struct pcm *pcm)
854{
855 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
856 if (pcm->flags & PCM_IN)
857 return pcm_mmap_capture_avail(pcm);
858 else
859 return pcm_mmap_playback_avail(pcm);
860}
861
862static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
863{
864 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
865 appl_ptr += frames;
866
867 /* check for boundary wrap */
868 if (appl_ptr > pcm->boundary)
869 appl_ptr -= pcm->boundary;
870 pcm->mmap_control->appl_ptr = appl_ptr;
871}
872
873int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
874 unsigned int *frames)
875{
876 unsigned int continuous, copy_frames, avail;
877
878 /* return the mmap buffer */
879 *areas = pcm->mmap_buffer;
880
881 /* and the application offset in frames */
882 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
883
884 avail = pcm_mmap_avail(pcm);
885 if (avail > pcm->buffer_size)
886 avail = pcm->buffer_size;
887 continuous = pcm->buffer_size - *offset;
888
889 /* we can only copy frames if the are availabale and continuos */
890 copy_frames = *frames;
891 if (copy_frames > avail)
892 copy_frames = avail;
893 if (copy_frames > continuous)
894 copy_frames = continuous;
895 *frames = copy_frames;
896
897 return 0;
898}
899
900int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
901{
902 /* update the application pointer in userspace and kernel */
903 pcm_mmap_appl_forward(pcm, frames);
904 pcm_sync_ptr(pcm, 0);
905
906 return frames;
907}
908
909int pcm_avail_update(struct pcm *pcm)
910{
911 pcm_sync_ptr(pcm, 0);
912 return pcm_mmap_avail(pcm);
913}
914
915int pcm_state(struct pcm *pcm)
916{
917 int err = pcm_sync_ptr(pcm, 0);
918 if (err < 0)
919 return err;
920
921 return pcm->mmap_status->state;
922}
923
924int pcm_wait(struct pcm *pcm, int timeout)
925{
926 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700927 int err;
928
929 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +0100930 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700931
932 do {
933 /* let's wait for avail or timeout */
934 err = poll(&pfd, 1, timeout);
935 if (err < 0)
936 return -errno;
937
938 /* timeout ? */
939 if (err == 0)
940 return 0;
941
942 /* have we been interrupted ? */
943 if (errno == -EINTR)
944 continue;
945
946 /* check for any errors */
947 if (pfd.revents & (POLLERR | POLLNVAL)) {
948 switch (pcm_state(pcm)) {
949 case PCM_STATE_XRUN:
950 return -EPIPE;
951 case PCM_STATE_SUSPENDED:
952 return -ESTRPIPE;
953 case PCM_STATE_DISCONNECTED:
954 return -ENODEV;
955 default:
956 return -EIO;
957 }
958 }
959 /* poll again if fd not ready for IO */
960 } while (!(pfd.revents & (POLLIN | POLLOUT)));
961
962 return 1;
963}
964
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700965int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700966{
967 int err = 0, frames, avail;
968 unsigned int offset = 0, count;
969
970 if (bytes == 0)
971 return 0;
972
973 count = pcm_bytes_to_frames(pcm, bytes);
974
975 while (count > 0) {
976
977 /* get the available space for writing new frames */
978 avail = pcm_avail_update(pcm);
979 if (avail < 0) {
980 fprintf(stderr, "cannot determine available mmap frames");
981 return err;
982 }
983
984 /* start the audio if we reach the threshold */
985 if (!pcm->running &&
986 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
987 if (pcm_start(pcm) < 0) {
988 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
989 (unsigned int)pcm->mmap_status->hw_ptr,
990 (unsigned int)pcm->mmap_control->appl_ptr,
991 avail);
992 return -errno;
993 }
994 }
995
996 /* sleep until we have space to write new frames */
997 if (pcm->running &&
998 (unsigned int)avail < pcm->mmap_control->avail_min) {
999 int time = -1;
1000
1001 if (pcm->flags & PCM_NOIRQ)
1002 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1003 / pcm->noirq_frames_per_msec;
1004
1005 err = pcm_wait(pcm, time);
1006 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301007 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001008 pcm->running = 0;
1009 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1010 (unsigned int)pcm->mmap_status->hw_ptr,
1011 (unsigned int)pcm->mmap_control->appl_ptr,
1012 avail);
1013 pcm->mmap_control->appl_ptr = 0;
1014 return err;
1015 }
1016 continue;
1017 }
1018
1019 frames = count;
1020 if (frames > avail)
1021 frames = avail;
1022
1023 if (!frames)
1024 break;
1025
1026 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001027 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001028 if (frames < 0) {
1029 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1030 (unsigned int)pcm->mmap_status->hw_ptr,
1031 (unsigned int)pcm->mmap_control->appl_ptr,
1032 avail);
1033 return frames;
1034 }
1035
1036 offset += frames;
1037 count -= frames;
1038 }
1039
Liam Girdwood6be28f12011-10-13 12:59:51 -07001040 return 0;
1041}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001042
1043int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1044{
1045 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1046 return -ENOSYS;
1047
1048 return pcm_mmap_transfer(pcm, (void *)data, count);
1049}
1050
1051int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1052{
1053 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1054 return -ENOSYS;
1055
1056 return pcm_mmap_transfer(pcm, data, count);
1057}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301058
1059long pcm_get_delay(struct pcm *pcm)
1060{
1061 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1062 return -1;
1063
1064 return pcm->pcm_delay;
1065}