blob: 074422af77f17e6eba336a1e5120e7832ce54547 [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;
Simon Wilson79d39652011-05-25 13:44:23 -0700173};
174
Simon Wilson851aa5c2011-05-30 21:18:26 -0700175unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700176{
177 return pcm->buffer_size;
178}
179
180const char* pcm_get_error(struct pcm *pcm)
181{
182 return pcm->error;
183}
184
185static int oops(struct pcm *pcm, int e, const char *fmt, ...)
186{
187 va_list ap;
188 int sz;
189
190 va_start(ap, fmt);
191 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
192 va_end(ap);
193 sz = strlen(pcm->error);
194
195 if (errno)
196 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
197 ": %s", strerror(e));
198 return -1;
199}
200
Simon Wilsonbc03b622011-06-15 17:19:01 -0700201static unsigned int pcm_format_to_alsa(enum pcm_format format)
202{
203 switch (format) {
204 case PCM_FORMAT_S32_LE:
205 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500206 case PCM_FORMAT_S8:
207 return SNDRV_PCM_FORMAT_S8;
208 case PCM_FORMAT_S24_LE:
209 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700210 default:
211 case PCM_FORMAT_S16_LE:
212 return SNDRV_PCM_FORMAT_S16_LE;
213 };
214}
215
Simon Wilson7136cf72013-07-17 10:30:35 -0700216unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700217{
218 switch (format) {
219 case PCM_FORMAT_S32_LE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700220 case PCM_FORMAT_S24_LE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700221 return 32;
222 default:
223 case PCM_FORMAT_S16_LE:
224 return 16;
225 };
226}
227
Liam Girdwood6be28f12011-10-13 12:59:51 -0700228unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
229{
230 return bytes / (pcm->config.channels *
231 (pcm_format_to_bits(pcm->config.format) >> 3));
232}
233
234unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
235{
236 return frames * pcm->config.channels *
237 (pcm_format_to_bits(pcm->config.format) >> 3);
238}
239
Eric Laurent40b018e2011-06-18 10:10:23 -0700240static int pcm_sync_ptr(struct pcm *pcm, int flags) {
241 if (pcm->sync_ptr) {
242 pcm->sync_ptr->flags = flags;
243 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
244 return -1;
245 }
246 return 0;
247}
248
249static int pcm_hw_mmap_status(struct pcm *pcm) {
250
251 if (pcm->sync_ptr)
252 return 0;
253
254 int page_size = sysconf(_SC_PAGE_SIZE);
255 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
256 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
257 if (pcm->mmap_status == MAP_FAILED)
258 pcm->mmap_status = NULL;
259 if (!pcm->mmap_status)
260 goto mmap_error;
261
262 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
263 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
264 if (pcm->mmap_control == MAP_FAILED)
265 pcm->mmap_control = NULL;
266 if (!pcm->mmap_control) {
267 munmap(pcm->mmap_status, page_size);
268 pcm->mmap_status = NULL;
269 goto mmap_error;
270 }
271 pcm->mmap_control->avail_min = 1;
272
273 return 0;
274
275mmap_error:
276
277 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
278 if (!pcm->sync_ptr)
279 return -ENOMEM;
280 pcm->mmap_status = &pcm->sync_ptr->s.status;
281 pcm->mmap_control = &pcm->sync_ptr->c.control;
282 pcm->mmap_control->avail_min = 1;
283 pcm_sync_ptr(pcm, 0);
284
285 return 0;
286}
287
288static void pcm_hw_munmap_status(struct pcm *pcm) {
289 if (pcm->sync_ptr) {
290 free(pcm->sync_ptr);
291 pcm->sync_ptr = NULL;
292 } else {
293 int page_size = sysconf(_SC_PAGE_SIZE);
294 if (pcm->mmap_status)
295 munmap(pcm->mmap_status, page_size);
296 if (pcm->mmap_control)
297 munmap(pcm->mmap_control, page_size);
298 }
299 pcm->mmap_status = NULL;
300 pcm->mmap_control = NULL;
301}
302
Liam Girdwood6be28f12011-10-13 12:59:51 -0700303static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700304 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700305 unsigned int frames)
306{
307 int size_bytes = pcm_frames_to_bytes(pcm, frames);
308 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
309 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
310
311 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700312 if (pcm->flags & PCM_IN)
313 memcpy(buf + src_offset_bytes,
314 (char*)pcm->mmap_buffer + pcm_offset_bytes,
315 size_bytes);
316 else
317 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
318 buf + src_offset_bytes,
319 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700320 return 0;
321}
322
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700323static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700324 unsigned int offset, unsigned int size)
325{
326 void *pcm_areas;
327 int commit;
328 unsigned int pcm_offset, frames, count = 0;
329
330 while (size > 0) {
331 frames = size;
332 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700333 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700334 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
335 if (commit < 0) {
336 oops(pcm, commit, "failed to commit %d frames\n", frames);
337 return commit;
338 }
339
340 offset += commit;
341 count += commit;
342 size -= commit;
343 }
344 return count;
345}
346
Eric Laurent40b018e2011-06-18 10:10:23 -0700347int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
348 struct timespec *tstamp)
349{
350 int frames;
351 int rc;
352 snd_pcm_uframes_t hw_ptr;
353
354 if (!pcm_is_ready(pcm))
355 return -1;
356
357 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
358 if (rc < 0)
359 return -1;
360
Eric Laurent7db48582011-11-17 11:47:59 -0800361 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
362 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800363 return -1;
364
Eric Laurent40b018e2011-06-18 10:10:23 -0700365 *tstamp = pcm->mmap_status->tstamp;
366 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
367 return -1;
368
369 hw_ptr = pcm->mmap_status->hw_ptr;
370 if (pcm->flags & PCM_IN)
371 frames = hw_ptr - pcm->mmap_control->appl_ptr;
372 else
373 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
374
375 if (frames < 0)
376 return -1;
377
378 *avail = (unsigned int)frames;
379
380 return 0;
381}
382
Mark Brown6bbe77a2012-02-10 22:07:09 +0000383int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700384{
385 struct snd_xferi x;
386
387 if (pcm->flags & PCM_IN)
388 return -EINVAL;
389
Mark Brown6bbe77a2012-02-10 22:07:09 +0000390 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700391 x.frames = count / (pcm->config.channels *
392 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700393
394 for (;;) {
395 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530396 int prepare_error = pcm_prepare(pcm);
397 if (prepare_error)
398 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700399 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
400 return oops(pcm, errno, "cannot write initial data");
401 pcm->running = 1;
402 return 0;
403 }
404 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530405 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700406 pcm->running = 0;
407 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700408 /* we failed to make our window -- try to restart if we are
409 * allowed to do so. Otherwise, simply allow the EPIPE error to
410 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700411 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700412 if (pcm->flags & PCM_NORESTART)
413 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700414 continue;
415 }
416 return oops(pcm, errno, "cannot write stream data");
417 }
418 return 0;
419 }
420}
421
422int pcm_read(struct pcm *pcm, void *data, unsigned int count)
423{
424 struct snd_xferi x;
425
426 if (!(pcm->flags & PCM_IN))
427 return -EINVAL;
428
429 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700430 x.frames = count / (pcm->config.channels *
431 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700432
433 for (;;) {
434 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700435 if (pcm_start(pcm) < 0) {
436 fprintf(stderr, "start error");
437 return -errno;
438 }
Simon Wilson79d39652011-05-25 13:44:23 -0700439 }
440 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530441 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700442 pcm->running = 0;
443 if (errno == EPIPE) {
444 /* we failed to make our window -- try to restart */
445 pcm->underruns++;
446 continue;
447 }
448 return oops(pcm, errno, "cannot read stream data");
449 }
450 return 0;
451 }
452}
453
454static struct pcm bad_pcm = {
455 .fd = -1,
456};
457
Simon Wilson43544882012-10-31 12:52:39 -0700458struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
459 unsigned int flags)
460{
461 struct snd_pcm_hw_params *params;
462 char fn[256];
463 int fd;
464
465 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
466 flags & PCM_IN ? 'c' : 'p');
467
468 fd = open(fn, O_RDWR);
469 if (fd < 0) {
470 fprintf(stderr, "cannot open device '%s'\n", fn);
471 goto err_open;
472 }
473
474 params = calloc(1, sizeof(struct snd_pcm_hw_params));
475 if (!params)
476 goto err_calloc;
477
478 param_init(params);
479 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
480 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
481 goto err_hw_refine;
482 }
483
484 close(fd);
485
486 return (struct pcm_params *)params;
487
488err_hw_refine:
489 free(params);
490err_calloc:
491 close(fd);
492err_open:
493 return NULL;
494}
495
496void pcm_params_free(struct pcm_params *pcm_params)
497{
498 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
499
500 if (params)
501 free(params);
502}
503
504static int pcm_param_to_alsa(enum pcm_param param)
505{
506 switch (param) {
507 case PCM_PARAM_SAMPLE_BITS:
508 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
509 break;
510 case PCM_PARAM_FRAME_BITS:
511 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
512 break;
513 case PCM_PARAM_CHANNELS:
514 return SNDRV_PCM_HW_PARAM_CHANNELS;
515 break;
516 case PCM_PARAM_RATE:
517 return SNDRV_PCM_HW_PARAM_RATE;
518 break;
519 case PCM_PARAM_PERIOD_TIME:
520 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
521 break;
522 case PCM_PARAM_PERIOD_SIZE:
523 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
524 break;
525 case PCM_PARAM_PERIOD_BYTES:
526 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
527 break;
528 case PCM_PARAM_PERIODS:
529 return SNDRV_PCM_HW_PARAM_PERIODS;
530 break;
531 case PCM_PARAM_BUFFER_TIME:
532 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
533 break;
534 case PCM_PARAM_BUFFER_SIZE:
535 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
536 break;
537 case PCM_PARAM_BUFFER_BYTES:
538 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
539 break;
540 case PCM_PARAM_TICK_TIME:
541 return SNDRV_PCM_HW_PARAM_TICK_TIME;
542 break;
543
544 default:
545 return -1;
546 }
547}
548
549unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
550 enum pcm_param param)
551{
552 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
553 int p;
554
555 if (!params)
556 return 0;
557
558 p = pcm_param_to_alsa(param);
559 if (p < 0)
560 return 0;
561
562 return param_get_min(params, p);
563}
564
565unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
566 enum pcm_param param)
567{
568 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
569 int p;
570
571 if (!params)
572 return 0;
573
574 p = pcm_param_to_alsa(param);
575 if (p < 0)
576 return 0;
577
578 return param_get_max(params, p);
579}
580
Simon Wilson79d39652011-05-25 13:44:23 -0700581int pcm_close(struct pcm *pcm)
582{
583 if (pcm == &bad_pcm)
584 return 0;
585
Eric Laurent40b018e2011-06-18 10:10:23 -0700586 pcm_hw_munmap_status(pcm);
587
Liam Girdwood6be28f12011-10-13 12:59:51 -0700588 if (pcm->flags & PCM_MMAP) {
589 pcm_stop(pcm);
590 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
591 }
592
Simon Wilson79d39652011-05-25 13:44:23 -0700593 if (pcm->fd >= 0)
594 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530595 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700596 pcm->running = 0;
597 pcm->buffer_size = 0;
598 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700599 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700600 return 0;
601}
602
Simon Wilson1bd580f2011-06-02 15:58:41 -0700603struct pcm *pcm_open(unsigned int card, unsigned int device,
604 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700605{
Simon Wilson79d39652011-05-25 13:44:23 -0700606 struct pcm *pcm;
607 struct snd_pcm_info info;
608 struct snd_pcm_hw_params params;
609 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700610 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700611 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700612
613 pcm = calloc(1, sizeof(struct pcm));
614 if (!pcm || !config)
615 return &bad_pcm; /* TODO: could support default config here */
616
617 pcm->config = *config;
618
Simon Wilson1bd580f2011-06-02 15:58:41 -0700619 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
620 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700621
622 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700623 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700624 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700625 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700626 return pcm;
627 }
628
629 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700630 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700631 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700632 }
633
634 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700635 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
636 pcm_format_to_alsa(config->format));
637 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
638 SNDRV_PCM_SUBFORMAT_STD);
639 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
640 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
641 pcm_format_to_bits(config->format));
642 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
643 pcm_format_to_bits(config->format) * config->channels);
644 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
645 config->channels);
646 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
647 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
648
Liam Girdwood6be28f12011-10-13 12:59:51 -0700649 if (flags & PCM_NOIRQ) {
650
651 if (!(flags & PCM_MMAP)) {
652 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
653 goto fail;
654 }
655
656 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
657 pcm->noirq_frames_per_msec = config->rate / 1000;
658 }
659
660 if (flags & PCM_MMAP)
661 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
662 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
663 else
664 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
665 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
666
Simon Wilson79d39652011-05-25 13:44:23 -0700667 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
668 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700669 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700670 }
671
Liam Girdwood6be28f12011-10-13 12:59:51 -0700672 /* get our refined hw_params */
673 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
674 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
675 pcm->buffer_size = config->period_count * config->period_size;
676
677 if (flags & PCM_MMAP) {
678 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
679 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
680 if (pcm->mmap_buffer == MAP_FAILED) {
681 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
682 pcm_frames_to_bytes(pcm, pcm->buffer_size));
683 goto fail_close;
684 }
685 }
686
687
Simon Wilson79d39652011-05-25 13:44:23 -0700688 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700689 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700690 sparams.period_step = 1;
691 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700692
Eric Laurent93e7b672012-08-22 16:18:14 -0700693 if (!config->start_threshold) {
694 if (pcm->flags & PCM_IN)
695 pcm->config.start_threshold = sparams.start_threshold = 1;
696 else
697 pcm->config.start_threshold = sparams.start_threshold =
698 config->period_count * config->period_size / 2;
699 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700700 sparams.start_threshold = config->start_threshold;
701
Liam Girdwood6be28f12011-10-13 12:59:51 -0700702 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800703 if (!config->stop_threshold) {
704 if (pcm->flags & PCM_IN)
705 pcm->config.stop_threshold = sparams.stop_threshold =
706 config->period_count * config->period_size * 10;
707 else
708 pcm->config.stop_threshold = sparams.stop_threshold =
709 config->period_count * config->period_size;
710 }
John Grossman3bb114a2011-07-21 10:59:55 -0700711 else
712 sparams.stop_threshold = config->stop_threshold;
713
Simon Wilson79d39652011-05-25 13:44:23 -0700714 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
715 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700716 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700717 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700718
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600719 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700720 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700721
722 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
723 oops(pcm, errno, "cannot set sw params");
724 goto fail;
725 }
726
Eric Laurent40b018e2011-06-18 10:10:23 -0700727 rc = pcm_hw_mmap_status(pcm);
728 if (rc < 0) {
729 oops(pcm, rc, "mmap status failed");
730 goto fail;
731 }
732
Glenn Kasten81012402013-08-22 15:11:48 -0700733#ifdef SNDRV_PCM_IOCTL_TTSTAMP
734 if (pcm->flags & PCM_MONOTONIC) {
735 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
736 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
737 if (rc < 0) {
738 oops(pcm, rc, "cannot set timestamp type");
739 goto fail;
740 }
741 }
742#endif
743
Simon Wilson79d39652011-05-25 13:44:23 -0700744 pcm->underruns = 0;
745 return pcm;
746
747fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700748 if (flags & PCM_MMAP)
749 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
750fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700751 close(pcm->fd);
752 pcm->fd = -1;
753 return pcm;
754}
755
756int pcm_is_ready(struct pcm *pcm)
757{
758 return pcm->fd >= 0;
759}
Simon Wilsond6458e62011-06-21 14:58:11 -0700760
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530761int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700762{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530763 if (pcm->prepared)
764 return 0;
765
Simon Wilsond6458e62011-06-21 14:58:11 -0700766 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
767 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700768
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530769 pcm->prepared = 1;
770 return 0;
771}
772
773int pcm_start(struct pcm *pcm)
774{
775 int prepare_error = pcm_prepare(pcm);
776 if (prepare_error)
777 return prepare_error;
778
Liam Girdwood6be28f12011-10-13 12:59:51 -0700779 if (pcm->flags & PCM_MMAP)
780 pcm_sync_ptr(pcm, 0);
781
Simon Wilsond6458e62011-06-21 14:58:11 -0700782 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
783 return oops(pcm, errno, "cannot start channel");
784
Liam Girdwood6be28f12011-10-13 12:59:51 -0700785 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700786 return 0;
787}
788
789int pcm_stop(struct pcm *pcm)
790{
791 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
792 return oops(pcm, errno, "cannot stop channel");
793
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530794 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700795 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700796 return 0;
797}
798
Liam Girdwood6be28f12011-10-13 12:59:51 -0700799static inline int pcm_mmap_playback_avail(struct pcm *pcm)
800{
801 int avail;
802
803 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
804
805 if (avail < 0)
806 avail += pcm->boundary;
807 else if (avail > (int)pcm->boundary)
808 avail -= pcm->boundary;
809
810 return avail;
811}
812
813static inline int pcm_mmap_capture_avail(struct pcm *pcm)
814{
815 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
816 if (avail < 0)
817 avail += pcm->boundary;
818 return avail;
819}
820
821static inline int pcm_mmap_avail(struct pcm *pcm)
822{
823 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
824 if (pcm->flags & PCM_IN)
825 return pcm_mmap_capture_avail(pcm);
826 else
827 return pcm_mmap_playback_avail(pcm);
828}
829
830static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
831{
832 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
833 appl_ptr += frames;
834
835 /* check for boundary wrap */
836 if (appl_ptr > pcm->boundary)
837 appl_ptr -= pcm->boundary;
838 pcm->mmap_control->appl_ptr = appl_ptr;
839}
840
841int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
842 unsigned int *frames)
843{
844 unsigned int continuous, copy_frames, avail;
845
846 /* return the mmap buffer */
847 *areas = pcm->mmap_buffer;
848
849 /* and the application offset in frames */
850 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
851
852 avail = pcm_mmap_avail(pcm);
853 if (avail > pcm->buffer_size)
854 avail = pcm->buffer_size;
855 continuous = pcm->buffer_size - *offset;
856
857 /* we can only copy frames if the are availabale and continuos */
858 copy_frames = *frames;
859 if (copy_frames > avail)
860 copy_frames = avail;
861 if (copy_frames > continuous)
862 copy_frames = continuous;
863 *frames = copy_frames;
864
865 return 0;
866}
867
868int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
869{
870 /* update the application pointer in userspace and kernel */
871 pcm_mmap_appl_forward(pcm, frames);
872 pcm_sync_ptr(pcm, 0);
873
874 return frames;
875}
876
877int pcm_avail_update(struct pcm *pcm)
878{
879 pcm_sync_ptr(pcm, 0);
880 return pcm_mmap_avail(pcm);
881}
882
883int pcm_state(struct pcm *pcm)
884{
885 int err = pcm_sync_ptr(pcm, 0);
886 if (err < 0)
887 return err;
888
889 return pcm->mmap_status->state;
890}
891
892int pcm_wait(struct pcm *pcm, int timeout)
893{
894 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700895 int err;
896
897 pfd.fd = pcm->fd;
898 pfd.events = POLLOUT | POLLERR | POLLNVAL;
899
900 do {
901 /* let's wait for avail or timeout */
902 err = poll(&pfd, 1, timeout);
903 if (err < 0)
904 return -errno;
905
906 /* timeout ? */
907 if (err == 0)
908 return 0;
909
910 /* have we been interrupted ? */
911 if (errno == -EINTR)
912 continue;
913
914 /* check for any errors */
915 if (pfd.revents & (POLLERR | POLLNVAL)) {
916 switch (pcm_state(pcm)) {
917 case PCM_STATE_XRUN:
918 return -EPIPE;
919 case PCM_STATE_SUSPENDED:
920 return -ESTRPIPE;
921 case PCM_STATE_DISCONNECTED:
922 return -ENODEV;
923 default:
924 return -EIO;
925 }
926 }
927 /* poll again if fd not ready for IO */
928 } while (!(pfd.revents & (POLLIN | POLLOUT)));
929
930 return 1;
931}
932
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700933int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700934{
935 int err = 0, frames, avail;
936 unsigned int offset = 0, count;
937
938 if (bytes == 0)
939 return 0;
940
941 count = pcm_bytes_to_frames(pcm, bytes);
942
943 while (count > 0) {
944
945 /* get the available space for writing new frames */
946 avail = pcm_avail_update(pcm);
947 if (avail < 0) {
948 fprintf(stderr, "cannot determine available mmap frames");
949 return err;
950 }
951
952 /* start the audio if we reach the threshold */
953 if (!pcm->running &&
954 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
955 if (pcm_start(pcm) < 0) {
956 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
957 (unsigned int)pcm->mmap_status->hw_ptr,
958 (unsigned int)pcm->mmap_control->appl_ptr,
959 avail);
960 return -errno;
961 }
962 }
963
964 /* sleep until we have space to write new frames */
965 if (pcm->running &&
966 (unsigned int)avail < pcm->mmap_control->avail_min) {
967 int time = -1;
968
969 if (pcm->flags & PCM_NOIRQ)
970 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
971 / pcm->noirq_frames_per_msec;
972
973 err = pcm_wait(pcm, time);
974 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530975 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700976 pcm->running = 0;
977 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
978 (unsigned int)pcm->mmap_status->hw_ptr,
979 (unsigned int)pcm->mmap_control->appl_ptr,
980 avail);
981 pcm->mmap_control->appl_ptr = 0;
982 return err;
983 }
984 continue;
985 }
986
987 frames = count;
988 if (frames > avail)
989 frames = avail;
990
991 if (!frames)
992 break;
993
994 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700995 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700996 if (frames < 0) {
997 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
998 (unsigned int)pcm->mmap_status->hw_ptr,
999 (unsigned int)pcm->mmap_control->appl_ptr,
1000 avail);
1001 return frames;
1002 }
1003
1004 offset += frames;
1005 count -= frames;
1006 }
1007
Liam Girdwood6be28f12011-10-13 12:59:51 -07001008 return 0;
1009}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001010
1011int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1012{
1013 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1014 return -ENOSYS;
1015
1016 return pcm_mmap_transfer(pcm, (void *)data, count);
1017}
1018
1019int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1020{
1021 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1022 return -ENOSYS;
1023
1024 return pcm_mmap_transfer(pcm, data, count);
1025}