blob: fd0242ae920c3545e21a61f025c7317494143005 [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
215static unsigned int pcm_format_to_bits(enum pcm_format format)
216{
217 switch (format) {
218 case PCM_FORMAT_S32_LE:
219 return 32;
220 default:
221 case PCM_FORMAT_S16_LE:
222 return 16;
223 };
224}
225
Liam Girdwood6be28f12011-10-13 12:59:51 -0700226unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
227{
228 return bytes / (pcm->config.channels *
229 (pcm_format_to_bits(pcm->config.format) >> 3));
230}
231
232unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
233{
234 return frames * pcm->config.channels *
235 (pcm_format_to_bits(pcm->config.format) >> 3);
236}
237
Eric Laurent40b018e2011-06-18 10:10:23 -0700238static int pcm_sync_ptr(struct pcm *pcm, int flags) {
239 if (pcm->sync_ptr) {
240 pcm->sync_ptr->flags = flags;
241 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
242 return -1;
243 }
244 return 0;
245}
246
247static int pcm_hw_mmap_status(struct pcm *pcm) {
248
249 if (pcm->sync_ptr)
250 return 0;
251
252 int page_size = sysconf(_SC_PAGE_SIZE);
253 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
254 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
255 if (pcm->mmap_status == MAP_FAILED)
256 pcm->mmap_status = NULL;
257 if (!pcm->mmap_status)
258 goto mmap_error;
259
260 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
261 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
262 if (pcm->mmap_control == MAP_FAILED)
263 pcm->mmap_control = NULL;
264 if (!pcm->mmap_control) {
265 munmap(pcm->mmap_status, page_size);
266 pcm->mmap_status = NULL;
267 goto mmap_error;
268 }
269 pcm->mmap_control->avail_min = 1;
270
271 return 0;
272
273mmap_error:
274
275 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
276 if (!pcm->sync_ptr)
277 return -ENOMEM;
278 pcm->mmap_status = &pcm->sync_ptr->s.status;
279 pcm->mmap_control = &pcm->sync_ptr->c.control;
280 pcm->mmap_control->avail_min = 1;
281 pcm_sync_ptr(pcm, 0);
282
283 return 0;
284}
285
286static void pcm_hw_munmap_status(struct pcm *pcm) {
287 if (pcm->sync_ptr) {
288 free(pcm->sync_ptr);
289 pcm->sync_ptr = NULL;
290 } else {
291 int page_size = sysconf(_SC_PAGE_SIZE);
292 if (pcm->mmap_status)
293 munmap(pcm->mmap_status, page_size);
294 if (pcm->mmap_control)
295 munmap(pcm->mmap_control, page_size);
296 }
297 pcm->mmap_status = NULL;
298 pcm->mmap_control = NULL;
299}
300
Liam Girdwood6be28f12011-10-13 12:59:51 -0700301static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
302 const char *src, unsigned int src_offset,
303 unsigned int frames)
304{
305 int size_bytes = pcm_frames_to_bytes(pcm, frames);
306 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
307 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
308
309 /* interleaved only atm */
310 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
311 src + src_offset_bytes, size_bytes);
312 return 0;
313}
314
Mark Brown6bbe77a2012-02-10 22:07:09 +0000315static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700316 unsigned int offset, unsigned int size)
317{
318 void *pcm_areas;
319 int commit;
320 unsigned int pcm_offset, frames, count = 0;
321
322 while (size > 0) {
323 frames = size;
324 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
325 pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
326 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
327 if (commit < 0) {
328 oops(pcm, commit, "failed to commit %d frames\n", frames);
329 return commit;
330 }
331
332 offset += commit;
333 count += commit;
334 size -= commit;
335 }
336 return count;
337}
338
Eric Laurent40b018e2011-06-18 10:10:23 -0700339int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
340 struct timespec *tstamp)
341{
342 int frames;
343 int rc;
344 snd_pcm_uframes_t hw_ptr;
345
346 if (!pcm_is_ready(pcm))
347 return -1;
348
349 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
350 if (rc < 0)
351 return -1;
352
Eric Laurent7db48582011-11-17 11:47:59 -0800353 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
354 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800355 return -1;
356
Eric Laurent40b018e2011-06-18 10:10:23 -0700357 *tstamp = pcm->mmap_status->tstamp;
358 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
359 return -1;
360
361 hw_ptr = pcm->mmap_status->hw_ptr;
362 if (pcm->flags & PCM_IN)
363 frames = hw_ptr - pcm->mmap_control->appl_ptr;
364 else
365 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
366
367 if (frames < 0)
368 return -1;
369
370 *avail = (unsigned int)frames;
371
372 return 0;
373}
374
Mark Brown6bbe77a2012-02-10 22:07:09 +0000375int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700376{
377 struct snd_xferi x;
378
379 if (pcm->flags & PCM_IN)
380 return -EINVAL;
381
Mark Brown6bbe77a2012-02-10 22:07:09 +0000382 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700383 x.frames = count / (pcm->config.channels *
384 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700385
386 for (;;) {
387 if (!pcm->running) {
388 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
389 return oops(pcm, errno, "cannot prepare channel");
390 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
391 return oops(pcm, errno, "cannot write initial data");
392 pcm->running = 1;
393 return 0;
394 }
395 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
396 pcm->running = 0;
397 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700398 /* we failed to make our window -- try to restart if we are
399 * allowed to do so. Otherwise, simply allow the EPIPE error to
400 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700401 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700402 if (pcm->flags & PCM_NORESTART)
403 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700404 continue;
405 }
406 return oops(pcm, errno, "cannot write stream data");
407 }
408 return 0;
409 }
410}
411
412int pcm_read(struct pcm *pcm, void *data, unsigned int count)
413{
414 struct snd_xferi x;
415
416 if (!(pcm->flags & PCM_IN))
417 return -EINVAL;
418
419 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700420 x.frames = count / (pcm->config.channels *
421 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700422
423 for (;;) {
424 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700425 if (pcm_start(pcm) < 0) {
426 fprintf(stderr, "start error");
427 return -errno;
428 }
Simon Wilson79d39652011-05-25 13:44:23 -0700429 }
430 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
431 pcm->running = 0;
432 if (errno == EPIPE) {
433 /* we failed to make our window -- try to restart */
434 pcm->underruns++;
435 continue;
436 }
437 return oops(pcm, errno, "cannot read stream data");
438 }
439 return 0;
440 }
441}
442
443static struct pcm bad_pcm = {
444 .fd = -1,
445};
446
Simon Wilson43544882012-10-31 12:52:39 -0700447struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
448 unsigned int flags)
449{
450 struct snd_pcm_hw_params *params;
451 char fn[256];
452 int fd;
453
454 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
455 flags & PCM_IN ? 'c' : 'p');
456
457 fd = open(fn, O_RDWR);
458 if (fd < 0) {
459 fprintf(stderr, "cannot open device '%s'\n", fn);
460 goto err_open;
461 }
462
463 params = calloc(1, sizeof(struct snd_pcm_hw_params));
464 if (!params)
465 goto err_calloc;
466
467 param_init(params);
468 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
469 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
470 goto err_hw_refine;
471 }
472
473 close(fd);
474
475 return (struct pcm_params *)params;
476
477err_hw_refine:
478 free(params);
479err_calloc:
480 close(fd);
481err_open:
482 return NULL;
483}
484
485void pcm_params_free(struct pcm_params *pcm_params)
486{
487 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
488
489 if (params)
490 free(params);
491}
492
493static int pcm_param_to_alsa(enum pcm_param param)
494{
495 switch (param) {
496 case PCM_PARAM_SAMPLE_BITS:
497 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
498 break;
499 case PCM_PARAM_FRAME_BITS:
500 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
501 break;
502 case PCM_PARAM_CHANNELS:
503 return SNDRV_PCM_HW_PARAM_CHANNELS;
504 break;
505 case PCM_PARAM_RATE:
506 return SNDRV_PCM_HW_PARAM_RATE;
507 break;
508 case PCM_PARAM_PERIOD_TIME:
509 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
510 break;
511 case PCM_PARAM_PERIOD_SIZE:
512 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
513 break;
514 case PCM_PARAM_PERIOD_BYTES:
515 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
516 break;
517 case PCM_PARAM_PERIODS:
518 return SNDRV_PCM_HW_PARAM_PERIODS;
519 break;
520 case PCM_PARAM_BUFFER_TIME:
521 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
522 break;
523 case PCM_PARAM_BUFFER_SIZE:
524 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
525 break;
526 case PCM_PARAM_BUFFER_BYTES:
527 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
528 break;
529 case PCM_PARAM_TICK_TIME:
530 return SNDRV_PCM_HW_PARAM_TICK_TIME;
531 break;
532
533 default:
534 return -1;
535 }
536}
537
538unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
539 enum pcm_param param)
540{
541 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
542 int p;
543
544 if (!params)
545 return 0;
546
547 p = pcm_param_to_alsa(param);
548 if (p < 0)
549 return 0;
550
551 return param_get_min(params, p);
552}
553
554unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
555 enum pcm_param param)
556{
557 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
558 int p;
559
560 if (!params)
561 return 0;
562
563 p = pcm_param_to_alsa(param);
564 if (p < 0)
565 return 0;
566
567 return param_get_max(params, p);
568}
569
Simon Wilson79d39652011-05-25 13:44:23 -0700570int pcm_close(struct pcm *pcm)
571{
572 if (pcm == &bad_pcm)
573 return 0;
574
Eric Laurent40b018e2011-06-18 10:10:23 -0700575 pcm_hw_munmap_status(pcm);
576
Liam Girdwood6be28f12011-10-13 12:59:51 -0700577 if (pcm->flags & PCM_MMAP) {
578 pcm_stop(pcm);
579 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
580 }
581
Simon Wilson79d39652011-05-25 13:44:23 -0700582 if (pcm->fd >= 0)
583 close(pcm->fd);
584 pcm->running = 0;
585 pcm->buffer_size = 0;
586 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700587 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700588 return 0;
589}
590
Simon Wilson1bd580f2011-06-02 15:58:41 -0700591struct pcm *pcm_open(unsigned int card, unsigned int device,
592 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700593{
Simon Wilson79d39652011-05-25 13:44:23 -0700594 struct pcm *pcm;
595 struct snd_pcm_info info;
596 struct snd_pcm_hw_params params;
597 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700598 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700599 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700600
601 pcm = calloc(1, sizeof(struct pcm));
602 if (!pcm || !config)
603 return &bad_pcm; /* TODO: could support default config here */
604
605 pcm->config = *config;
606
Simon Wilson1bd580f2011-06-02 15:58:41 -0700607 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
608 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700609
610 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700611 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700612 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700613 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700614 return pcm;
615 }
616
617 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700618 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700619 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700620 }
621
622 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700623 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
624 pcm_format_to_alsa(config->format));
625 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
626 SNDRV_PCM_SUBFORMAT_STD);
627 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
628 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
629 pcm_format_to_bits(config->format));
630 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
631 pcm_format_to_bits(config->format) * config->channels);
632 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
633 config->channels);
634 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
635 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
636
Liam Girdwood6be28f12011-10-13 12:59:51 -0700637 if (flags & PCM_NOIRQ) {
638
639 if (!(flags & PCM_MMAP)) {
640 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
641 goto fail;
642 }
643
644 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
645 pcm->noirq_frames_per_msec = config->rate / 1000;
646 }
647
648 if (flags & PCM_MMAP)
649 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
650 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
651 else
652 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
653 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
654
Simon Wilson79d39652011-05-25 13:44:23 -0700655 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
656 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700657 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700658 }
659
Liam Girdwood6be28f12011-10-13 12:59:51 -0700660 /* get our refined hw_params */
661 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
662 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
663 pcm->buffer_size = config->period_count * config->period_size;
664
665 if (flags & PCM_MMAP) {
666 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
667 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
668 if (pcm->mmap_buffer == MAP_FAILED) {
669 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
670 pcm_frames_to_bytes(pcm, pcm->buffer_size));
671 goto fail_close;
672 }
673 }
674
675
Simon Wilson79d39652011-05-25 13:44:23 -0700676 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700677 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700678 sparams.period_step = 1;
679 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700680
Eric Laurent93e7b672012-08-22 16:18:14 -0700681 if (!config->start_threshold) {
682 if (pcm->flags & PCM_IN)
683 pcm->config.start_threshold = sparams.start_threshold = 1;
684 else
685 pcm->config.start_threshold = sparams.start_threshold =
686 config->period_count * config->period_size / 2;
687 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700688 sparams.start_threshold = config->start_threshold;
689
Liam Girdwood6be28f12011-10-13 12:59:51 -0700690 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800691 if (!config->stop_threshold) {
692 if (pcm->flags & PCM_IN)
693 pcm->config.stop_threshold = sparams.stop_threshold =
694 config->period_count * config->period_size * 10;
695 else
696 pcm->config.stop_threshold = sparams.stop_threshold =
697 config->period_count * config->period_size;
698 }
John Grossman3bb114a2011-07-21 10:59:55 -0700699 else
700 sparams.stop_threshold = config->stop_threshold;
701
Simon Wilson79d39652011-05-25 13:44:23 -0700702 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
703 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700704 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700705 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700706
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600707 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700708 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700709
710 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
711 oops(pcm, errno, "cannot set sw params");
712 goto fail;
713 }
714
Eric Laurent40b018e2011-06-18 10:10:23 -0700715 rc = pcm_hw_mmap_status(pcm);
716 if (rc < 0) {
717 oops(pcm, rc, "mmap status failed");
718 goto fail;
719 }
720
Simon Wilson79d39652011-05-25 13:44:23 -0700721 pcm->underruns = 0;
722 return pcm;
723
724fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700725 if (flags & PCM_MMAP)
726 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
727fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700728 close(pcm->fd);
729 pcm->fd = -1;
730 return pcm;
731}
732
733int pcm_is_ready(struct pcm *pcm)
734{
735 return pcm->fd >= 0;
736}
Simon Wilsond6458e62011-06-21 14:58:11 -0700737
738int pcm_start(struct pcm *pcm)
739{
740 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
741 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700742
743 if (pcm->flags & PCM_MMAP)
744 pcm_sync_ptr(pcm, 0);
745
Simon Wilsond6458e62011-06-21 14:58:11 -0700746 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
747 return oops(pcm, errno, "cannot start channel");
748
Liam Girdwood6be28f12011-10-13 12:59:51 -0700749 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700750 return 0;
751}
752
753int pcm_stop(struct pcm *pcm)
754{
755 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
756 return oops(pcm, errno, "cannot stop channel");
757
Liam Girdwood6be28f12011-10-13 12:59:51 -0700758 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700759 return 0;
760}
761
Liam Girdwood6be28f12011-10-13 12:59:51 -0700762static inline int pcm_mmap_playback_avail(struct pcm *pcm)
763{
764 int avail;
765
766 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
767
768 if (avail < 0)
769 avail += pcm->boundary;
770 else if (avail > (int)pcm->boundary)
771 avail -= pcm->boundary;
772
773 return avail;
774}
775
776static inline int pcm_mmap_capture_avail(struct pcm *pcm)
777{
778 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
779 if (avail < 0)
780 avail += pcm->boundary;
781 return avail;
782}
783
784static inline int pcm_mmap_avail(struct pcm *pcm)
785{
786 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
787 if (pcm->flags & PCM_IN)
788 return pcm_mmap_capture_avail(pcm);
789 else
790 return pcm_mmap_playback_avail(pcm);
791}
792
793static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
794{
795 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
796 appl_ptr += frames;
797
798 /* check for boundary wrap */
799 if (appl_ptr > pcm->boundary)
800 appl_ptr -= pcm->boundary;
801 pcm->mmap_control->appl_ptr = appl_ptr;
802}
803
804int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
805 unsigned int *frames)
806{
807 unsigned int continuous, copy_frames, avail;
808
809 /* return the mmap buffer */
810 *areas = pcm->mmap_buffer;
811
812 /* and the application offset in frames */
813 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
814
815 avail = pcm_mmap_avail(pcm);
816 if (avail > pcm->buffer_size)
817 avail = pcm->buffer_size;
818 continuous = pcm->buffer_size - *offset;
819
820 /* we can only copy frames if the are availabale and continuos */
821 copy_frames = *frames;
822 if (copy_frames > avail)
823 copy_frames = avail;
824 if (copy_frames > continuous)
825 copy_frames = continuous;
826 *frames = copy_frames;
827
828 return 0;
829}
830
831int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
832{
833 /* update the application pointer in userspace and kernel */
834 pcm_mmap_appl_forward(pcm, frames);
835 pcm_sync_ptr(pcm, 0);
836
837 return frames;
838}
839
840int pcm_avail_update(struct pcm *pcm)
841{
842 pcm_sync_ptr(pcm, 0);
843 return pcm_mmap_avail(pcm);
844}
845
846int pcm_state(struct pcm *pcm)
847{
848 int err = pcm_sync_ptr(pcm, 0);
849 if (err < 0)
850 return err;
851
852 return pcm->mmap_status->state;
853}
854
855int pcm_wait(struct pcm *pcm, int timeout)
856{
857 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700858 int err;
859
860 pfd.fd = pcm->fd;
861 pfd.events = POLLOUT | POLLERR | POLLNVAL;
862
863 do {
864 /* let's wait for avail or timeout */
865 err = poll(&pfd, 1, timeout);
866 if (err < 0)
867 return -errno;
868
869 /* timeout ? */
870 if (err == 0)
871 return 0;
872
873 /* have we been interrupted ? */
874 if (errno == -EINTR)
875 continue;
876
877 /* check for any errors */
878 if (pfd.revents & (POLLERR | POLLNVAL)) {
879 switch (pcm_state(pcm)) {
880 case PCM_STATE_XRUN:
881 return -EPIPE;
882 case PCM_STATE_SUSPENDED:
883 return -ESTRPIPE;
884 case PCM_STATE_DISCONNECTED:
885 return -ENODEV;
886 default:
887 return -EIO;
888 }
889 }
890 /* poll again if fd not ready for IO */
891 } while (!(pfd.revents & (POLLIN | POLLOUT)));
892
893 return 1;
894}
895
Mark Brown6bbe77a2012-02-10 22:07:09 +0000896int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700897{
898 int err = 0, frames, avail;
899 unsigned int offset = 0, count;
900
901 if (bytes == 0)
902 return 0;
903
904 count = pcm_bytes_to_frames(pcm, bytes);
905
906 while (count > 0) {
907
908 /* get the available space for writing new frames */
909 avail = pcm_avail_update(pcm);
910 if (avail < 0) {
911 fprintf(stderr, "cannot determine available mmap frames");
912 return err;
913 }
914
915 /* start the audio if we reach the threshold */
916 if (!pcm->running &&
917 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
918 if (pcm_start(pcm) < 0) {
919 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
920 (unsigned int)pcm->mmap_status->hw_ptr,
921 (unsigned int)pcm->mmap_control->appl_ptr,
922 avail);
923 return -errno;
924 }
925 }
926
927 /* sleep until we have space to write new frames */
928 if (pcm->running &&
929 (unsigned int)avail < pcm->mmap_control->avail_min) {
930 int time = -1;
931
932 if (pcm->flags & PCM_NOIRQ)
933 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
934 / pcm->noirq_frames_per_msec;
935
936 err = pcm_wait(pcm, time);
937 if (err < 0) {
938 pcm->running = 0;
939 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
940 (unsigned int)pcm->mmap_status->hw_ptr,
941 (unsigned int)pcm->mmap_control->appl_ptr,
942 avail);
943 pcm->mmap_control->appl_ptr = 0;
944 return err;
945 }
946 continue;
947 }
948
949 frames = count;
950 if (frames > avail)
951 frames = avail;
952
953 if (!frames)
954 break;
955
956 /* copy frames from buffer */
957 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
958 if (frames < 0) {
959 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
960 (unsigned int)pcm->mmap_status->hw_ptr,
961 (unsigned int)pcm->mmap_control->appl_ptr,
962 avail);
963 return frames;
964 }
965
966 offset += frames;
967 count -= frames;
968 }
969
Liam Girdwood6be28f12011-10-13 12:59:51 -0700970 return 0;
971}