blob: ecd2304bd7a36e2ea03aa01e0770e498adf3c0a4 [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
96static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
97{
98 if (param_is_interval(n)) {
99 struct snd_interval *i = param_to_interval(p, n);
100 i->max = val;
101 }
102}
103
104static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
105{
106 if (param_is_interval(n)) {
107 struct snd_interval *i = param_to_interval(p, n);
108 i->min = val;
109 i->max = val;
110 i->integer = 1;
111 }
112}
113
Liam Girdwood6be28f12011-10-13 12:59:51 -0700114static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
115{
116 if (param_is_interval(n)) {
117 struct snd_interval *i = param_to_interval(p, n);
118 if (i->integer)
119 return i->max;
120 }
121 return 0;
122}
123
Simon Wilson79d39652011-05-25 13:44:23 -0700124static void param_init(struct snd_pcm_hw_params *p)
125{
126 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700127
Simon Wilson79d39652011-05-25 13:44:23 -0700128 memset(p, 0, sizeof(*p));
129 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
130 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
131 struct snd_mask *m = param_to_mask(p, n);
132 m->bits[0] = ~0;
133 m->bits[1] = ~0;
134 }
135 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
136 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
137 struct snd_interval *i = param_to_interval(p, n);
138 i->min = 0;
139 i->max = ~0;
140 }
141}
142
143#define PCM_ERROR_MAX 128
144
145struct pcm {
146 int fd;
147 unsigned int flags;
148 int running:1;
149 int underruns;
150 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700151 unsigned int boundary;
Simon Wilson79d39652011-05-25 13:44:23 -0700152 char error[PCM_ERROR_MAX];
153 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700154 struct snd_pcm_mmap_status *mmap_status;
155 struct snd_pcm_mmap_control *mmap_control;
156 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700157 void *mmap_buffer;
158 unsigned int noirq_frames_per_msec;
Simon Wilson79d39652011-05-25 13:44:23 -0700159};
160
Simon Wilson851aa5c2011-05-30 21:18:26 -0700161unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700162{
163 return pcm->buffer_size;
164}
165
166const char* pcm_get_error(struct pcm *pcm)
167{
168 return pcm->error;
169}
170
171static int oops(struct pcm *pcm, int e, const char *fmt, ...)
172{
173 va_list ap;
174 int sz;
175
176 va_start(ap, fmt);
177 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
178 va_end(ap);
179 sz = strlen(pcm->error);
180
181 if (errno)
182 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
183 ": %s", strerror(e));
184 return -1;
185}
186
Simon Wilsonbc03b622011-06-15 17:19:01 -0700187static unsigned int pcm_format_to_alsa(enum pcm_format format)
188{
189 switch (format) {
190 case PCM_FORMAT_S32_LE:
191 return SNDRV_PCM_FORMAT_S32_LE;
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500192 case PCM_FORMAT_S8:
193 return SNDRV_PCM_FORMAT_S8;
194 case PCM_FORMAT_S24_LE:
195 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700196 default:
197 case PCM_FORMAT_S16_LE:
198 return SNDRV_PCM_FORMAT_S16_LE;
199 };
200}
201
202static unsigned int pcm_format_to_bits(enum pcm_format format)
203{
204 switch (format) {
205 case PCM_FORMAT_S32_LE:
206 return 32;
207 default:
208 case PCM_FORMAT_S16_LE:
209 return 16;
210 };
211}
212
Liam Girdwood6be28f12011-10-13 12:59:51 -0700213unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
214{
215 return bytes / (pcm->config.channels *
216 (pcm_format_to_bits(pcm->config.format) >> 3));
217}
218
219unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
220{
221 return frames * pcm->config.channels *
222 (pcm_format_to_bits(pcm->config.format) >> 3);
223}
224
Eric Laurent40b018e2011-06-18 10:10:23 -0700225static int pcm_sync_ptr(struct pcm *pcm, int flags) {
226 if (pcm->sync_ptr) {
227 pcm->sync_ptr->flags = flags;
228 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
229 return -1;
230 }
231 return 0;
232}
233
234static int pcm_hw_mmap_status(struct pcm *pcm) {
235
236 if (pcm->sync_ptr)
237 return 0;
238
239 int page_size = sysconf(_SC_PAGE_SIZE);
240 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
241 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
242 if (pcm->mmap_status == MAP_FAILED)
243 pcm->mmap_status = NULL;
244 if (!pcm->mmap_status)
245 goto mmap_error;
246
247 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
248 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
249 if (pcm->mmap_control == MAP_FAILED)
250 pcm->mmap_control = NULL;
251 if (!pcm->mmap_control) {
252 munmap(pcm->mmap_status, page_size);
253 pcm->mmap_status = NULL;
254 goto mmap_error;
255 }
256 pcm->mmap_control->avail_min = 1;
257
258 return 0;
259
260mmap_error:
261
262 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
263 if (!pcm->sync_ptr)
264 return -ENOMEM;
265 pcm->mmap_status = &pcm->sync_ptr->s.status;
266 pcm->mmap_control = &pcm->sync_ptr->c.control;
267 pcm->mmap_control->avail_min = 1;
268 pcm_sync_ptr(pcm, 0);
269
270 return 0;
271}
272
273static void pcm_hw_munmap_status(struct pcm *pcm) {
274 if (pcm->sync_ptr) {
275 free(pcm->sync_ptr);
276 pcm->sync_ptr = NULL;
277 } else {
278 int page_size = sysconf(_SC_PAGE_SIZE);
279 if (pcm->mmap_status)
280 munmap(pcm->mmap_status, page_size);
281 if (pcm->mmap_control)
282 munmap(pcm->mmap_control, page_size);
283 }
284 pcm->mmap_status = NULL;
285 pcm->mmap_control = NULL;
286}
287
Liam Girdwood6be28f12011-10-13 12:59:51 -0700288static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
289 const char *src, unsigned int src_offset,
290 unsigned int frames)
291{
292 int size_bytes = pcm_frames_to_bytes(pcm, frames);
293 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
294 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
295
296 /* interleaved only atm */
297 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
298 src + src_offset_bytes, size_bytes);
299 return 0;
300}
301
Mark Brown6bbe77a2012-02-10 22:07:09 +0000302static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700303 unsigned int offset, unsigned int size)
304{
305 void *pcm_areas;
306 int commit;
307 unsigned int pcm_offset, frames, count = 0;
308
309 while (size > 0) {
310 frames = size;
311 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
312 pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
313 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
314 if (commit < 0) {
315 oops(pcm, commit, "failed to commit %d frames\n", frames);
316 return commit;
317 }
318
319 offset += commit;
320 count += commit;
321 size -= commit;
322 }
323 return count;
324}
325
Eric Laurent40b018e2011-06-18 10:10:23 -0700326int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
327 struct timespec *tstamp)
328{
329 int frames;
330 int rc;
331 snd_pcm_uframes_t hw_ptr;
332
333 if (!pcm_is_ready(pcm))
334 return -1;
335
336 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
337 if (rc < 0)
338 return -1;
339
Eric Laurent7db48582011-11-17 11:47:59 -0800340 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
341 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800342 return -1;
343
Eric Laurent40b018e2011-06-18 10:10:23 -0700344 *tstamp = pcm->mmap_status->tstamp;
345 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
346 return -1;
347
348 hw_ptr = pcm->mmap_status->hw_ptr;
349 if (pcm->flags & PCM_IN)
350 frames = hw_ptr - pcm->mmap_control->appl_ptr;
351 else
352 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
353
354 if (frames < 0)
355 return -1;
356
357 *avail = (unsigned int)frames;
358
359 return 0;
360}
361
Mark Brown6bbe77a2012-02-10 22:07:09 +0000362int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700363{
364 struct snd_xferi x;
365
366 if (pcm->flags & PCM_IN)
367 return -EINVAL;
368
Mark Brown6bbe77a2012-02-10 22:07:09 +0000369 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700370 x.frames = count / (pcm->config.channels *
371 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700372
373 for (;;) {
374 if (!pcm->running) {
375 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
376 return oops(pcm, errno, "cannot prepare channel");
377 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
378 return oops(pcm, errno, "cannot write initial data");
379 pcm->running = 1;
380 return 0;
381 }
382 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
383 pcm->running = 0;
384 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700385 /* we failed to make our window -- try to restart if we are
386 * allowed to do so. Otherwise, simply allow the EPIPE error to
387 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700388 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700389 if (pcm->flags & PCM_NORESTART)
390 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700391 continue;
392 }
393 return oops(pcm, errno, "cannot write stream data");
394 }
395 return 0;
396 }
397}
398
399int pcm_read(struct pcm *pcm, void *data, unsigned int count)
400{
401 struct snd_xferi x;
402
403 if (!(pcm->flags & PCM_IN))
404 return -EINVAL;
405
406 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700407 x.frames = count / (pcm->config.channels *
408 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700409
410 for (;;) {
411 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700412 if (pcm_start(pcm) < 0) {
413 fprintf(stderr, "start error");
414 return -errno;
415 }
Simon Wilson79d39652011-05-25 13:44:23 -0700416 }
417 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
418 pcm->running = 0;
419 if (errno == EPIPE) {
420 /* we failed to make our window -- try to restart */
421 pcm->underruns++;
422 continue;
423 }
424 return oops(pcm, errno, "cannot read stream data");
425 }
426 return 0;
427 }
428}
429
430static struct pcm bad_pcm = {
431 .fd = -1,
432};
433
434int pcm_close(struct pcm *pcm)
435{
436 if (pcm == &bad_pcm)
437 return 0;
438
Eric Laurent40b018e2011-06-18 10:10:23 -0700439 pcm_hw_munmap_status(pcm);
440
Liam Girdwood6be28f12011-10-13 12:59:51 -0700441 if (pcm->flags & PCM_MMAP) {
442 pcm_stop(pcm);
443 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
444 }
445
Simon Wilson79d39652011-05-25 13:44:23 -0700446 if (pcm->fd >= 0)
447 close(pcm->fd);
448 pcm->running = 0;
449 pcm->buffer_size = 0;
450 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700451 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700452 return 0;
453}
454
Simon Wilson1bd580f2011-06-02 15:58:41 -0700455struct pcm *pcm_open(unsigned int card, unsigned int device,
456 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700457{
Simon Wilson79d39652011-05-25 13:44:23 -0700458 struct pcm *pcm;
459 struct snd_pcm_info info;
460 struct snd_pcm_hw_params params;
461 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700462 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700463 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700464
465 pcm = calloc(1, sizeof(struct pcm));
466 if (!pcm || !config)
467 return &bad_pcm; /* TODO: could support default config here */
468
469 pcm->config = *config;
470
Simon Wilson1bd580f2011-06-02 15:58:41 -0700471 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
472 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700473
474 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700475 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700476 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700477 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700478 return pcm;
479 }
480
481 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700482 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700483 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700484 }
485
486 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700487 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
488 pcm_format_to_alsa(config->format));
489 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
490 SNDRV_PCM_SUBFORMAT_STD);
491 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
492 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
493 pcm_format_to_bits(config->format));
494 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
495 pcm_format_to_bits(config->format) * config->channels);
496 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
497 config->channels);
498 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
499 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
500
Liam Girdwood6be28f12011-10-13 12:59:51 -0700501 if (flags & PCM_NOIRQ) {
502
503 if (!(flags & PCM_MMAP)) {
504 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
505 goto fail;
506 }
507
508 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
509 pcm->noirq_frames_per_msec = config->rate / 1000;
510 }
511
512 if (flags & PCM_MMAP)
513 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
514 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
515 else
516 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
517 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
518
Simon Wilson79d39652011-05-25 13:44:23 -0700519 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
520 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700521 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700522 }
523
Liam Girdwood6be28f12011-10-13 12:59:51 -0700524 /* get our refined hw_params */
525 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
526 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
527 pcm->buffer_size = config->period_count * config->period_size;
528
529 if (flags & PCM_MMAP) {
530 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
531 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
532 if (pcm->mmap_buffer == MAP_FAILED) {
533 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
534 pcm_frames_to_bytes(pcm, pcm->buffer_size));
535 goto fail_close;
536 }
537 }
538
539
Simon Wilson79d39652011-05-25 13:44:23 -0700540 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700541 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700542 sparams.period_step = 1;
543 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700544
Eric Laurent93e7b672012-08-22 16:18:14 -0700545 if (!config->start_threshold) {
546 if (pcm->flags & PCM_IN)
547 pcm->config.start_threshold = sparams.start_threshold = 1;
548 else
549 pcm->config.start_threshold = sparams.start_threshold =
550 config->period_count * config->period_size / 2;
551 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700552 sparams.start_threshold = config->start_threshold;
553
Liam Girdwood6be28f12011-10-13 12:59:51 -0700554 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800555 if (!config->stop_threshold) {
556 if (pcm->flags & PCM_IN)
557 pcm->config.stop_threshold = sparams.stop_threshold =
558 config->period_count * config->period_size * 10;
559 else
560 pcm->config.stop_threshold = sparams.stop_threshold =
561 config->period_count * config->period_size;
562 }
John Grossman3bb114a2011-07-21 10:59:55 -0700563 else
564 sparams.stop_threshold = config->stop_threshold;
565
Simon Wilson79d39652011-05-25 13:44:23 -0700566 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
567 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700568 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700569 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700570
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600571 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700572 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700573
574 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
575 oops(pcm, errno, "cannot set sw params");
576 goto fail;
577 }
578
Eric Laurent40b018e2011-06-18 10:10:23 -0700579 rc = pcm_hw_mmap_status(pcm);
580 if (rc < 0) {
581 oops(pcm, rc, "mmap status failed");
582 goto fail;
583 }
584
Simon Wilson79d39652011-05-25 13:44:23 -0700585 pcm->underruns = 0;
586 return pcm;
587
588fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700589 if (flags & PCM_MMAP)
590 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
591fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700592 close(pcm->fd);
593 pcm->fd = -1;
594 return pcm;
595}
596
597int pcm_is_ready(struct pcm *pcm)
598{
599 return pcm->fd >= 0;
600}
Simon Wilsond6458e62011-06-21 14:58:11 -0700601
602int pcm_start(struct pcm *pcm)
603{
604 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
605 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700606
607 if (pcm->flags & PCM_MMAP)
608 pcm_sync_ptr(pcm, 0);
609
Simon Wilsond6458e62011-06-21 14:58:11 -0700610 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
611 return oops(pcm, errno, "cannot start channel");
612
Liam Girdwood6be28f12011-10-13 12:59:51 -0700613 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700614 return 0;
615}
616
617int pcm_stop(struct pcm *pcm)
618{
619 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
620 return oops(pcm, errno, "cannot stop channel");
621
Liam Girdwood6be28f12011-10-13 12:59:51 -0700622 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700623 return 0;
624}
625
Liam Girdwood6be28f12011-10-13 12:59:51 -0700626static inline int pcm_mmap_playback_avail(struct pcm *pcm)
627{
628 int avail;
629
630 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
631
632 if (avail < 0)
633 avail += pcm->boundary;
634 else if (avail > (int)pcm->boundary)
635 avail -= pcm->boundary;
636
637 return avail;
638}
639
640static inline int pcm_mmap_capture_avail(struct pcm *pcm)
641{
642 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
643 if (avail < 0)
644 avail += pcm->boundary;
645 return avail;
646}
647
648static inline int pcm_mmap_avail(struct pcm *pcm)
649{
650 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
651 if (pcm->flags & PCM_IN)
652 return pcm_mmap_capture_avail(pcm);
653 else
654 return pcm_mmap_playback_avail(pcm);
655}
656
657static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
658{
659 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
660 appl_ptr += frames;
661
662 /* check for boundary wrap */
663 if (appl_ptr > pcm->boundary)
664 appl_ptr -= pcm->boundary;
665 pcm->mmap_control->appl_ptr = appl_ptr;
666}
667
668int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
669 unsigned int *frames)
670{
671 unsigned int continuous, copy_frames, avail;
672
673 /* return the mmap buffer */
674 *areas = pcm->mmap_buffer;
675
676 /* and the application offset in frames */
677 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
678
679 avail = pcm_mmap_avail(pcm);
680 if (avail > pcm->buffer_size)
681 avail = pcm->buffer_size;
682 continuous = pcm->buffer_size - *offset;
683
684 /* we can only copy frames if the are availabale and continuos */
685 copy_frames = *frames;
686 if (copy_frames > avail)
687 copy_frames = avail;
688 if (copy_frames > continuous)
689 copy_frames = continuous;
690 *frames = copy_frames;
691
692 return 0;
693}
694
695int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
696{
697 /* update the application pointer in userspace and kernel */
698 pcm_mmap_appl_forward(pcm, frames);
699 pcm_sync_ptr(pcm, 0);
700
701 return frames;
702}
703
704int pcm_avail_update(struct pcm *pcm)
705{
706 pcm_sync_ptr(pcm, 0);
707 return pcm_mmap_avail(pcm);
708}
709
710int pcm_state(struct pcm *pcm)
711{
712 int err = pcm_sync_ptr(pcm, 0);
713 if (err < 0)
714 return err;
715
716 return pcm->mmap_status->state;
717}
718
719int pcm_wait(struct pcm *pcm, int timeout)
720{
721 struct pollfd pfd;
722 unsigned short revents = 0;
723 int err;
724
725 pfd.fd = pcm->fd;
726 pfd.events = POLLOUT | POLLERR | POLLNVAL;
727
728 do {
729 /* let's wait for avail or timeout */
730 err = poll(&pfd, 1, timeout);
731 if (err < 0)
732 return -errno;
733
734 /* timeout ? */
735 if (err == 0)
736 return 0;
737
738 /* have we been interrupted ? */
739 if (errno == -EINTR)
740 continue;
741
742 /* check for any errors */
743 if (pfd.revents & (POLLERR | POLLNVAL)) {
744 switch (pcm_state(pcm)) {
745 case PCM_STATE_XRUN:
746 return -EPIPE;
747 case PCM_STATE_SUSPENDED:
748 return -ESTRPIPE;
749 case PCM_STATE_DISCONNECTED:
750 return -ENODEV;
751 default:
752 return -EIO;
753 }
754 }
755 /* poll again if fd not ready for IO */
756 } while (!(pfd.revents & (POLLIN | POLLOUT)));
757
758 return 1;
759}
760
Mark Brown6bbe77a2012-02-10 22:07:09 +0000761int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700762{
763 int err = 0, frames, avail;
764 unsigned int offset = 0, count;
765
766 if (bytes == 0)
767 return 0;
768
769 count = pcm_bytes_to_frames(pcm, bytes);
770
771 while (count > 0) {
772
773 /* get the available space for writing new frames */
774 avail = pcm_avail_update(pcm);
775 if (avail < 0) {
776 fprintf(stderr, "cannot determine available mmap frames");
777 return err;
778 }
779
780 /* start the audio if we reach the threshold */
781 if (!pcm->running &&
782 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
783 if (pcm_start(pcm) < 0) {
784 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
785 (unsigned int)pcm->mmap_status->hw_ptr,
786 (unsigned int)pcm->mmap_control->appl_ptr,
787 avail);
788 return -errno;
789 }
790 }
791
792 /* sleep until we have space to write new frames */
793 if (pcm->running &&
794 (unsigned int)avail < pcm->mmap_control->avail_min) {
795 int time = -1;
796
797 if (pcm->flags & PCM_NOIRQ)
798 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
799 / pcm->noirq_frames_per_msec;
800
801 err = pcm_wait(pcm, time);
802 if (err < 0) {
803 pcm->running = 0;
804 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
805 (unsigned int)pcm->mmap_status->hw_ptr,
806 (unsigned int)pcm->mmap_control->appl_ptr,
807 avail);
808 pcm->mmap_control->appl_ptr = 0;
809 return err;
810 }
811 continue;
812 }
813
814 frames = count;
815 if (frames > avail)
816 frames = avail;
817
818 if (!frames)
819 break;
820
821 /* copy frames from buffer */
822 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
823 if (frames < 0) {
824 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
825 (unsigned int)pcm->mmap_status->hw_ptr,
826 (unsigned int)pcm->mmap_control->appl_ptr,
827 avail);
828 return frames;
829 }
830
831 offset += frames;
832 count -= frames;
833 }
834
835_end:
836 return 0;
837}