blob: 2dca1570f4301f526d18a10071167ff637af0d91 [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;
192 default:
193 case PCM_FORMAT_S16_LE:
194 return SNDRV_PCM_FORMAT_S16_LE;
195 };
196}
197
198static unsigned int pcm_format_to_bits(enum pcm_format format)
199{
200 switch (format) {
201 case PCM_FORMAT_S32_LE:
202 return 32;
203 default:
204 case PCM_FORMAT_S16_LE:
205 return 16;
206 };
207}
208
Liam Girdwood6be28f12011-10-13 12:59:51 -0700209unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
210{
211 return bytes / (pcm->config.channels *
212 (pcm_format_to_bits(pcm->config.format) >> 3));
213}
214
215unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
216{
217 return frames * pcm->config.channels *
218 (pcm_format_to_bits(pcm->config.format) >> 3);
219}
220
Eric Laurent40b018e2011-06-18 10:10:23 -0700221static int pcm_sync_ptr(struct pcm *pcm, int flags) {
222 if (pcm->sync_ptr) {
223 pcm->sync_ptr->flags = flags;
224 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
225 return -1;
226 }
227 return 0;
228}
229
230static int pcm_hw_mmap_status(struct pcm *pcm) {
231
232 if (pcm->sync_ptr)
233 return 0;
234
235 int page_size = sysconf(_SC_PAGE_SIZE);
236 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
237 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
238 if (pcm->mmap_status == MAP_FAILED)
239 pcm->mmap_status = NULL;
240 if (!pcm->mmap_status)
241 goto mmap_error;
242
243 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
244 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
245 if (pcm->mmap_control == MAP_FAILED)
246 pcm->mmap_control = NULL;
247 if (!pcm->mmap_control) {
248 munmap(pcm->mmap_status, page_size);
249 pcm->mmap_status = NULL;
250 goto mmap_error;
251 }
252 pcm->mmap_control->avail_min = 1;
253
254 return 0;
255
256mmap_error:
257
258 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
259 if (!pcm->sync_ptr)
260 return -ENOMEM;
261 pcm->mmap_status = &pcm->sync_ptr->s.status;
262 pcm->mmap_control = &pcm->sync_ptr->c.control;
263 pcm->mmap_control->avail_min = 1;
264 pcm_sync_ptr(pcm, 0);
265
266 return 0;
267}
268
269static void pcm_hw_munmap_status(struct pcm *pcm) {
270 if (pcm->sync_ptr) {
271 free(pcm->sync_ptr);
272 pcm->sync_ptr = NULL;
273 } else {
274 int page_size = sysconf(_SC_PAGE_SIZE);
275 if (pcm->mmap_status)
276 munmap(pcm->mmap_status, page_size);
277 if (pcm->mmap_control)
278 munmap(pcm->mmap_control, page_size);
279 }
280 pcm->mmap_status = NULL;
281 pcm->mmap_control = NULL;
282}
283
Liam Girdwood6be28f12011-10-13 12:59:51 -0700284static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
285 const char *src, unsigned int src_offset,
286 unsigned int frames)
287{
288 int size_bytes = pcm_frames_to_bytes(pcm, frames);
289 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
290 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
291
292 /* interleaved only atm */
293 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
294 src + src_offset_bytes, size_bytes);
295 return 0;
296}
297
Mark Brown6bbe77a2012-02-10 22:07:09 +0000298static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700299 unsigned int offset, unsigned int size)
300{
301 void *pcm_areas;
302 int commit;
303 unsigned int pcm_offset, frames, count = 0;
304
305 while (size > 0) {
306 frames = size;
307 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
308 pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
309 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
310 if (commit < 0) {
311 oops(pcm, commit, "failed to commit %d frames\n", frames);
312 return commit;
313 }
314
315 offset += commit;
316 count += commit;
317 size -= commit;
318 }
319 return count;
320}
321
Eric Laurent40b018e2011-06-18 10:10:23 -0700322int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
323 struct timespec *tstamp)
324{
325 int frames;
326 int rc;
327 snd_pcm_uframes_t hw_ptr;
328
329 if (!pcm_is_ready(pcm))
330 return -1;
331
332 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
333 if (rc < 0)
334 return -1;
335
Eric Laurent7db48582011-11-17 11:47:59 -0800336 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
337 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800338 return -1;
339
Eric Laurent40b018e2011-06-18 10:10:23 -0700340 *tstamp = pcm->mmap_status->tstamp;
341 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
342 return -1;
343
344 hw_ptr = pcm->mmap_status->hw_ptr;
345 if (pcm->flags & PCM_IN)
346 frames = hw_ptr - pcm->mmap_control->appl_ptr;
347 else
348 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
349
350 if (frames < 0)
351 return -1;
352
353 *avail = (unsigned int)frames;
354
355 return 0;
356}
357
Mark Brown6bbe77a2012-02-10 22:07:09 +0000358int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700359{
360 struct snd_xferi x;
361
362 if (pcm->flags & PCM_IN)
363 return -EINVAL;
364
Mark Brown6bbe77a2012-02-10 22:07:09 +0000365 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700366 x.frames = count / (pcm->config.channels *
367 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700368
369 for (;;) {
370 if (!pcm->running) {
371 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
372 return oops(pcm, errno, "cannot prepare channel");
373 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
374 return oops(pcm, errno, "cannot write initial data");
375 pcm->running = 1;
376 return 0;
377 }
378 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
379 pcm->running = 0;
380 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700381 /* we failed to make our window -- try to restart if we are
382 * allowed to do so. Otherwise, simply allow the EPIPE error to
383 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700384 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700385 if (pcm->flags & PCM_NORESTART)
386 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700387 continue;
388 }
389 return oops(pcm, errno, "cannot write stream data");
390 }
391 return 0;
392 }
393}
394
395int pcm_read(struct pcm *pcm, void *data, unsigned int count)
396{
397 struct snd_xferi x;
398
399 if (!(pcm->flags & PCM_IN))
400 return -EINVAL;
401
402 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700403 x.frames = count / (pcm->config.channels *
404 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700405
406 for (;;) {
407 if (!pcm->running) {
Mark Brown5f64fe72011-07-29 11:04:13 +0100408 pcm_start(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700409 }
410 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
411 pcm->running = 0;
412 if (errno == EPIPE) {
413 /* we failed to make our window -- try to restart */
414 pcm->underruns++;
415 continue;
416 }
417 return oops(pcm, errno, "cannot read stream data");
418 }
419 return 0;
420 }
421}
422
423static struct pcm bad_pcm = {
424 .fd = -1,
425};
426
427int pcm_close(struct pcm *pcm)
428{
429 if (pcm == &bad_pcm)
430 return 0;
431
Eric Laurent40b018e2011-06-18 10:10:23 -0700432 pcm_hw_munmap_status(pcm);
433
Liam Girdwood6be28f12011-10-13 12:59:51 -0700434 if (pcm->flags & PCM_MMAP) {
435 pcm_stop(pcm);
436 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
437 }
438
Simon Wilson79d39652011-05-25 13:44:23 -0700439 if (pcm->fd >= 0)
440 close(pcm->fd);
441 pcm->running = 0;
442 pcm->buffer_size = 0;
443 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700444 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700445 return 0;
446}
447
Simon Wilson1bd580f2011-06-02 15:58:41 -0700448struct pcm *pcm_open(unsigned int card, unsigned int device,
449 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700450{
Simon Wilson79d39652011-05-25 13:44:23 -0700451 struct pcm *pcm;
452 struct snd_pcm_info info;
453 struct snd_pcm_hw_params params;
454 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700455 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700456 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700457
458 pcm = calloc(1, sizeof(struct pcm));
459 if (!pcm || !config)
460 return &bad_pcm; /* TODO: could support default config here */
461
462 pcm->config = *config;
463
Simon Wilson1bd580f2011-06-02 15:58:41 -0700464 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
465 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700466
467 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700468 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700469 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700470 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700471 return pcm;
472 }
473
474 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700475 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700476 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700477 }
478
479 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700480 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
481 pcm_format_to_alsa(config->format));
482 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
483 SNDRV_PCM_SUBFORMAT_STD);
484 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
485 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
486 pcm_format_to_bits(config->format));
487 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
488 pcm_format_to_bits(config->format) * config->channels);
489 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
490 config->channels);
491 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
492 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
493
Liam Girdwood6be28f12011-10-13 12:59:51 -0700494 if (flags & PCM_NOIRQ) {
495
496 if (!(flags & PCM_MMAP)) {
497 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
498 goto fail;
499 }
500
501 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
502 pcm->noirq_frames_per_msec = config->rate / 1000;
503 }
504
505 if (flags & PCM_MMAP)
506 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
507 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
508 else
509 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
510 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
511
Simon Wilson79d39652011-05-25 13:44:23 -0700512 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
513 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700514 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700515 }
516
Liam Girdwood6be28f12011-10-13 12:59:51 -0700517 /* get our refined hw_params */
518 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
519 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
520 pcm->buffer_size = config->period_count * config->period_size;
521
522 if (flags & PCM_MMAP) {
523 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
524 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
525 if (pcm->mmap_buffer == MAP_FAILED) {
526 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
527 pcm_frames_to_bytes(pcm, pcm->buffer_size));
528 goto fail_close;
529 }
530 }
531
532
Simon Wilson79d39652011-05-25 13:44:23 -0700533 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700534 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700535 sparams.period_step = 1;
536 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700537
538 if (!config->start_threshold)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700539 pcm->config.start_threshold = sparams.start_threshold =
540 config->period_count * config->period_size / 2;
John Grossman3bb114a2011-07-21 10:59:55 -0700541 else
542 sparams.start_threshold = config->start_threshold;
543
Liam Girdwood6be28f12011-10-13 12:59:51 -0700544 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800545 if (!config->stop_threshold) {
546 if (pcm->flags & PCM_IN)
547 pcm->config.stop_threshold = sparams.stop_threshold =
548 config->period_count * config->period_size * 10;
549 else
550 pcm->config.stop_threshold = sparams.stop_threshold =
551 config->period_count * config->period_size;
552 }
John Grossman3bb114a2011-07-21 10:59:55 -0700553 else
554 sparams.stop_threshold = config->stop_threshold;
555
Simon Wilson79d39652011-05-25 13:44:23 -0700556 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
557 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700558 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700559 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700560
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600561 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700562 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700563
564 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
565 oops(pcm, errno, "cannot set sw params");
566 goto fail;
567 }
568
Eric Laurent40b018e2011-06-18 10:10:23 -0700569 rc = pcm_hw_mmap_status(pcm);
570 if (rc < 0) {
571 oops(pcm, rc, "mmap status failed");
572 goto fail;
573 }
574
Simon Wilson79d39652011-05-25 13:44:23 -0700575 pcm->underruns = 0;
576 return pcm;
577
578fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700579 if (flags & PCM_MMAP)
580 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
581fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700582 close(pcm->fd);
583 pcm->fd = -1;
584 return pcm;
585}
586
587int pcm_is_ready(struct pcm *pcm)
588{
589 return pcm->fd >= 0;
590}
Simon Wilsond6458e62011-06-21 14:58:11 -0700591
592int pcm_start(struct pcm *pcm)
593{
594 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
595 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700596
597 if (pcm->flags & PCM_MMAP)
598 pcm_sync_ptr(pcm, 0);
599
Simon Wilsond6458e62011-06-21 14:58:11 -0700600 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
601 return oops(pcm, errno, "cannot start channel");
602
Liam Girdwood6be28f12011-10-13 12:59:51 -0700603 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700604 return 0;
605}
606
607int pcm_stop(struct pcm *pcm)
608{
609 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
610 return oops(pcm, errno, "cannot stop channel");
611
Liam Girdwood6be28f12011-10-13 12:59:51 -0700612 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700613 return 0;
614}
615
Liam Girdwood6be28f12011-10-13 12:59:51 -0700616static inline int pcm_mmap_playback_avail(struct pcm *pcm)
617{
618 int avail;
619
620 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
621
622 if (avail < 0)
623 avail += pcm->boundary;
624 else if (avail > (int)pcm->boundary)
625 avail -= pcm->boundary;
626
627 return avail;
628}
629
630static inline int pcm_mmap_capture_avail(struct pcm *pcm)
631{
632 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
633 if (avail < 0)
634 avail += pcm->boundary;
635 return avail;
636}
637
638static inline int pcm_mmap_avail(struct pcm *pcm)
639{
640 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
641 if (pcm->flags & PCM_IN)
642 return pcm_mmap_capture_avail(pcm);
643 else
644 return pcm_mmap_playback_avail(pcm);
645}
646
647static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
648{
649 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
650 appl_ptr += frames;
651
652 /* check for boundary wrap */
653 if (appl_ptr > pcm->boundary)
654 appl_ptr -= pcm->boundary;
655 pcm->mmap_control->appl_ptr = appl_ptr;
656}
657
658int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
659 unsigned int *frames)
660{
661 unsigned int continuous, copy_frames, avail;
662
663 /* return the mmap buffer */
664 *areas = pcm->mmap_buffer;
665
666 /* and the application offset in frames */
667 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
668
669 avail = pcm_mmap_avail(pcm);
670 if (avail > pcm->buffer_size)
671 avail = pcm->buffer_size;
672 continuous = pcm->buffer_size - *offset;
673
674 /* we can only copy frames if the are availabale and continuos */
675 copy_frames = *frames;
676 if (copy_frames > avail)
677 copy_frames = avail;
678 if (copy_frames > continuous)
679 copy_frames = continuous;
680 *frames = copy_frames;
681
682 return 0;
683}
684
685int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
686{
687 /* update the application pointer in userspace and kernel */
688 pcm_mmap_appl_forward(pcm, frames);
689 pcm_sync_ptr(pcm, 0);
690
691 return frames;
692}
693
694int pcm_avail_update(struct pcm *pcm)
695{
696 pcm_sync_ptr(pcm, 0);
697 return pcm_mmap_avail(pcm);
698}
699
700int pcm_state(struct pcm *pcm)
701{
702 int err = pcm_sync_ptr(pcm, 0);
703 if (err < 0)
704 return err;
705
706 return pcm->mmap_status->state;
707}
708
709int pcm_wait(struct pcm *pcm, int timeout)
710{
711 struct pollfd pfd;
712 unsigned short revents = 0;
713 int err;
714
715 pfd.fd = pcm->fd;
716 pfd.events = POLLOUT | POLLERR | POLLNVAL;
717
718 do {
719 /* let's wait for avail or timeout */
720 err = poll(&pfd, 1, timeout);
721 if (err < 0)
722 return -errno;
723
724 /* timeout ? */
725 if (err == 0)
726 return 0;
727
728 /* have we been interrupted ? */
729 if (errno == -EINTR)
730 continue;
731
732 /* check for any errors */
733 if (pfd.revents & (POLLERR | POLLNVAL)) {
734 switch (pcm_state(pcm)) {
735 case PCM_STATE_XRUN:
736 return -EPIPE;
737 case PCM_STATE_SUSPENDED:
738 return -ESTRPIPE;
739 case PCM_STATE_DISCONNECTED:
740 return -ENODEV;
741 default:
742 return -EIO;
743 }
744 }
745 /* poll again if fd not ready for IO */
746 } while (!(pfd.revents & (POLLIN | POLLOUT)));
747
748 return 1;
749}
750
Mark Brown6bbe77a2012-02-10 22:07:09 +0000751int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700752{
753 int err = 0, frames, avail;
754 unsigned int offset = 0, count;
755
756 if (bytes == 0)
757 return 0;
758
759 count = pcm_bytes_to_frames(pcm, bytes);
760
761 while (count > 0) {
762
763 /* get the available space for writing new frames */
764 avail = pcm_avail_update(pcm);
765 if (avail < 0) {
766 fprintf(stderr, "cannot determine available mmap frames");
767 return err;
768 }
769
770 /* start the audio if we reach the threshold */
771 if (!pcm->running &&
772 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
773 if (pcm_start(pcm) < 0) {
774 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
775 (unsigned int)pcm->mmap_status->hw_ptr,
776 (unsigned int)pcm->mmap_control->appl_ptr,
777 avail);
778 return -errno;
779 }
780 }
781
782 /* sleep until we have space to write new frames */
783 if (pcm->running &&
784 (unsigned int)avail < pcm->mmap_control->avail_min) {
785 int time = -1;
786
787 if (pcm->flags & PCM_NOIRQ)
788 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
789 / pcm->noirq_frames_per_msec;
790
791 err = pcm_wait(pcm, time);
792 if (err < 0) {
793 pcm->running = 0;
794 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
795 (unsigned int)pcm->mmap_status->hw_ptr,
796 (unsigned int)pcm->mmap_control->appl_ptr,
797 avail);
798 pcm->mmap_control->appl_ptr = 0;
799 return err;
800 }
801 continue;
802 }
803
804 frames = count;
805 if (frames > avail)
806 frames = avail;
807
808 if (!frames)
809 break;
810
811 /* copy frames from buffer */
812 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
813 if (frames < 0) {
814 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
815 (unsigned int)pcm->mmap_status->hw_ptr,
816 (unsigned int)pcm->mmap_control->appl_ptr,
817 avail);
818 return frames;
819 }
820
821 offset += frames;
822 count -= frames;
823 }
824
825_end:
826 return 0;
827}