blob: 90c9dad243f45f7e2de5c60dd590080cc613f184 [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>
Liam Girdwood6be28f12011-10-13 12:59:51 -070041#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070042
43#include <linux/ioctl.h>
44#define __force
45#define __bitwise
46#define __user
47#include <sound/asound.h>
48
49#include <tinyalsa/asoundlib.h>
50
51#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070052#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070053
54static inline int param_is_mask(int p)
55{
56 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
57 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
58}
59
60static inline int param_is_interval(int p)
61{
62 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
63 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
64}
65
66static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
67{
68 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
69}
70
71static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
72{
73 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
74}
75
76static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
77{
78 if (bit >= SNDRV_MASK_MAX)
79 return;
80 if (param_is_mask(n)) {
81 struct snd_mask *m = param_to_mask(p, n);
82 m->bits[0] = 0;
83 m->bits[1] = 0;
84 m->bits[bit >> 5] |= (1 << (bit & 31));
85 }
86}
87
88static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
89{
90 if (param_is_interval(n)) {
91 struct snd_interval *i = param_to_interval(p, n);
92 i->min = val;
93 }
94}
95
Simon Wilson43544882012-10-31 12:52:39 -070096static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
97{
98 if (param_is_interval(n)) {
99 struct snd_interval *i = param_to_interval(p, n);
100 return i->min;
101 }
102 return 0;
103}
104
105static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
106{
107 if (param_is_interval(n)) {
108 struct snd_interval *i = param_to_interval(p, n);
109 return i->max;
110 }
111 return 0;
112}
113
Simon Wilson79d39652011-05-25 13:44:23 -0700114static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
115{
116 if (param_is_interval(n)) {
117 struct snd_interval *i = param_to_interval(p, n);
118 i->min = val;
119 i->max = val;
120 i->integer = 1;
121 }
122}
123
Liam Girdwood6be28f12011-10-13 12:59:51 -0700124static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
125{
126 if (param_is_interval(n)) {
127 struct snd_interval *i = param_to_interval(p, n);
128 if (i->integer)
129 return i->max;
130 }
131 return 0;
132}
133
Simon Wilson79d39652011-05-25 13:44:23 -0700134static void param_init(struct snd_pcm_hw_params *p)
135{
136 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700137
Simon Wilson79d39652011-05-25 13:44:23 -0700138 memset(p, 0, sizeof(*p));
139 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
140 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
141 struct snd_mask *m = param_to_mask(p, n);
142 m->bits[0] = ~0;
143 m->bits[1] = ~0;
144 }
145 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
146 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
147 struct snd_interval *i = param_to_interval(p, n);
148 i->min = 0;
149 i->max = ~0;
150 }
Simon Wilson43544882012-10-31 12:52:39 -0700151 p->rmask = ~0U;
152 p->cmask = 0;
153 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700154}
155
156#define PCM_ERROR_MAX 128
157
158struct pcm {
159 int fd;
160 unsigned int flags;
161 int running:1;
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530162 int prepared:1;
Simon Wilson79d39652011-05-25 13:44:23 -0700163 int underruns;
164 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700165 unsigned int boundary;
Simon Wilson79d39652011-05-25 13:44:23 -0700166 char error[PCM_ERROR_MAX];
167 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700168 struct snd_pcm_mmap_status *mmap_status;
169 struct snd_pcm_mmap_control *mmap_control;
170 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700171 void *mmap_buffer;
172 unsigned int noirq_frames_per_msec;
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530173 long pcm_delay;
Simon Wilson79d39652011-05-25 13:44:23 -0700174};
175
Simon Wilson851aa5c2011-05-30 21:18:26 -0700176unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700177{
178 return pcm->buffer_size;
179}
180
181const char* pcm_get_error(struct pcm *pcm)
182{
183 return pcm->error;
184}
185
186static int oops(struct pcm *pcm, int e, const char *fmt, ...)
187{
188 va_list ap;
189 int sz;
190
191 va_start(ap, fmt);
192 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
193 va_end(ap);
194 sz = strlen(pcm->error);
195
196 if (errno)
197 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
198 ": %s", strerror(e));
199 return -1;
200}
201
Simon Wilsonbc03b622011-06-15 17:19:01 -0700202static unsigned int pcm_format_to_alsa(enum pcm_format format)
203{
204 switch (format) {
205 case PCM_FORMAT_S32_LE:
206 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500207 case PCM_FORMAT_S8:
208 return SNDRV_PCM_FORMAT_S8;
209 case PCM_FORMAT_S24_LE:
210 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700211 default:
212 case PCM_FORMAT_S16_LE:
213 return SNDRV_PCM_FORMAT_S16_LE;
214 };
215}
216
Simon Wilson7136cf72013-07-17 10:30:35 -0700217unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700218{
219 switch (format) {
220 case PCM_FORMAT_S32_LE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700221 case PCM_FORMAT_S24_LE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700222 return 32;
223 default:
224 case PCM_FORMAT_S16_LE:
225 return 16;
226 };
227}
228
Liam Girdwood6be28f12011-10-13 12:59:51 -0700229unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
230{
231 return bytes / (pcm->config.channels *
232 (pcm_format_to_bits(pcm->config.format) >> 3));
233}
234
235unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
236{
237 return frames * pcm->config.channels *
238 (pcm_format_to_bits(pcm->config.format) >> 3);
239}
240
Eric Laurent40b018e2011-06-18 10:10:23 -0700241static int pcm_sync_ptr(struct pcm *pcm, int flags) {
242 if (pcm->sync_ptr) {
243 pcm->sync_ptr->flags = flags;
244 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
245 return -1;
246 }
247 return 0;
248}
249
250static int pcm_hw_mmap_status(struct pcm *pcm) {
251
252 if (pcm->sync_ptr)
253 return 0;
254
255 int page_size = sysconf(_SC_PAGE_SIZE);
256 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
257 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
258 if (pcm->mmap_status == MAP_FAILED)
259 pcm->mmap_status = NULL;
260 if (!pcm->mmap_status)
261 goto mmap_error;
262
263 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
264 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
265 if (pcm->mmap_control == MAP_FAILED)
266 pcm->mmap_control = NULL;
267 if (!pcm->mmap_control) {
268 munmap(pcm->mmap_status, page_size);
269 pcm->mmap_status = NULL;
270 goto mmap_error;
271 }
272 pcm->mmap_control->avail_min = 1;
273
274 return 0;
275
276mmap_error:
277
278 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
279 if (!pcm->sync_ptr)
280 return -ENOMEM;
281 pcm->mmap_status = &pcm->sync_ptr->s.status;
282 pcm->mmap_control = &pcm->sync_ptr->c.control;
283 pcm->mmap_control->avail_min = 1;
284 pcm_sync_ptr(pcm, 0);
285
286 return 0;
287}
288
289static void pcm_hw_munmap_status(struct pcm *pcm) {
290 if (pcm->sync_ptr) {
291 free(pcm->sync_ptr);
292 pcm->sync_ptr = NULL;
293 } else {
294 int page_size = sysconf(_SC_PAGE_SIZE);
295 if (pcm->mmap_status)
296 munmap(pcm->mmap_status, page_size);
297 if (pcm->mmap_control)
298 munmap(pcm->mmap_control, page_size);
299 }
300 pcm->mmap_status = NULL;
301 pcm->mmap_control = NULL;
302}
303
Liam Girdwood6be28f12011-10-13 12:59:51 -0700304static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700305 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700306 unsigned int frames)
307{
308 int size_bytes = pcm_frames_to_bytes(pcm, frames);
309 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
310 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
311
312 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700313 if (pcm->flags & PCM_IN)
314 memcpy(buf + src_offset_bytes,
315 (char*)pcm->mmap_buffer + pcm_offset_bytes,
316 size_bytes);
317 else
318 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
319 buf + src_offset_bytes,
320 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700321 return 0;
322}
323
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700324static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700325 unsigned int offset, unsigned int size)
326{
327 void *pcm_areas;
328 int commit;
329 unsigned int pcm_offset, frames, count = 0;
330
331 while (size > 0) {
332 frames = size;
333 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700334 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700335 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
336 if (commit < 0) {
337 oops(pcm, commit, "failed to commit %d frames\n", frames);
338 return commit;
339 }
340
341 offset += commit;
342 count += commit;
343 size -= commit;
344 }
345 return count;
346}
347
Eric Laurent40b018e2011-06-18 10:10:23 -0700348int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
349 struct timespec *tstamp)
350{
351 int frames;
352 int rc;
353 snd_pcm_uframes_t hw_ptr;
354
355 if (!pcm_is_ready(pcm))
356 return -1;
357
358 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
359 if (rc < 0)
360 return -1;
361
Eric Laurent7db48582011-11-17 11:47:59 -0800362 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
363 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800364 return -1;
365
Eric Laurent40b018e2011-06-18 10:10:23 -0700366 *tstamp = pcm->mmap_status->tstamp;
367 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
368 return -1;
369
370 hw_ptr = pcm->mmap_status->hw_ptr;
371 if (pcm->flags & PCM_IN)
372 frames = hw_ptr - pcm->mmap_control->appl_ptr;
373 else
374 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
375
376 if (frames < 0)
377 return -1;
378
379 *avail = (unsigned int)frames;
380
381 return 0;
382}
383
Mark Brown6bbe77a2012-02-10 22:07:09 +0000384int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700385{
386 struct snd_xferi x;
387
388 if (pcm->flags & PCM_IN)
389 return -EINVAL;
390
Mark Brown6bbe77a2012-02-10 22:07:09 +0000391 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700392 x.frames = count / (pcm->config.channels *
393 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700394
395 for (;;) {
396 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530397 int prepare_error = pcm_prepare(pcm);
398 if (prepare_error)
399 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700400 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
401 return oops(pcm, errno, "cannot write initial data");
402 pcm->running = 1;
403 return 0;
404 }
405 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530406 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700407 pcm->running = 0;
408 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700409 /* we failed to make our window -- try to restart if we are
410 * allowed to do so. Otherwise, simply allow the EPIPE error to
411 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700412 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700413 if (pcm->flags & PCM_NORESTART)
414 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700415 continue;
416 }
417 return oops(pcm, errno, "cannot write stream data");
418 }
419 return 0;
420 }
421}
422
423int pcm_read(struct pcm *pcm, void *data, unsigned int count)
424{
425 struct snd_xferi x;
426
427 if (!(pcm->flags & PCM_IN))
428 return -EINVAL;
429
430 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700431 x.frames = count / (pcm->config.channels *
432 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700433
434 for (;;) {
435 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700436 if (pcm_start(pcm) < 0) {
437 fprintf(stderr, "start error");
438 return -errno;
439 }
Simon Wilson79d39652011-05-25 13:44:23 -0700440 }
441 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530442 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700443 pcm->running = 0;
444 if (errno == EPIPE) {
445 /* we failed to make our window -- try to restart */
446 pcm->underruns++;
447 continue;
448 }
449 return oops(pcm, errno, "cannot read stream data");
450 }
451 return 0;
452 }
453}
454
455static struct pcm bad_pcm = {
456 .fd = -1,
457};
458
Simon Wilson43544882012-10-31 12:52:39 -0700459struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
460 unsigned int flags)
461{
462 struct snd_pcm_hw_params *params;
463 char fn[256];
464 int fd;
465
466 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
467 flags & PCM_IN ? 'c' : 'p');
468
469 fd = open(fn, O_RDWR);
470 if (fd < 0) {
471 fprintf(stderr, "cannot open device '%s'\n", fn);
472 goto err_open;
473 }
474
475 params = calloc(1, sizeof(struct snd_pcm_hw_params));
476 if (!params)
477 goto err_calloc;
478
479 param_init(params);
480 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
481 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
482 goto err_hw_refine;
483 }
484
485 close(fd);
486
487 return (struct pcm_params *)params;
488
489err_hw_refine:
490 free(params);
491err_calloc:
492 close(fd);
493err_open:
494 return NULL;
495}
496
497void pcm_params_free(struct pcm_params *pcm_params)
498{
499 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
500
501 if (params)
502 free(params);
503}
504
505static int pcm_param_to_alsa(enum pcm_param param)
506{
507 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700508 case PCM_PARAM_ACCESS:
509 return SNDRV_PCM_HW_PARAM_ACCESS;
510 case PCM_PARAM_FORMAT:
511 return SNDRV_PCM_HW_PARAM_FORMAT;
512 case PCM_PARAM_SUBFORMAT:
513 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700514 case PCM_PARAM_SAMPLE_BITS:
515 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
516 break;
517 case PCM_PARAM_FRAME_BITS:
518 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
519 break;
520 case PCM_PARAM_CHANNELS:
521 return SNDRV_PCM_HW_PARAM_CHANNELS;
522 break;
523 case PCM_PARAM_RATE:
524 return SNDRV_PCM_HW_PARAM_RATE;
525 break;
526 case PCM_PARAM_PERIOD_TIME:
527 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
528 break;
529 case PCM_PARAM_PERIOD_SIZE:
530 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
531 break;
532 case PCM_PARAM_PERIOD_BYTES:
533 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
534 break;
535 case PCM_PARAM_PERIODS:
536 return SNDRV_PCM_HW_PARAM_PERIODS;
537 break;
538 case PCM_PARAM_BUFFER_TIME:
539 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
540 break;
541 case PCM_PARAM_BUFFER_SIZE:
542 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
543 break;
544 case PCM_PARAM_BUFFER_BYTES:
545 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
546 break;
547 case PCM_PARAM_TICK_TIME:
548 return SNDRV_PCM_HW_PARAM_TICK_TIME;
549 break;
550
551 default:
552 return -1;
553 }
554}
555
Andy Hungad807622014-03-10 18:08:15 -0700556struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
557 enum pcm_param param)
558{
559 int p;
560 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
561 if (params == NULL) {
562 return NULL;
563 }
564
565 p = pcm_param_to_alsa(param);
566 if (p < 0 || !param_is_mask(p)) {
567 return NULL;
568 }
569
570 return (struct pcm_mask *)param_to_mask(params, p);
571}
572
Simon Wilson43544882012-10-31 12:52:39 -0700573unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
574 enum pcm_param param)
575{
576 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
577 int p;
578
579 if (!params)
580 return 0;
581
582 p = pcm_param_to_alsa(param);
583 if (p < 0)
584 return 0;
585
586 return param_get_min(params, p);
587}
588
589unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
590 enum pcm_param param)
591{
592 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
593 int p;
594
595 if (!params)
596 return 0;
597
598 p = pcm_param_to_alsa(param);
599 if (p < 0)
600 return 0;
601
602 return param_get_max(params, p);
603}
604
Simon Wilson79d39652011-05-25 13:44:23 -0700605int pcm_close(struct pcm *pcm)
606{
607 if (pcm == &bad_pcm)
608 return 0;
609
Eric Laurent40b018e2011-06-18 10:10:23 -0700610 pcm_hw_munmap_status(pcm);
611
Liam Girdwood6be28f12011-10-13 12:59:51 -0700612 if (pcm->flags & PCM_MMAP) {
613 pcm_stop(pcm);
614 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
615 }
616
Simon Wilson79d39652011-05-25 13:44:23 -0700617 if (pcm->fd >= 0)
618 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530619 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700620 pcm->running = 0;
621 pcm->buffer_size = 0;
622 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700623 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700624 return 0;
625}
626
Simon Wilson1bd580f2011-06-02 15:58:41 -0700627struct pcm *pcm_open(unsigned int card, unsigned int device,
628 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700629{
Simon Wilson79d39652011-05-25 13:44:23 -0700630 struct pcm *pcm;
631 struct snd_pcm_info info;
632 struct snd_pcm_hw_params params;
633 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700634 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700635 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700636
637 pcm = calloc(1, sizeof(struct pcm));
638 if (!pcm || !config)
639 return &bad_pcm; /* TODO: could support default config here */
640
641 pcm->config = *config;
642
Simon Wilson1bd580f2011-06-02 15:58:41 -0700643 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
644 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700645
646 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700647 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700648 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700649 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700650 return pcm;
651 }
652
653 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700654 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700655 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700656 }
657
658 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700659 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
660 pcm_format_to_alsa(config->format));
661 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
662 SNDRV_PCM_SUBFORMAT_STD);
663 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
664 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
665 pcm_format_to_bits(config->format));
666 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
667 pcm_format_to_bits(config->format) * config->channels);
668 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
669 config->channels);
670 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
671 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
672
Liam Girdwood6be28f12011-10-13 12:59:51 -0700673 if (flags & PCM_NOIRQ) {
674
675 if (!(flags & PCM_MMAP)) {
676 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
677 goto fail;
678 }
679
680 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
681 pcm->noirq_frames_per_msec = config->rate / 1000;
682 }
683
684 if (flags & PCM_MMAP)
685 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
686 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
687 else
688 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
689 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
690
Simon Wilson79d39652011-05-25 13:44:23 -0700691 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
692 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700693 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700694 }
695
Liam Girdwood6be28f12011-10-13 12:59:51 -0700696 /* get our refined hw_params */
697 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
698 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
699 pcm->buffer_size = config->period_count * config->period_size;
700
701 if (flags & PCM_MMAP) {
702 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
703 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
704 if (pcm->mmap_buffer == MAP_FAILED) {
705 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
706 pcm_frames_to_bytes(pcm, pcm->buffer_size));
707 goto fail_close;
708 }
709 }
710
711
Simon Wilson79d39652011-05-25 13:44:23 -0700712 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700713 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700714 sparams.period_step = 1;
715 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700716
Eric Laurent93e7b672012-08-22 16:18:14 -0700717 if (!config->start_threshold) {
718 if (pcm->flags & PCM_IN)
719 pcm->config.start_threshold = sparams.start_threshold = 1;
720 else
721 pcm->config.start_threshold = sparams.start_threshold =
722 config->period_count * config->period_size / 2;
723 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700724 sparams.start_threshold = config->start_threshold;
725
Liam Girdwood6be28f12011-10-13 12:59:51 -0700726 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800727 if (!config->stop_threshold) {
728 if (pcm->flags & PCM_IN)
729 pcm->config.stop_threshold = sparams.stop_threshold =
730 config->period_count * config->period_size * 10;
731 else
732 pcm->config.stop_threshold = sparams.stop_threshold =
733 config->period_count * config->period_size;
734 }
John Grossman3bb114a2011-07-21 10:59:55 -0700735 else
736 sparams.stop_threshold = config->stop_threshold;
737
Simon Wilson79d39652011-05-25 13:44:23 -0700738 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
739 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700740 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700741 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700742
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600743 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700744 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700745
746 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
747 oops(pcm, errno, "cannot set sw params");
748 goto fail;
749 }
750
Eric Laurent40b018e2011-06-18 10:10:23 -0700751 rc = pcm_hw_mmap_status(pcm);
752 if (rc < 0) {
753 oops(pcm, rc, "mmap status failed");
754 goto fail;
755 }
756
Glenn Kasten81012402013-08-22 15:11:48 -0700757#ifdef SNDRV_PCM_IOCTL_TTSTAMP
758 if (pcm->flags & PCM_MONOTONIC) {
759 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
760 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
761 if (rc < 0) {
762 oops(pcm, rc, "cannot set timestamp type");
763 goto fail;
764 }
765 }
766#endif
767
Simon Wilson79d39652011-05-25 13:44:23 -0700768 pcm->underruns = 0;
769 return pcm;
770
771fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700772 if (flags & PCM_MMAP)
773 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
774fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700775 close(pcm->fd);
776 pcm->fd = -1;
777 return pcm;
778}
779
780int pcm_is_ready(struct pcm *pcm)
781{
782 return pcm->fd >= 0;
783}
Simon Wilsond6458e62011-06-21 14:58:11 -0700784
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530785int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700786{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530787 if (pcm->prepared)
788 return 0;
789
Simon Wilsond6458e62011-06-21 14:58:11 -0700790 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
791 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700792
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530793 pcm->prepared = 1;
794 return 0;
795}
796
797int pcm_start(struct pcm *pcm)
798{
799 int prepare_error = pcm_prepare(pcm);
800 if (prepare_error)
801 return prepare_error;
802
Liam Girdwood6be28f12011-10-13 12:59:51 -0700803 if (pcm->flags & PCM_MMAP)
804 pcm_sync_ptr(pcm, 0);
805
Simon Wilsond6458e62011-06-21 14:58:11 -0700806 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
807 return oops(pcm, errno, "cannot start channel");
808
Liam Girdwood6be28f12011-10-13 12:59:51 -0700809 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700810 return 0;
811}
812
813int pcm_stop(struct pcm *pcm)
814{
815 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
816 return oops(pcm, errno, "cannot stop channel");
817
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530818 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700819 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700820 return 0;
821}
822
Liam Girdwood6be28f12011-10-13 12:59:51 -0700823static inline int pcm_mmap_playback_avail(struct pcm *pcm)
824{
825 int avail;
826
827 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
828
829 if (avail < 0)
830 avail += pcm->boundary;
831 else if (avail > (int)pcm->boundary)
832 avail -= pcm->boundary;
833
834 return avail;
835}
836
837static inline int pcm_mmap_capture_avail(struct pcm *pcm)
838{
839 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
840 if (avail < 0)
841 avail += pcm->boundary;
842 return avail;
843}
844
845static inline int pcm_mmap_avail(struct pcm *pcm)
846{
847 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
848 if (pcm->flags & PCM_IN)
849 return pcm_mmap_capture_avail(pcm);
850 else
851 return pcm_mmap_playback_avail(pcm);
852}
853
854static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
855{
856 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
857 appl_ptr += frames;
858
859 /* check for boundary wrap */
860 if (appl_ptr > pcm->boundary)
861 appl_ptr -= pcm->boundary;
862 pcm->mmap_control->appl_ptr = appl_ptr;
863}
864
865int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
866 unsigned int *frames)
867{
868 unsigned int continuous, copy_frames, avail;
869
870 /* return the mmap buffer */
871 *areas = pcm->mmap_buffer;
872
873 /* and the application offset in frames */
874 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
875
876 avail = pcm_mmap_avail(pcm);
877 if (avail > pcm->buffer_size)
878 avail = pcm->buffer_size;
879 continuous = pcm->buffer_size - *offset;
880
881 /* we can only copy frames if the are availabale and continuos */
882 copy_frames = *frames;
883 if (copy_frames > avail)
884 copy_frames = avail;
885 if (copy_frames > continuous)
886 copy_frames = continuous;
887 *frames = copy_frames;
888
889 return 0;
890}
891
892int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
893{
894 /* update the application pointer in userspace and kernel */
895 pcm_mmap_appl_forward(pcm, frames);
896 pcm_sync_ptr(pcm, 0);
897
898 return frames;
899}
900
901int pcm_avail_update(struct pcm *pcm)
902{
903 pcm_sync_ptr(pcm, 0);
904 return pcm_mmap_avail(pcm);
905}
906
907int pcm_state(struct pcm *pcm)
908{
909 int err = pcm_sync_ptr(pcm, 0);
910 if (err < 0)
911 return err;
912
913 return pcm->mmap_status->state;
914}
915
916int pcm_wait(struct pcm *pcm, int timeout)
917{
918 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700919 int err;
920
921 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +0100922 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700923
924 do {
925 /* let's wait for avail or timeout */
926 err = poll(&pfd, 1, timeout);
927 if (err < 0)
928 return -errno;
929
930 /* timeout ? */
931 if (err == 0)
932 return 0;
933
934 /* have we been interrupted ? */
935 if (errno == -EINTR)
936 continue;
937
938 /* check for any errors */
939 if (pfd.revents & (POLLERR | POLLNVAL)) {
940 switch (pcm_state(pcm)) {
941 case PCM_STATE_XRUN:
942 return -EPIPE;
943 case PCM_STATE_SUSPENDED:
944 return -ESTRPIPE;
945 case PCM_STATE_DISCONNECTED:
946 return -ENODEV;
947 default:
948 return -EIO;
949 }
950 }
951 /* poll again if fd not ready for IO */
952 } while (!(pfd.revents & (POLLIN | POLLOUT)));
953
954 return 1;
955}
956
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700957int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700958{
959 int err = 0, frames, avail;
960 unsigned int offset = 0, count;
961
962 if (bytes == 0)
963 return 0;
964
965 count = pcm_bytes_to_frames(pcm, bytes);
966
967 while (count > 0) {
968
969 /* get the available space for writing new frames */
970 avail = pcm_avail_update(pcm);
971 if (avail < 0) {
972 fprintf(stderr, "cannot determine available mmap frames");
973 return err;
974 }
975
976 /* start the audio if we reach the threshold */
977 if (!pcm->running &&
978 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
979 if (pcm_start(pcm) < 0) {
980 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
981 (unsigned int)pcm->mmap_status->hw_ptr,
982 (unsigned int)pcm->mmap_control->appl_ptr,
983 avail);
984 return -errno;
985 }
986 }
987
988 /* sleep until we have space to write new frames */
989 if (pcm->running &&
990 (unsigned int)avail < pcm->mmap_control->avail_min) {
991 int time = -1;
992
993 if (pcm->flags & PCM_NOIRQ)
994 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
995 / pcm->noirq_frames_per_msec;
996
997 err = pcm_wait(pcm, time);
998 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530999 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001000 pcm->running = 0;
1001 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1002 (unsigned int)pcm->mmap_status->hw_ptr,
1003 (unsigned int)pcm->mmap_control->appl_ptr,
1004 avail);
1005 pcm->mmap_control->appl_ptr = 0;
1006 return err;
1007 }
1008 continue;
1009 }
1010
1011 frames = count;
1012 if (frames > avail)
1013 frames = avail;
1014
1015 if (!frames)
1016 break;
1017
1018 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001019 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001020 if (frames < 0) {
1021 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1022 (unsigned int)pcm->mmap_status->hw_ptr,
1023 (unsigned int)pcm->mmap_control->appl_ptr,
1024 avail);
1025 return frames;
1026 }
1027
1028 offset += frames;
1029 count -= frames;
1030 }
1031
Liam Girdwood6be28f12011-10-13 12:59:51 -07001032 return 0;
1033}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001034
1035int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1036{
1037 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1038 return -ENOSYS;
1039
1040 return pcm_mmap_transfer(pcm, (void *)data, count);
1041}
1042
1043int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1044{
1045 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1046 return -ENOSYS;
1047
1048 return pcm_mmap_transfer(pcm, data, count);
1049}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301050
1051long pcm_get_delay(struct pcm *pcm)
1052{
1053 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1054 return -1;
1055
1056 return pcm->pcm_delay;
1057}