blob: 461eb5a4fd243e1559b2fbc82cedbddf8e7e1c50 [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
298static int pcm_mmap_write_areas(struct pcm *pcm, char *src,
299 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 Laurentee9ba872011-11-15 19:04:03 -0800336 if (pcm->mmap_status->state == PCM_STATE_XRUN)
337 return -1;
338
Eric Laurent40b018e2011-06-18 10:10:23 -0700339 *tstamp = pcm->mmap_status->tstamp;
340 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
341 return -1;
342
343 hw_ptr = pcm->mmap_status->hw_ptr;
344 if (pcm->flags & PCM_IN)
345 frames = hw_ptr - pcm->mmap_control->appl_ptr;
346 else
347 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
348
349 if (frames < 0)
350 return -1;
351
352 *avail = (unsigned int)frames;
353
354 return 0;
355}
356
Simon Wilson79d39652011-05-25 13:44:23 -0700357int pcm_write(struct pcm *pcm, void *data, unsigned int count)
358{
359 struct snd_xferi x;
360
361 if (pcm->flags & PCM_IN)
362 return -EINVAL;
363
364 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700365 x.frames = count / (pcm->config.channels *
366 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700367
368 for (;;) {
369 if (!pcm->running) {
370 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
371 return oops(pcm, errno, "cannot prepare channel");
372 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
373 return oops(pcm, errno, "cannot write initial data");
374 pcm->running = 1;
375 return 0;
376 }
377 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
378 pcm->running = 0;
379 if (errno == EPIPE) {
380 /* we failed to make our window -- try to restart */
381 pcm->underruns++;
382 continue;
383 }
384 return oops(pcm, errno, "cannot write stream data");
385 }
386 return 0;
387 }
388}
389
390int pcm_read(struct pcm *pcm, void *data, unsigned int count)
391{
392 struct snd_xferi x;
393
394 if (!(pcm->flags & PCM_IN))
395 return -EINVAL;
396
397 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700398 x.frames = count / (pcm->config.channels *
399 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700400
401 for (;;) {
402 if (!pcm->running) {
403 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
404 return oops(pcm, errno, "cannot prepare channel");
405 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START))
406 return oops(pcm, errno, "cannot start channel");
407 pcm->running = 1;
408 }
409 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
410 pcm->running = 0;
411 if (errno == EPIPE) {
412 /* we failed to make our window -- try to restart */
413 pcm->underruns++;
414 continue;
415 }
416 return oops(pcm, errno, "cannot read stream data");
417 }
418 return 0;
419 }
420}
421
422static struct pcm bad_pcm = {
423 .fd = -1,
424};
425
426int pcm_close(struct pcm *pcm)
427{
428 if (pcm == &bad_pcm)
429 return 0;
430
Eric Laurent40b018e2011-06-18 10:10:23 -0700431 pcm_hw_munmap_status(pcm);
432
Liam Girdwood6be28f12011-10-13 12:59:51 -0700433 if (pcm->flags & PCM_MMAP) {
434 pcm_stop(pcm);
435 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
436 }
437
Simon Wilson79d39652011-05-25 13:44:23 -0700438 if (pcm->fd >= 0)
439 close(pcm->fd);
440 pcm->running = 0;
441 pcm->buffer_size = 0;
442 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700443 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700444 return 0;
445}
446
Simon Wilson1bd580f2011-06-02 15:58:41 -0700447struct pcm *pcm_open(unsigned int card, unsigned int device,
448 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700449{
Simon Wilson79d39652011-05-25 13:44:23 -0700450 struct pcm *pcm;
451 struct snd_pcm_info info;
452 struct snd_pcm_hw_params params;
453 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700454 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700455 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700456
457 pcm = calloc(1, sizeof(struct pcm));
458 if (!pcm || !config)
459 return &bad_pcm; /* TODO: could support default config here */
460
461 pcm->config = *config;
462
Simon Wilson1bd580f2011-06-02 15:58:41 -0700463 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
464 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700465
466 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700467 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700468 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700469 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700470 return pcm;
471 }
472
473 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700474 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700475 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700476 }
477
478 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700479 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
480 pcm_format_to_alsa(config->format));
481 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
482 SNDRV_PCM_SUBFORMAT_STD);
483 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
484 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
485 pcm_format_to_bits(config->format));
486 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
487 pcm_format_to_bits(config->format) * config->channels);
488 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
489 config->channels);
490 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
491 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
492
Liam Girdwood6be28f12011-10-13 12:59:51 -0700493 if (flags & PCM_NOIRQ) {
494
495 if (!(flags & PCM_MMAP)) {
496 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
497 goto fail;
498 }
499
500 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
501 pcm->noirq_frames_per_msec = config->rate / 1000;
502 }
503
504 if (flags & PCM_MMAP)
505 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
506 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
507 else
508 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
509 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
510
Simon Wilson79d39652011-05-25 13:44:23 -0700511 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
512 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700513 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700514 }
515
Liam Girdwood6be28f12011-10-13 12:59:51 -0700516 /* get our refined hw_params */
517 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
518 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
519 pcm->buffer_size = config->period_count * config->period_size;
520
521 if (flags & PCM_MMAP) {
522 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
523 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
524 if (pcm->mmap_buffer == MAP_FAILED) {
525 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
526 pcm_frames_to_bytes(pcm, pcm->buffer_size));
527 goto fail_close;
528 }
529 }
530
531
Simon Wilson79d39652011-05-25 13:44:23 -0700532 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700533 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700534 sparams.period_step = 1;
535 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700536
537 if (!config->start_threshold)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700538 pcm->config.start_threshold = sparams.start_threshold =
539 config->period_count * config->period_size / 2;
John Grossman3bb114a2011-07-21 10:59:55 -0700540 else
541 sparams.start_threshold = config->start_threshold;
542
Liam Girdwood6be28f12011-10-13 12:59:51 -0700543 /* pick a high stop threshold - todo: does this need further tuning */
John Grossman3bb114a2011-07-21 10:59:55 -0700544 if (!config->stop_threshold)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700545 pcm->config.stop_threshold = sparams.stop_threshold =
Eric Laurentee9ba872011-11-15 19:04:03 -0800546 config->period_count * config->period_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700547 else
548 sparams.stop_threshold = config->stop_threshold;
549
Simon Wilson79d39652011-05-25 13:44:23 -0700550 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
551 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700552 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700553 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700554
Liam Girdwood6be28f12011-10-13 12:59:51 -0700555 while (pcm->boundary * 2 <= LONG_MAX - pcm->buffer_size)
556 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700557
558 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
559 oops(pcm, errno, "cannot set sw params");
560 goto fail;
561 }
562
Eric Laurent40b018e2011-06-18 10:10:23 -0700563 rc = pcm_hw_mmap_status(pcm);
564 if (rc < 0) {
565 oops(pcm, rc, "mmap status failed");
566 goto fail;
567 }
568
Simon Wilson79d39652011-05-25 13:44:23 -0700569 pcm->underruns = 0;
570 return pcm;
571
572fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700573 if (flags & PCM_MMAP)
574 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
575fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700576 close(pcm->fd);
577 pcm->fd = -1;
578 return pcm;
579}
580
581int pcm_is_ready(struct pcm *pcm)
582{
583 return pcm->fd >= 0;
584}
Simon Wilsond6458e62011-06-21 14:58:11 -0700585
586int pcm_start(struct pcm *pcm)
587{
588 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
589 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700590
591 if (pcm->flags & PCM_MMAP)
592 pcm_sync_ptr(pcm, 0);
593
Simon Wilsond6458e62011-06-21 14:58:11 -0700594 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
595 return oops(pcm, errno, "cannot start channel");
596
Liam Girdwood6be28f12011-10-13 12:59:51 -0700597 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700598 return 0;
599}
600
601int pcm_stop(struct pcm *pcm)
602{
603 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
604 return oops(pcm, errno, "cannot stop channel");
605
Liam Girdwood6be28f12011-10-13 12:59:51 -0700606 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700607 return 0;
608}
609
Liam Girdwood6be28f12011-10-13 12:59:51 -0700610static inline int pcm_mmap_playback_avail(struct pcm *pcm)
611{
612 int avail;
613
614 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
615
616 if (avail < 0)
617 avail += pcm->boundary;
618 else if (avail > (int)pcm->boundary)
619 avail -= pcm->boundary;
620
621 return avail;
622}
623
624static inline int pcm_mmap_capture_avail(struct pcm *pcm)
625{
626 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
627 if (avail < 0)
628 avail += pcm->boundary;
629 return avail;
630}
631
632static inline int pcm_mmap_avail(struct pcm *pcm)
633{
634 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
635 if (pcm->flags & PCM_IN)
636 return pcm_mmap_capture_avail(pcm);
637 else
638 return pcm_mmap_playback_avail(pcm);
639}
640
641static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
642{
643 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
644 appl_ptr += frames;
645
646 /* check for boundary wrap */
647 if (appl_ptr > pcm->boundary)
648 appl_ptr -= pcm->boundary;
649 pcm->mmap_control->appl_ptr = appl_ptr;
650}
651
652int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
653 unsigned int *frames)
654{
655 unsigned int continuous, copy_frames, avail;
656
657 /* return the mmap buffer */
658 *areas = pcm->mmap_buffer;
659
660 /* and the application offset in frames */
661 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
662
663 avail = pcm_mmap_avail(pcm);
664 if (avail > pcm->buffer_size)
665 avail = pcm->buffer_size;
666 continuous = pcm->buffer_size - *offset;
667
668 /* we can only copy frames if the are availabale and continuos */
669 copy_frames = *frames;
670 if (copy_frames > avail)
671 copy_frames = avail;
672 if (copy_frames > continuous)
673 copy_frames = continuous;
674 *frames = copy_frames;
675
676 return 0;
677}
678
679int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
680{
681 /* update the application pointer in userspace and kernel */
682 pcm_mmap_appl_forward(pcm, frames);
683 pcm_sync_ptr(pcm, 0);
684
685 return frames;
686}
687
688int pcm_avail_update(struct pcm *pcm)
689{
690 pcm_sync_ptr(pcm, 0);
691 return pcm_mmap_avail(pcm);
692}
693
694int pcm_state(struct pcm *pcm)
695{
696 int err = pcm_sync_ptr(pcm, 0);
697 if (err < 0)
698 return err;
699
700 return pcm->mmap_status->state;
701}
702
703int pcm_wait(struct pcm *pcm, int timeout)
704{
705 struct pollfd pfd;
706 unsigned short revents = 0;
707 int err;
708
709 pfd.fd = pcm->fd;
710 pfd.events = POLLOUT | POLLERR | POLLNVAL;
711
712 do {
713 /* let's wait for avail or timeout */
714 err = poll(&pfd, 1, timeout);
715 if (err < 0)
716 return -errno;
717
718 /* timeout ? */
719 if (err == 0)
720 return 0;
721
722 /* have we been interrupted ? */
723 if (errno == -EINTR)
724 continue;
725
726 /* check for any errors */
727 if (pfd.revents & (POLLERR | POLLNVAL)) {
728 switch (pcm_state(pcm)) {
729 case PCM_STATE_XRUN:
730 return -EPIPE;
731 case PCM_STATE_SUSPENDED:
732 return -ESTRPIPE;
733 case PCM_STATE_DISCONNECTED:
734 return -ENODEV;
735 default:
736 return -EIO;
737 }
738 }
739 /* poll again if fd not ready for IO */
740 } while (!(pfd.revents & (POLLIN | POLLOUT)));
741
742 return 1;
743}
744
745int pcm_mmap_write(struct pcm *pcm, void *buffer, unsigned int bytes)
746{
747 int err = 0, frames, avail;
748 unsigned int offset = 0, count;
749
750 if (bytes == 0)
751 return 0;
752
753 count = pcm_bytes_to_frames(pcm, bytes);
754
755 while (count > 0) {
756
757 /* get the available space for writing new frames */
758 avail = pcm_avail_update(pcm);
759 if (avail < 0) {
760 fprintf(stderr, "cannot determine available mmap frames");
761 return err;
762 }
763
764 /* start the audio if we reach the threshold */
765 if (!pcm->running &&
766 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
767 if (pcm_start(pcm) < 0) {
768 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
769 (unsigned int)pcm->mmap_status->hw_ptr,
770 (unsigned int)pcm->mmap_control->appl_ptr,
771 avail);
772 return -errno;
773 }
774 }
775
776 /* sleep until we have space to write new frames */
777 if (pcm->running &&
778 (unsigned int)avail < pcm->mmap_control->avail_min) {
779 int time = -1;
780
781 if (pcm->flags & PCM_NOIRQ)
782 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
783 / pcm->noirq_frames_per_msec;
784
785 err = pcm_wait(pcm, time);
786 if (err < 0) {
787 pcm->running = 0;
788 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
789 (unsigned int)pcm->mmap_status->hw_ptr,
790 (unsigned int)pcm->mmap_control->appl_ptr,
791 avail);
792 pcm->mmap_control->appl_ptr = 0;
793 return err;
794 }
795 continue;
796 }
797
798 frames = count;
799 if (frames > avail)
800 frames = avail;
801
802 if (!frames)
803 break;
804
805 /* copy frames from buffer */
806 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
807 if (frames < 0) {
808 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
809 (unsigned int)pcm->mmap_status->hw_ptr,
810 (unsigned int)pcm->mmap_control->appl_ptr,
811 avail);
812 return frames;
813 }
814
815 offset += frames;
816 count -= frames;
817 }
818
819_end:
820 return 0;
821}