blob: a97947f4626665821d0933dd9726728829b86b36 [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
Taylor Holberton4f556062016-09-16 09:54:36 -0400253static int pcm_sync_ptr(struct pcm *pcm, int flags)
254{
Eric Laurent40b018e2011-06-18 10:10:23 -0700255 if (pcm->sync_ptr) {
256 pcm->sync_ptr->flags = flags;
257 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
258 return -1;
259 }
260 return 0;
261}
262
Taylor Holberton4f556062016-09-16 09:54:36 -0400263static int pcm_hw_mmap_status(struct pcm *pcm)
264{
Eric Laurent40b018e2011-06-18 10:10:23 -0700265 if (pcm->sync_ptr)
266 return 0;
267
268 int page_size = sysconf(_SC_PAGE_SIZE);
269 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
270 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
271 if (pcm->mmap_status == MAP_FAILED)
272 pcm->mmap_status = NULL;
273 if (!pcm->mmap_status)
274 goto mmap_error;
275
276 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
277 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
278 if (pcm->mmap_control == MAP_FAILED)
279 pcm->mmap_control = NULL;
280 if (!pcm->mmap_control) {
281 munmap(pcm->mmap_status, page_size);
282 pcm->mmap_status = NULL;
283 goto mmap_error;
284 }
285 pcm->mmap_control->avail_min = 1;
286
287 return 0;
288
289mmap_error:
290
291 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
292 if (!pcm->sync_ptr)
293 return -ENOMEM;
294 pcm->mmap_status = &pcm->sync_ptr->s.status;
295 pcm->mmap_control = &pcm->sync_ptr->c.control;
296 pcm->mmap_control->avail_min = 1;
297 pcm_sync_ptr(pcm, 0);
298
299 return 0;
300}
301
302static void pcm_hw_munmap_status(struct pcm *pcm) {
303 if (pcm->sync_ptr) {
304 free(pcm->sync_ptr);
305 pcm->sync_ptr = NULL;
306 } else {
307 int page_size = sysconf(_SC_PAGE_SIZE);
308 if (pcm->mmap_status)
309 munmap(pcm->mmap_status, page_size);
310 if (pcm->mmap_control)
311 munmap(pcm->mmap_control, page_size);
312 }
313 pcm->mmap_status = NULL;
314 pcm->mmap_control = NULL;
315}
316
Liam Girdwood6be28f12011-10-13 12:59:51 -0700317static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700318 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700319 unsigned int frames)
320{
321 int size_bytes = pcm_frames_to_bytes(pcm, frames);
322 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
323 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
324
325 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700326 if (pcm->flags & PCM_IN)
327 memcpy(buf + src_offset_bytes,
328 (char*)pcm->mmap_buffer + pcm_offset_bytes,
329 size_bytes);
330 else
331 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
332 buf + src_offset_bytes,
333 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700334 return 0;
335}
336
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700337static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700338 unsigned int offset, unsigned int size)
339{
340 void *pcm_areas;
341 int commit;
342 unsigned int pcm_offset, frames, count = 0;
343
344 while (size > 0) {
345 frames = size;
346 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700347 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700348 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
349 if (commit < 0) {
350 oops(pcm, commit, "failed to commit %d frames\n", frames);
351 return commit;
352 }
353
354 offset += commit;
355 count += commit;
356 size -= commit;
357 }
358 return count;
359}
360
Eric Laurent40b018e2011-06-18 10:10:23 -0700361int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
362 struct timespec *tstamp)
363{
364 int frames;
365 int rc;
366 snd_pcm_uframes_t hw_ptr;
367
368 if (!pcm_is_ready(pcm))
369 return -1;
370
371 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
372 if (rc < 0)
373 return -1;
374
Eric Laurent7db48582011-11-17 11:47:59 -0800375 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
376 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800377 return -1;
378
Eric Laurent40b018e2011-06-18 10:10:23 -0700379 *tstamp = pcm->mmap_status->tstamp;
380 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
381 return -1;
382
383 hw_ptr = pcm->mmap_status->hw_ptr;
384 if (pcm->flags & PCM_IN)
385 frames = hw_ptr - pcm->mmap_control->appl_ptr;
386 else
387 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
388
389 if (frames < 0)
390 return -1;
391
392 *avail = (unsigned int)frames;
393
394 return 0;
395}
396
Mark Brown6bbe77a2012-02-10 22:07:09 +0000397int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700398{
399 struct snd_xferi x;
400
401 if (pcm->flags & PCM_IN)
402 return -EINVAL;
403
Mark Brown6bbe77a2012-02-10 22:07:09 +0000404 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700405 x.frames = count / (pcm->config.channels *
406 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700407
408 for (;;) {
409 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530410 int prepare_error = pcm_prepare(pcm);
411 if (prepare_error)
412 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700413 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
414 return oops(pcm, errno, "cannot write initial data");
415 pcm->running = 1;
416 return 0;
417 }
418 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530419 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700420 pcm->running = 0;
421 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700422 /* we failed to make our window -- try to restart if we are
423 * allowed to do so. Otherwise, simply allow the EPIPE error to
424 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700425 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700426 if (pcm->flags & PCM_NORESTART)
427 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700428 continue;
429 }
430 return oops(pcm, errno, "cannot write stream data");
431 }
432 return 0;
433 }
434}
435
436int pcm_read(struct pcm *pcm, void *data, unsigned int count)
437{
438 struct snd_xferi x;
439
440 if (!(pcm->flags & PCM_IN))
441 return -EINVAL;
442
443 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700444 x.frames = count / (pcm->config.channels *
445 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700446
447 for (;;) {
448 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700449 if (pcm_start(pcm) < 0) {
450 fprintf(stderr, "start error");
451 return -errno;
452 }
Simon Wilson79d39652011-05-25 13:44:23 -0700453 }
454 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530455 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700456 pcm->running = 0;
457 if (errno == EPIPE) {
458 /* we failed to make our window -- try to restart */
459 pcm->underruns++;
460 continue;
461 }
462 return oops(pcm, errno, "cannot read stream data");
463 }
464 return 0;
465 }
466}
467
468static struct pcm bad_pcm = {
469 .fd = -1,
470};
471
Simon Wilson43544882012-10-31 12:52:39 -0700472struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
473 unsigned int flags)
474{
475 struct snd_pcm_hw_params *params;
476 char fn[256];
477 int fd;
478
479 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
480 flags & PCM_IN ? 'c' : 'p');
481
482 fd = open(fn, O_RDWR);
483 if (fd < 0) {
484 fprintf(stderr, "cannot open device '%s'\n", fn);
485 goto err_open;
486 }
487
488 params = calloc(1, sizeof(struct snd_pcm_hw_params));
489 if (!params)
490 goto err_calloc;
491
492 param_init(params);
493 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
494 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
495 goto err_hw_refine;
496 }
497
498 close(fd);
499
500 return (struct pcm_params *)params;
501
502err_hw_refine:
503 free(params);
504err_calloc:
505 close(fd);
506err_open:
507 return NULL;
508}
509
510void pcm_params_free(struct pcm_params *pcm_params)
511{
512 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
513
514 if (params)
515 free(params);
516}
517
518static int pcm_param_to_alsa(enum pcm_param param)
519{
520 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700521 case PCM_PARAM_ACCESS:
522 return SNDRV_PCM_HW_PARAM_ACCESS;
523 case PCM_PARAM_FORMAT:
524 return SNDRV_PCM_HW_PARAM_FORMAT;
525 case PCM_PARAM_SUBFORMAT:
526 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700527 case PCM_PARAM_SAMPLE_BITS:
528 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
529 break;
530 case PCM_PARAM_FRAME_BITS:
531 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
532 break;
533 case PCM_PARAM_CHANNELS:
534 return SNDRV_PCM_HW_PARAM_CHANNELS;
535 break;
536 case PCM_PARAM_RATE:
537 return SNDRV_PCM_HW_PARAM_RATE;
538 break;
539 case PCM_PARAM_PERIOD_TIME:
540 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
541 break;
542 case PCM_PARAM_PERIOD_SIZE:
543 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
544 break;
545 case PCM_PARAM_PERIOD_BYTES:
546 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
547 break;
548 case PCM_PARAM_PERIODS:
549 return SNDRV_PCM_HW_PARAM_PERIODS;
550 break;
551 case PCM_PARAM_BUFFER_TIME:
552 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
553 break;
554 case PCM_PARAM_BUFFER_SIZE:
555 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
556 break;
557 case PCM_PARAM_BUFFER_BYTES:
558 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
559 break;
560 case PCM_PARAM_TICK_TIME:
561 return SNDRV_PCM_HW_PARAM_TICK_TIME;
562 break;
563
564 default:
565 return -1;
566 }
567}
568
Andy Hungad807622014-03-10 18:08:15 -0700569struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
570 enum pcm_param param)
571{
572 int p;
573 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
574 if (params == NULL) {
575 return NULL;
576 }
577
578 p = pcm_param_to_alsa(param);
579 if (p < 0 || !param_is_mask(p)) {
580 return NULL;
581 }
582
583 return (struct pcm_mask *)param_to_mask(params, p);
584}
585
Simon Wilson43544882012-10-31 12:52:39 -0700586unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
587 enum pcm_param param)
588{
589 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
590 int p;
591
592 if (!params)
593 return 0;
594
595 p = pcm_param_to_alsa(param);
596 if (p < 0)
597 return 0;
598
599 return param_get_min(params, p);
600}
601
602unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
603 enum pcm_param param)
604{
605 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
606 int p;
607
608 if (!params)
609 return 0;
610
611 p = pcm_param_to_alsa(param);
612 if (p < 0)
613 return 0;
614
615 return param_get_max(params, p);
616}
617
Simon Wilson79d39652011-05-25 13:44:23 -0700618int pcm_close(struct pcm *pcm)
619{
620 if (pcm == &bad_pcm)
621 return 0;
622
Eric Laurent40b018e2011-06-18 10:10:23 -0700623 pcm_hw_munmap_status(pcm);
624
Liam Girdwood6be28f12011-10-13 12:59:51 -0700625 if (pcm->flags & PCM_MMAP) {
626 pcm_stop(pcm);
627 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
628 }
629
Simon Wilson79d39652011-05-25 13:44:23 -0700630 if (pcm->fd >= 0)
631 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530632 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700633 pcm->running = 0;
634 pcm->buffer_size = 0;
635 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700636 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700637 return 0;
638}
639
Simon Wilson1bd580f2011-06-02 15:58:41 -0700640struct pcm *pcm_open(unsigned int card, unsigned int device,
641 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700642{
Simon Wilson79d39652011-05-25 13:44:23 -0700643 struct pcm *pcm;
644 struct snd_pcm_info info;
645 struct snd_pcm_hw_params params;
646 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700647 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700648 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700649
650 pcm = calloc(1, sizeof(struct pcm));
651 if (!pcm || !config)
652 return &bad_pcm; /* TODO: could support default config here */
653
654 pcm->config = *config;
655
Simon Wilson1bd580f2011-06-02 15:58:41 -0700656 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
657 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700658
659 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700660 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700661 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700662 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700663 return pcm;
664 }
665
666 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700667 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700668 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700669 }
David Wagner4cddf192014-04-02 15:12:54 +0200670 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700671
672 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700673 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
674 pcm_format_to_alsa(config->format));
675 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
676 SNDRV_PCM_SUBFORMAT_STD);
677 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
678 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
679 pcm_format_to_bits(config->format));
680 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
681 pcm_format_to_bits(config->format) * config->channels);
682 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
683 config->channels);
684 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
685 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
686
Liam Girdwood6be28f12011-10-13 12:59:51 -0700687 if (flags & PCM_NOIRQ) {
688
689 if (!(flags & PCM_MMAP)) {
690 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
691 goto fail;
692 }
693
694 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
695 pcm->noirq_frames_per_msec = config->rate / 1000;
696 }
697
698 if (flags & PCM_MMAP)
699 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
700 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
701 else
702 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
703 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
704
Simon Wilson79d39652011-05-25 13:44:23 -0700705 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
706 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700707 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700708 }
709
Liam Girdwood6be28f12011-10-13 12:59:51 -0700710 /* get our refined hw_params */
711 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
712 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
713 pcm->buffer_size = config->period_count * config->period_size;
714
715 if (flags & PCM_MMAP) {
716 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
717 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
718 if (pcm->mmap_buffer == MAP_FAILED) {
719 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
720 pcm_frames_to_bytes(pcm, pcm->buffer_size));
721 goto fail_close;
722 }
723 }
724
725
Simon Wilson79d39652011-05-25 13:44:23 -0700726 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700727 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700728 sparams.period_step = 1;
729 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700730
Eric Laurent93e7b672012-08-22 16:18:14 -0700731 if (!config->start_threshold) {
732 if (pcm->flags & PCM_IN)
733 pcm->config.start_threshold = sparams.start_threshold = 1;
734 else
735 pcm->config.start_threshold = sparams.start_threshold =
736 config->period_count * config->period_size / 2;
737 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700738 sparams.start_threshold = config->start_threshold;
739
Liam Girdwood6be28f12011-10-13 12:59:51 -0700740 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800741 if (!config->stop_threshold) {
742 if (pcm->flags & PCM_IN)
743 pcm->config.stop_threshold = sparams.stop_threshold =
744 config->period_count * config->period_size * 10;
745 else
746 pcm->config.stop_threshold = sparams.stop_threshold =
747 config->period_count * config->period_size;
748 }
John Grossman3bb114a2011-07-21 10:59:55 -0700749 else
750 sparams.stop_threshold = config->stop_threshold;
751
Simon Wilson79d39652011-05-25 13:44:23 -0700752 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
753 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700754 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700755 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700756
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600757 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700758 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700759
760 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
761 oops(pcm, errno, "cannot set sw params");
762 goto fail;
763 }
764
Eric Laurent40b018e2011-06-18 10:10:23 -0700765 rc = pcm_hw_mmap_status(pcm);
766 if (rc < 0) {
767 oops(pcm, rc, "mmap status failed");
768 goto fail;
769 }
770
Glenn Kasten81012402013-08-22 15:11:48 -0700771#ifdef SNDRV_PCM_IOCTL_TTSTAMP
772 if (pcm->flags & PCM_MONOTONIC) {
773 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
774 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
775 if (rc < 0) {
776 oops(pcm, rc, "cannot set timestamp type");
777 goto fail;
778 }
779 }
780#endif
781
Simon Wilson79d39652011-05-25 13:44:23 -0700782 pcm->underruns = 0;
783 return pcm;
784
785fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700786 if (flags & PCM_MMAP)
787 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
788fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700789 close(pcm->fd);
790 pcm->fd = -1;
791 return pcm;
792}
793
794int pcm_is_ready(struct pcm *pcm)
795{
796 return pcm->fd >= 0;
797}
Simon Wilsond6458e62011-06-21 14:58:11 -0700798
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530799int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700800{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530801 if (pcm->prepared)
802 return 0;
803
Simon Wilsond6458e62011-06-21 14:58:11 -0700804 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
805 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700806
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530807 pcm->prepared = 1;
808 return 0;
809}
810
811int pcm_start(struct pcm *pcm)
812{
813 int prepare_error = pcm_prepare(pcm);
814 if (prepare_error)
815 return prepare_error;
816
Liam Girdwood6be28f12011-10-13 12:59:51 -0700817 if (pcm->flags & PCM_MMAP)
818 pcm_sync_ptr(pcm, 0);
819
Simon Wilsond6458e62011-06-21 14:58:11 -0700820 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
821 return oops(pcm, errno, "cannot start channel");
822
Liam Girdwood6be28f12011-10-13 12:59:51 -0700823 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700824 return 0;
825}
826
827int pcm_stop(struct pcm *pcm)
828{
829 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
830 return oops(pcm, errno, "cannot stop channel");
831
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530832 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700833 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700834 return 0;
835}
836
Liam Girdwood6be28f12011-10-13 12:59:51 -0700837static inline int pcm_mmap_playback_avail(struct pcm *pcm)
838{
839 int avail;
840
841 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
842
843 if (avail < 0)
844 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +0800845 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700846 avail -= pcm->boundary;
847
848 return avail;
849}
850
851static inline int pcm_mmap_capture_avail(struct pcm *pcm)
852{
853 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
854 if (avail < 0)
855 avail += pcm->boundary;
856 return avail;
857}
858
859static inline int pcm_mmap_avail(struct pcm *pcm)
860{
861 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
862 if (pcm->flags & PCM_IN)
863 return pcm_mmap_capture_avail(pcm);
864 else
865 return pcm_mmap_playback_avail(pcm);
866}
867
868static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
869{
870 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
871 appl_ptr += frames;
872
873 /* check for boundary wrap */
874 if (appl_ptr > pcm->boundary)
875 appl_ptr -= pcm->boundary;
876 pcm->mmap_control->appl_ptr = appl_ptr;
877}
878
879int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
880 unsigned int *frames)
881{
882 unsigned int continuous, copy_frames, avail;
883
884 /* return the mmap buffer */
885 *areas = pcm->mmap_buffer;
886
887 /* and the application offset in frames */
888 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
889
890 avail = pcm_mmap_avail(pcm);
891 if (avail > pcm->buffer_size)
892 avail = pcm->buffer_size;
893 continuous = pcm->buffer_size - *offset;
894
895 /* we can only copy frames if the are availabale and continuos */
896 copy_frames = *frames;
897 if (copy_frames > avail)
898 copy_frames = avail;
899 if (copy_frames > continuous)
900 copy_frames = continuous;
901 *frames = copy_frames;
902
903 return 0;
904}
905
906int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
907{
908 /* update the application pointer in userspace and kernel */
909 pcm_mmap_appl_forward(pcm, frames);
910 pcm_sync_ptr(pcm, 0);
911
912 return frames;
913}
914
915int pcm_avail_update(struct pcm *pcm)
916{
917 pcm_sync_ptr(pcm, 0);
918 return pcm_mmap_avail(pcm);
919}
920
921int pcm_state(struct pcm *pcm)
922{
923 int err = pcm_sync_ptr(pcm, 0);
924 if (err < 0)
925 return err;
926
927 return pcm->mmap_status->state;
928}
929
930int pcm_wait(struct pcm *pcm, int timeout)
931{
932 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700933 int err;
934
935 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +0100936 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700937
938 do {
939 /* let's wait for avail or timeout */
940 err = poll(&pfd, 1, timeout);
941 if (err < 0)
942 return -errno;
943
944 /* timeout ? */
945 if (err == 0)
946 return 0;
947
948 /* have we been interrupted ? */
949 if (errno == -EINTR)
950 continue;
951
952 /* check for any errors */
953 if (pfd.revents & (POLLERR | POLLNVAL)) {
954 switch (pcm_state(pcm)) {
955 case PCM_STATE_XRUN:
956 return -EPIPE;
957 case PCM_STATE_SUSPENDED:
958 return -ESTRPIPE;
959 case PCM_STATE_DISCONNECTED:
960 return -ENODEV;
961 default:
962 return -EIO;
963 }
964 }
965 /* poll again if fd not ready for IO */
966 } while (!(pfd.revents & (POLLIN | POLLOUT)));
967
968 return 1;
969}
970
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700971int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700972{
973 int err = 0, frames, avail;
974 unsigned int offset = 0, count;
975
976 if (bytes == 0)
977 return 0;
978
979 count = pcm_bytes_to_frames(pcm, bytes);
980
981 while (count > 0) {
982
983 /* get the available space for writing new frames */
984 avail = pcm_avail_update(pcm);
985 if (avail < 0) {
986 fprintf(stderr, "cannot determine available mmap frames");
987 return err;
988 }
989
990 /* start the audio if we reach the threshold */
991 if (!pcm->running &&
992 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
993 if (pcm_start(pcm) < 0) {
994 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
995 (unsigned int)pcm->mmap_status->hw_ptr,
996 (unsigned int)pcm->mmap_control->appl_ptr,
997 avail);
998 return -errno;
999 }
1000 }
1001
1002 /* sleep until we have space to write new frames */
1003 if (pcm->running &&
1004 (unsigned int)avail < pcm->mmap_control->avail_min) {
1005 int time = -1;
1006
1007 if (pcm->flags & PCM_NOIRQ)
1008 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1009 / pcm->noirq_frames_per_msec;
1010
1011 err = pcm_wait(pcm, time);
1012 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301013 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001014 pcm->running = 0;
1015 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1016 (unsigned int)pcm->mmap_status->hw_ptr,
1017 (unsigned int)pcm->mmap_control->appl_ptr,
1018 avail);
1019 pcm->mmap_control->appl_ptr = 0;
1020 return err;
1021 }
1022 continue;
1023 }
1024
1025 frames = count;
1026 if (frames > avail)
1027 frames = avail;
1028
1029 if (!frames)
1030 break;
1031
1032 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001033 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001034 if (frames < 0) {
1035 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1036 (unsigned int)pcm->mmap_status->hw_ptr,
1037 (unsigned int)pcm->mmap_control->appl_ptr,
1038 avail);
1039 return frames;
1040 }
1041
1042 offset += frames;
1043 count -= frames;
1044 }
1045
Liam Girdwood6be28f12011-10-13 12:59:51 -07001046 return 0;
1047}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001048
1049int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1050{
1051 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1052 return -ENOSYS;
1053
1054 return pcm_mmap_transfer(pcm, (void *)data, count);
1055}
1056
1057int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1058{
1059 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1060 return -ENOSYS;
1061
1062 return pcm_mmap_transfer(pcm, data, count);
1063}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301064
1065long pcm_get_delay(struct pcm *pcm)
1066{
1067 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1068 return -1;
1069
1070 return pcm->pcm_delay;
1071}