blob: 70d1d2f79429def756dd8f42965afa05fb8df7e2 [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;
162 int underruns;
163 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700164 unsigned int boundary;
Simon Wilson79d39652011-05-25 13:44:23 -0700165 char error[PCM_ERROR_MAX];
166 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700167 struct snd_pcm_mmap_status *mmap_status;
168 struct snd_pcm_mmap_control *mmap_control;
169 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700170 void *mmap_buffer;
171 unsigned int noirq_frames_per_msec;
Simon Wilson79d39652011-05-25 13:44:23 -0700172};
173
Simon Wilson851aa5c2011-05-30 21:18:26 -0700174unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700175{
176 return pcm->buffer_size;
177}
178
179const char* pcm_get_error(struct pcm *pcm)
180{
181 return pcm->error;
182}
183
184static int oops(struct pcm *pcm, int e, const char *fmt, ...)
185{
186 va_list ap;
187 int sz;
188
189 va_start(ap, fmt);
190 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
191 va_end(ap);
192 sz = strlen(pcm->error);
193
194 if (errno)
195 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
196 ": %s", strerror(e));
197 return -1;
198}
199
Simon Wilsonbc03b622011-06-15 17:19:01 -0700200static unsigned int pcm_format_to_alsa(enum pcm_format format)
201{
202 switch (format) {
203 case PCM_FORMAT_S32_LE:
204 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500205 case PCM_FORMAT_S8:
206 return SNDRV_PCM_FORMAT_S8;
207 case PCM_FORMAT_S24_LE:
208 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700209 default:
210 case PCM_FORMAT_S16_LE:
211 return SNDRV_PCM_FORMAT_S16_LE;
212 };
213}
214
Simon Wilson7136cf72013-07-17 10:30:35 -0700215unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700216{
217 switch (format) {
218 case PCM_FORMAT_S32_LE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700219 case PCM_FORMAT_S24_LE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700220 return 32;
221 default:
222 case PCM_FORMAT_S16_LE:
223 return 16;
224 };
225}
226
Liam Girdwood6be28f12011-10-13 12:59:51 -0700227unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
228{
229 return bytes / (pcm->config.channels *
230 (pcm_format_to_bits(pcm->config.format) >> 3));
231}
232
233unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
234{
235 return frames * pcm->config.channels *
236 (pcm_format_to_bits(pcm->config.format) >> 3);
237}
238
Eric Laurent40b018e2011-06-18 10:10:23 -0700239static int pcm_sync_ptr(struct pcm *pcm, int flags) {
240 if (pcm->sync_ptr) {
241 pcm->sync_ptr->flags = flags;
242 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
243 return -1;
244 }
245 return 0;
246}
247
248static int pcm_hw_mmap_status(struct pcm *pcm) {
249
250 if (pcm->sync_ptr)
251 return 0;
252
253 int page_size = sysconf(_SC_PAGE_SIZE);
254 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
255 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
256 if (pcm->mmap_status == MAP_FAILED)
257 pcm->mmap_status = NULL;
258 if (!pcm->mmap_status)
259 goto mmap_error;
260
261 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
262 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
263 if (pcm->mmap_control == MAP_FAILED)
264 pcm->mmap_control = NULL;
265 if (!pcm->mmap_control) {
266 munmap(pcm->mmap_status, page_size);
267 pcm->mmap_status = NULL;
268 goto mmap_error;
269 }
270 pcm->mmap_control->avail_min = 1;
271
272 return 0;
273
274mmap_error:
275
276 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
277 if (!pcm->sync_ptr)
278 return -ENOMEM;
279 pcm->mmap_status = &pcm->sync_ptr->s.status;
280 pcm->mmap_control = &pcm->sync_ptr->c.control;
281 pcm->mmap_control->avail_min = 1;
282 pcm_sync_ptr(pcm, 0);
283
284 return 0;
285}
286
287static void pcm_hw_munmap_status(struct pcm *pcm) {
288 if (pcm->sync_ptr) {
289 free(pcm->sync_ptr);
290 pcm->sync_ptr = NULL;
291 } else {
292 int page_size = sysconf(_SC_PAGE_SIZE);
293 if (pcm->mmap_status)
294 munmap(pcm->mmap_status, page_size);
295 if (pcm->mmap_control)
296 munmap(pcm->mmap_control, page_size);
297 }
298 pcm->mmap_status = NULL;
299 pcm->mmap_control = NULL;
300}
301
Liam Girdwood6be28f12011-10-13 12:59:51 -0700302static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700303 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700304 unsigned int frames)
305{
306 int size_bytes = pcm_frames_to_bytes(pcm, frames);
307 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
308 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
309
310 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700311 if (pcm->flags & PCM_IN)
312 memcpy(buf + src_offset_bytes,
313 (char*)pcm->mmap_buffer + pcm_offset_bytes,
314 size_bytes);
315 else
316 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
317 buf + src_offset_bytes,
318 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700319 return 0;
320}
321
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700322static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700323 unsigned int offset, unsigned int size)
324{
325 void *pcm_areas;
326 int commit;
327 unsigned int pcm_offset, frames, count = 0;
328
329 while (size > 0) {
330 frames = size;
331 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700332 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700333 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
334 if (commit < 0) {
335 oops(pcm, commit, "failed to commit %d frames\n", frames);
336 return commit;
337 }
338
339 offset += commit;
340 count += commit;
341 size -= commit;
342 }
343 return count;
344}
345
Eric Laurent40b018e2011-06-18 10:10:23 -0700346int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
347 struct timespec *tstamp)
348{
349 int frames;
350 int rc;
351 snd_pcm_uframes_t hw_ptr;
352
353 if (!pcm_is_ready(pcm))
354 return -1;
355
356 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
357 if (rc < 0)
358 return -1;
359
Eric Laurent7db48582011-11-17 11:47:59 -0800360 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
361 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800362 return -1;
363
Eric Laurent40b018e2011-06-18 10:10:23 -0700364 *tstamp = pcm->mmap_status->tstamp;
365 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
366 return -1;
367
368 hw_ptr = pcm->mmap_status->hw_ptr;
369 if (pcm->flags & PCM_IN)
370 frames = hw_ptr - pcm->mmap_control->appl_ptr;
371 else
372 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
373
374 if (frames < 0)
375 return -1;
376
377 *avail = (unsigned int)frames;
378
379 return 0;
380}
381
Mark Brown6bbe77a2012-02-10 22:07:09 +0000382int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700383{
384 struct snd_xferi x;
385
386 if (pcm->flags & PCM_IN)
387 return -EINVAL;
388
Mark Brown6bbe77a2012-02-10 22:07:09 +0000389 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700390 x.frames = count / (pcm->config.channels *
391 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700392
393 for (;;) {
394 if (!pcm->running) {
395 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
396 return oops(pcm, errno, "cannot prepare channel");
397 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
398 return oops(pcm, errno, "cannot write initial data");
399 pcm->running = 1;
400 return 0;
401 }
402 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
403 pcm->running = 0;
404 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700405 /* we failed to make our window -- try to restart if we are
406 * allowed to do so. Otherwise, simply allow the EPIPE error to
407 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700408 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700409 if (pcm->flags & PCM_NORESTART)
410 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700411 continue;
412 }
413 return oops(pcm, errno, "cannot write stream data");
414 }
415 return 0;
416 }
417}
418
419int pcm_read(struct pcm *pcm, void *data, unsigned int count)
420{
421 struct snd_xferi x;
422
423 if (!(pcm->flags & PCM_IN))
424 return -EINVAL;
425
426 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700427 x.frames = count / (pcm->config.channels *
428 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700429
430 for (;;) {
431 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700432 if (pcm_start(pcm) < 0) {
433 fprintf(stderr, "start error");
434 return -errno;
435 }
Simon Wilson79d39652011-05-25 13:44:23 -0700436 }
437 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
438 pcm->running = 0;
439 if (errno == EPIPE) {
440 /* we failed to make our window -- try to restart */
441 pcm->underruns++;
442 continue;
443 }
444 return oops(pcm, errno, "cannot read stream data");
445 }
446 return 0;
447 }
448}
449
450static struct pcm bad_pcm = {
451 .fd = -1,
452};
453
Simon Wilson43544882012-10-31 12:52:39 -0700454struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
455 unsigned int flags)
456{
457 struct snd_pcm_hw_params *params;
458 char fn[256];
459 int fd;
460
461 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
462 flags & PCM_IN ? 'c' : 'p');
463
464 fd = open(fn, O_RDWR);
465 if (fd < 0) {
466 fprintf(stderr, "cannot open device '%s'\n", fn);
467 goto err_open;
468 }
469
470 params = calloc(1, sizeof(struct snd_pcm_hw_params));
471 if (!params)
472 goto err_calloc;
473
474 param_init(params);
475 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
476 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
477 goto err_hw_refine;
478 }
479
480 close(fd);
481
482 return (struct pcm_params *)params;
483
484err_hw_refine:
485 free(params);
486err_calloc:
487 close(fd);
488err_open:
489 return NULL;
490}
491
492void pcm_params_free(struct pcm_params *pcm_params)
493{
494 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
495
496 if (params)
497 free(params);
498}
499
500static int pcm_param_to_alsa(enum pcm_param param)
501{
502 switch (param) {
503 case PCM_PARAM_SAMPLE_BITS:
504 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
505 break;
506 case PCM_PARAM_FRAME_BITS:
507 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
508 break;
509 case PCM_PARAM_CHANNELS:
510 return SNDRV_PCM_HW_PARAM_CHANNELS;
511 break;
512 case PCM_PARAM_RATE:
513 return SNDRV_PCM_HW_PARAM_RATE;
514 break;
515 case PCM_PARAM_PERIOD_TIME:
516 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
517 break;
518 case PCM_PARAM_PERIOD_SIZE:
519 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
520 break;
521 case PCM_PARAM_PERIOD_BYTES:
522 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
523 break;
524 case PCM_PARAM_PERIODS:
525 return SNDRV_PCM_HW_PARAM_PERIODS;
526 break;
527 case PCM_PARAM_BUFFER_TIME:
528 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
529 break;
530 case PCM_PARAM_BUFFER_SIZE:
531 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
532 break;
533 case PCM_PARAM_BUFFER_BYTES:
534 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
535 break;
536 case PCM_PARAM_TICK_TIME:
537 return SNDRV_PCM_HW_PARAM_TICK_TIME;
538 break;
539
540 default:
541 return -1;
542 }
543}
544
545unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
546 enum pcm_param param)
547{
548 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
549 int p;
550
551 if (!params)
552 return 0;
553
554 p = pcm_param_to_alsa(param);
555 if (p < 0)
556 return 0;
557
558 return param_get_min(params, p);
559}
560
561unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
562 enum pcm_param param)
563{
564 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
565 int p;
566
567 if (!params)
568 return 0;
569
570 p = pcm_param_to_alsa(param);
571 if (p < 0)
572 return 0;
573
574 return param_get_max(params, p);
575}
576
Simon Wilson79d39652011-05-25 13:44:23 -0700577int pcm_close(struct pcm *pcm)
578{
579 if (pcm == &bad_pcm)
580 return 0;
581
Eric Laurent40b018e2011-06-18 10:10:23 -0700582 pcm_hw_munmap_status(pcm);
583
Liam Girdwood6be28f12011-10-13 12:59:51 -0700584 if (pcm->flags & PCM_MMAP) {
585 pcm_stop(pcm);
586 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
587 }
588
Simon Wilson79d39652011-05-25 13:44:23 -0700589 if (pcm->fd >= 0)
590 close(pcm->fd);
591 pcm->running = 0;
592 pcm->buffer_size = 0;
593 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700594 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700595 return 0;
596}
597
Simon Wilson1bd580f2011-06-02 15:58:41 -0700598struct pcm *pcm_open(unsigned int card, unsigned int device,
599 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700600{
Simon Wilson79d39652011-05-25 13:44:23 -0700601 struct pcm *pcm;
602 struct snd_pcm_info info;
603 struct snd_pcm_hw_params params;
604 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700605 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700606 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700607
608 pcm = calloc(1, sizeof(struct pcm));
609 if (!pcm || !config)
610 return &bad_pcm; /* TODO: could support default config here */
611
612 pcm->config = *config;
613
Simon Wilson1bd580f2011-06-02 15:58:41 -0700614 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
615 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700616
617 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700618 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700619 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700620 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700621 return pcm;
622 }
623
624 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700625 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700626 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700627 }
628
629 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700630 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
631 pcm_format_to_alsa(config->format));
632 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
633 SNDRV_PCM_SUBFORMAT_STD);
634 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
635 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
636 pcm_format_to_bits(config->format));
637 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
638 pcm_format_to_bits(config->format) * config->channels);
639 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
640 config->channels);
641 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
642 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
643
Liam Girdwood6be28f12011-10-13 12:59:51 -0700644 if (flags & PCM_NOIRQ) {
645
646 if (!(flags & PCM_MMAP)) {
647 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
648 goto fail;
649 }
650
651 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
652 pcm->noirq_frames_per_msec = config->rate / 1000;
653 }
654
655 if (flags & PCM_MMAP)
656 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
657 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
658 else
659 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
660 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
661
Simon Wilson79d39652011-05-25 13:44:23 -0700662 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
663 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700664 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700665 }
666
Liam Girdwood6be28f12011-10-13 12:59:51 -0700667 /* get our refined hw_params */
668 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
669 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
670 pcm->buffer_size = config->period_count * config->period_size;
671
672 if (flags & PCM_MMAP) {
673 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
674 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
675 if (pcm->mmap_buffer == MAP_FAILED) {
676 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
677 pcm_frames_to_bytes(pcm, pcm->buffer_size));
678 goto fail_close;
679 }
680 }
681
682
Simon Wilson79d39652011-05-25 13:44:23 -0700683 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700684 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700685 sparams.period_step = 1;
686 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700687
Eric Laurent93e7b672012-08-22 16:18:14 -0700688 if (!config->start_threshold) {
689 if (pcm->flags & PCM_IN)
690 pcm->config.start_threshold = sparams.start_threshold = 1;
691 else
692 pcm->config.start_threshold = sparams.start_threshold =
693 config->period_count * config->period_size / 2;
694 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700695 sparams.start_threshold = config->start_threshold;
696
Liam Girdwood6be28f12011-10-13 12:59:51 -0700697 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800698 if (!config->stop_threshold) {
699 if (pcm->flags & PCM_IN)
700 pcm->config.stop_threshold = sparams.stop_threshold =
701 config->period_count * config->period_size * 10;
702 else
703 pcm->config.stop_threshold = sparams.stop_threshold =
704 config->period_count * config->period_size;
705 }
John Grossman3bb114a2011-07-21 10:59:55 -0700706 else
707 sparams.stop_threshold = config->stop_threshold;
708
Simon Wilson79d39652011-05-25 13:44:23 -0700709 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
710 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700711 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700712 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700713
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600714 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700715 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700716
717 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
718 oops(pcm, errno, "cannot set sw params");
719 goto fail;
720 }
721
Eric Laurent40b018e2011-06-18 10:10:23 -0700722 rc = pcm_hw_mmap_status(pcm);
723 if (rc < 0) {
724 oops(pcm, rc, "mmap status failed");
725 goto fail;
726 }
727
Simon Wilson79d39652011-05-25 13:44:23 -0700728 pcm->underruns = 0;
729 return pcm;
730
731fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700732 if (flags & PCM_MMAP)
733 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
734fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700735 close(pcm->fd);
736 pcm->fd = -1;
737 return pcm;
738}
739
740int pcm_is_ready(struct pcm *pcm)
741{
742 return pcm->fd >= 0;
743}
Simon Wilsond6458e62011-06-21 14:58:11 -0700744
745int pcm_start(struct pcm *pcm)
746{
747 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
748 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700749
750 if (pcm->flags & PCM_MMAP)
751 pcm_sync_ptr(pcm, 0);
752
Simon Wilsond6458e62011-06-21 14:58:11 -0700753 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
754 return oops(pcm, errno, "cannot start channel");
755
Liam Girdwood6be28f12011-10-13 12:59:51 -0700756 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700757 return 0;
758}
759
760int pcm_stop(struct pcm *pcm)
761{
762 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
763 return oops(pcm, errno, "cannot stop channel");
764
Liam Girdwood6be28f12011-10-13 12:59:51 -0700765 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700766 return 0;
767}
768
Liam Girdwood6be28f12011-10-13 12:59:51 -0700769static inline int pcm_mmap_playback_avail(struct pcm *pcm)
770{
771 int avail;
772
773 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
774
775 if (avail < 0)
776 avail += pcm->boundary;
777 else if (avail > (int)pcm->boundary)
778 avail -= pcm->boundary;
779
780 return avail;
781}
782
783static inline int pcm_mmap_capture_avail(struct pcm *pcm)
784{
785 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
786 if (avail < 0)
787 avail += pcm->boundary;
788 return avail;
789}
790
791static inline int pcm_mmap_avail(struct pcm *pcm)
792{
793 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
794 if (pcm->flags & PCM_IN)
795 return pcm_mmap_capture_avail(pcm);
796 else
797 return pcm_mmap_playback_avail(pcm);
798}
799
800static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
801{
802 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
803 appl_ptr += frames;
804
805 /* check for boundary wrap */
806 if (appl_ptr > pcm->boundary)
807 appl_ptr -= pcm->boundary;
808 pcm->mmap_control->appl_ptr = appl_ptr;
809}
810
811int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
812 unsigned int *frames)
813{
814 unsigned int continuous, copy_frames, avail;
815
816 /* return the mmap buffer */
817 *areas = pcm->mmap_buffer;
818
819 /* and the application offset in frames */
820 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
821
822 avail = pcm_mmap_avail(pcm);
823 if (avail > pcm->buffer_size)
824 avail = pcm->buffer_size;
825 continuous = pcm->buffer_size - *offset;
826
827 /* we can only copy frames if the are availabale and continuos */
828 copy_frames = *frames;
829 if (copy_frames > avail)
830 copy_frames = avail;
831 if (copy_frames > continuous)
832 copy_frames = continuous;
833 *frames = copy_frames;
834
835 return 0;
836}
837
838int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
839{
840 /* update the application pointer in userspace and kernel */
841 pcm_mmap_appl_forward(pcm, frames);
842 pcm_sync_ptr(pcm, 0);
843
844 return frames;
845}
846
847int pcm_avail_update(struct pcm *pcm)
848{
849 pcm_sync_ptr(pcm, 0);
850 return pcm_mmap_avail(pcm);
851}
852
853int pcm_state(struct pcm *pcm)
854{
855 int err = pcm_sync_ptr(pcm, 0);
856 if (err < 0)
857 return err;
858
859 return pcm->mmap_status->state;
860}
861
862int pcm_wait(struct pcm *pcm, int timeout)
863{
864 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700865 int err;
866
867 pfd.fd = pcm->fd;
868 pfd.events = POLLOUT | POLLERR | POLLNVAL;
869
870 do {
871 /* let's wait for avail or timeout */
872 err = poll(&pfd, 1, timeout);
873 if (err < 0)
874 return -errno;
875
876 /* timeout ? */
877 if (err == 0)
878 return 0;
879
880 /* have we been interrupted ? */
881 if (errno == -EINTR)
882 continue;
883
884 /* check for any errors */
885 if (pfd.revents & (POLLERR | POLLNVAL)) {
886 switch (pcm_state(pcm)) {
887 case PCM_STATE_XRUN:
888 return -EPIPE;
889 case PCM_STATE_SUSPENDED:
890 return -ESTRPIPE;
891 case PCM_STATE_DISCONNECTED:
892 return -ENODEV;
893 default:
894 return -EIO;
895 }
896 }
897 /* poll again if fd not ready for IO */
898 } while (!(pfd.revents & (POLLIN | POLLOUT)));
899
900 return 1;
901}
902
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700903int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700904{
905 int err = 0, frames, avail;
906 unsigned int offset = 0, count;
907
908 if (bytes == 0)
909 return 0;
910
911 count = pcm_bytes_to_frames(pcm, bytes);
912
913 while (count > 0) {
914
915 /* get the available space for writing new frames */
916 avail = pcm_avail_update(pcm);
917 if (avail < 0) {
918 fprintf(stderr, "cannot determine available mmap frames");
919 return err;
920 }
921
922 /* start the audio if we reach the threshold */
923 if (!pcm->running &&
924 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
925 if (pcm_start(pcm) < 0) {
926 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
927 (unsigned int)pcm->mmap_status->hw_ptr,
928 (unsigned int)pcm->mmap_control->appl_ptr,
929 avail);
930 return -errno;
931 }
932 }
933
934 /* sleep until we have space to write new frames */
935 if (pcm->running &&
936 (unsigned int)avail < pcm->mmap_control->avail_min) {
937 int time = -1;
938
939 if (pcm->flags & PCM_NOIRQ)
940 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
941 / pcm->noirq_frames_per_msec;
942
943 err = pcm_wait(pcm, time);
944 if (err < 0) {
945 pcm->running = 0;
946 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
947 (unsigned int)pcm->mmap_status->hw_ptr,
948 (unsigned int)pcm->mmap_control->appl_ptr,
949 avail);
950 pcm->mmap_control->appl_ptr = 0;
951 return err;
952 }
953 continue;
954 }
955
956 frames = count;
957 if (frames > avail)
958 frames = avail;
959
960 if (!frames)
961 break;
962
963 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700964 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700965 if (frames < 0) {
966 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
967 (unsigned int)pcm->mmap_status->hw_ptr,
968 (unsigned int)pcm->mmap_control->appl_ptr,
969 avail);
970 return frames;
971 }
972
973 offset += frames;
974 count -= frames;
975 }
976
Liam Girdwood6be28f12011-10-13 12:59:51 -0700977 return 0;
978}
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700979
980int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
981{
982 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
983 return -ENOSYS;
984
985 return pcm_mmap_transfer(pcm, (void *)data, count);
986}
987
988int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
989{
990 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
991 return -ENOSYS;
992
993 return pcm_mmap_transfer(pcm, data, count);
994}