blob: 4e9133acb3514ad05b111734075aeab0f736857f [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -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>
Simon Wilsone9942c82011-10-13 13:57:25 -070036#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Simon Wilsone9942c82011-10-13 13:57:25 -070041#include <limits.h>
Simon Wilsonedff7082011-06-06 15:33:34 -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
Simon Wilsone9942c82011-10-13 13:57:25 -070052#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilsonedff7082011-06-06 15:33:34 -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
Simon Wilsone9942c82011-10-13 13:57:25 -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 Wilsonedff7082011-06-06 15:33:34 -0700124static void param_init(struct snd_pcm_hw_params *p)
125{
126 int n;
127
128 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;
Simon Wilsone9942c82011-10-13 13:57:25 -0700151 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700152 char error[PCM_ERROR_MAX];
153 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700154 struct snd_pcm_mmap_status *mmap_status;
155 struct snd_pcm_mmap_control *mmap_control;
156 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700157 void *mmap_buffer;
158 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700159 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700160};
161
162unsigned int pcm_get_buffer_size(struct pcm *pcm)
163{
164 return pcm->buffer_size;
165}
166
167const char* pcm_get_error(struct pcm *pcm)
168{
169 return pcm->error;
170}
171
172static int oops(struct pcm *pcm, int e, const char *fmt, ...)
173{
174 va_list ap;
175 int sz;
176
177 va_start(ap, fmt);
178 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
179 va_end(ap);
180 sz = strlen(pcm->error);
181
182 if (errno)
183 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
184 ": %s", strerror(e));
185 return -1;
186}
187
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700188static unsigned int pcm_format_to_alsa(enum pcm_format format)
189{
190 switch (format) {
191 case PCM_FORMAT_S32_LE:
192 return SNDRV_PCM_FORMAT_S32_LE;
193 default:
194 case PCM_FORMAT_S16_LE:
195 return SNDRV_PCM_FORMAT_S16_LE;
196 };
197}
198
199static unsigned int pcm_format_to_bits(enum pcm_format format)
200{
201 switch (format) {
202 case PCM_FORMAT_S32_LE:
203 return 32;
204 default:
205 case PCM_FORMAT_S16_LE:
206 return 16;
207 };
208}
209
Simon Wilsone9942c82011-10-13 13:57:25 -0700210unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
211{
212 return bytes / (pcm->config.channels *
213 (pcm_format_to_bits(pcm->config.format) >> 3));
214}
215
216unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
217{
218 return frames * pcm->config.channels *
219 (pcm_format_to_bits(pcm->config.format) >> 3);
220}
221
Simon Wilsondd88f132011-07-25 10:58:30 -0700222static int pcm_sync_ptr(struct pcm *pcm, int flags) {
223 if (pcm->sync_ptr) {
224 pcm->sync_ptr->flags = flags;
225 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
226 return -1;
227 }
228 return 0;
229}
230
231static int pcm_hw_mmap_status(struct pcm *pcm) {
232
233 if (pcm->sync_ptr)
234 return 0;
235
236 int page_size = sysconf(_SC_PAGE_SIZE);
237 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
238 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
239 if (pcm->mmap_status == MAP_FAILED)
240 pcm->mmap_status = NULL;
241 if (!pcm->mmap_status)
242 goto mmap_error;
243
244 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
245 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
246 if (pcm->mmap_control == MAP_FAILED)
247 pcm->mmap_control = NULL;
248 if (!pcm->mmap_control) {
249 munmap(pcm->mmap_status, page_size);
250 pcm->mmap_status = NULL;
251 goto mmap_error;
252 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700253 if (pcm->flags & PCM_MMAP)
254 pcm->mmap_control->avail_min = pcm->config.avail_min;
255 else
256 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700257
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;
Eric Laurent73b9c672011-10-14 11:12:24 -0700267 if (pcm->flags & PCM_MMAP)
268 pcm->mmap_control->avail_min = pcm->config.avail_min;
269 else
270 pcm->mmap_control->avail_min = 1;
271
Simon Wilsondd88f132011-07-25 10:58:30 -0700272 pcm_sync_ptr(pcm, 0);
273
274 return 0;
275}
276
277static void pcm_hw_munmap_status(struct pcm *pcm) {
278 if (pcm->sync_ptr) {
279 free(pcm->sync_ptr);
280 pcm->sync_ptr = NULL;
281 } else {
282 int page_size = sysconf(_SC_PAGE_SIZE);
283 if (pcm->mmap_status)
284 munmap(pcm->mmap_status, page_size);
285 if (pcm->mmap_control)
286 munmap(pcm->mmap_control, page_size);
287 }
288 pcm->mmap_status = NULL;
289 pcm->mmap_control = NULL;
290}
291
Simon Wilsone9942c82011-10-13 13:57:25 -0700292static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
293 const char *src, unsigned int src_offset,
294 unsigned int frames)
295{
296 int size_bytes = pcm_frames_to_bytes(pcm, frames);
297 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
298 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
299
300 /* interleaved only atm */
301 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
302 src + src_offset_bytes, size_bytes);
303 return 0;
304}
305
Simon Wilsondaa83292012-02-28 15:26:02 -0800306static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
Simon Wilsone9942c82011-10-13 13:57:25 -0700307 unsigned int offset, unsigned int size)
308{
309 void *pcm_areas;
310 int commit;
311 unsigned int pcm_offset, frames, count = 0;
312
313 while (size > 0) {
314 frames = size;
315 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
316 pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
317 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
318 if (commit < 0) {
319 oops(pcm, commit, "failed to commit %d frames\n", frames);
320 return commit;
321 }
322
323 offset += commit;
324 count += commit;
325 size -= commit;
326 }
327 return count;
328}
329
Simon Wilsondd88f132011-07-25 10:58:30 -0700330int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
331 struct timespec *tstamp)
332{
333 int frames;
334 int rc;
335 snd_pcm_uframes_t hw_ptr;
336
337 if (!pcm_is_ready(pcm))
338 return -1;
339
340 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
341 if (rc < 0)
342 return -1;
343
Simon Wilson8dd366f2011-11-17 13:16:52 -0800344 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
345 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800346 return -1;
347
Simon Wilsondd88f132011-07-25 10:58:30 -0700348 *tstamp = pcm->mmap_status->tstamp;
349 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
350 return -1;
351
352 hw_ptr = pcm->mmap_status->hw_ptr;
353 if (pcm->flags & PCM_IN)
354 frames = hw_ptr - pcm->mmap_control->appl_ptr;
355 else
356 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
357
358 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700359 frames += pcm->boundary;
360 else if (frames > (int)pcm->boundary)
361 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700362
363 *avail = (unsigned int)frames;
364
365 return 0;
366}
367
Simon Wilsondaa83292012-02-28 15:26:02 -0800368int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700369{
370 struct snd_xferi x;
371
372 if (pcm->flags & PCM_IN)
373 return -EINVAL;
374
Simon Wilsondaa83292012-02-28 15:26:02 -0800375 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700376 x.frames = count / (pcm->config.channels *
377 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700378
379 for (;;) {
380 if (!pcm->running) {
381 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
382 return oops(pcm, errno, "cannot prepare channel");
383 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
384 return oops(pcm, errno, "cannot write initial data");
385 pcm->running = 1;
386 return 0;
387 }
388 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
389 pcm->running = 0;
390 if (errno == EPIPE) {
John Grossman673253a2012-04-03 16:37:38 -0700391 /* we failed to make our window -- try to restart if we are
392 * allowed to do so. Otherwise, simply allow the EPIPE error to
393 * propagate up to the app level */
Simon Wilsonedff7082011-06-06 15:33:34 -0700394 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700395 if (pcm->flags & PCM_NORESTART)
396 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700397 continue;
398 }
399 return oops(pcm, errno, "cannot write stream data");
400 }
401 return 0;
402 }
403}
404
405int pcm_read(struct pcm *pcm, void *data, unsigned int count)
406{
407 struct snd_xferi x;
408
409 if (!(pcm->flags & PCM_IN))
410 return -EINVAL;
411
412 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700413 x.frames = count / (pcm->config.channels *
414 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700415
416 for (;;) {
417 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700418 if (pcm_start(pcm) < 0) {
419 fprintf(stderr, "start error");
420 return -errno;
421 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700422 }
423 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
424 pcm->running = 0;
425 if (errno == EPIPE) {
426 /* we failed to make our window -- try to restart */
427 pcm->underruns++;
428 continue;
429 }
430 return oops(pcm, errno, "cannot read stream data");
431 }
432 return 0;
433 }
434}
435
436static struct pcm bad_pcm = {
437 .fd = -1,
438};
439
440int pcm_close(struct pcm *pcm)
441{
442 if (pcm == &bad_pcm)
443 return 0;
444
Simon Wilsondd88f132011-07-25 10:58:30 -0700445 pcm_hw_munmap_status(pcm);
446
Simon Wilsone9942c82011-10-13 13:57:25 -0700447 if (pcm->flags & PCM_MMAP) {
448 pcm_stop(pcm);
449 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
450 }
451
Simon Wilsonedff7082011-06-06 15:33:34 -0700452 if (pcm->fd >= 0)
453 close(pcm->fd);
454 pcm->running = 0;
455 pcm->buffer_size = 0;
456 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700457 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700458 return 0;
459}
460
Simon Wilsonedff7082011-06-06 15:33:34 -0700461struct pcm *pcm_open(unsigned int card, unsigned int device,
462 unsigned int flags, struct pcm_config *config)
463{
464 struct pcm *pcm;
465 struct snd_pcm_info info;
466 struct snd_pcm_hw_params params;
467 struct snd_pcm_sw_params sparams;
468 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700469 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700470
471 pcm = calloc(1, sizeof(struct pcm));
472 if (!pcm || !config)
473 return &bad_pcm; /* TODO: could support default config here */
474
475 pcm->config = *config;
476
477 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
478 flags & PCM_IN ? 'c' : 'p');
479
480 pcm->flags = flags;
481 pcm->fd = open(fn, O_RDWR);
482 if (pcm->fd < 0) {
483 oops(pcm, errno, "cannot open device '%s'", fn);
484 return pcm;
485 }
486
487 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
488 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700489 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700490 }
491
492 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700493 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
494 pcm_format_to_alsa(config->format));
495 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
496 SNDRV_PCM_SUBFORMAT_STD);
497 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
498 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
499 pcm_format_to_bits(config->format));
500 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
501 pcm_format_to_bits(config->format) * config->channels);
502 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
503 config->channels);
504 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
505 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
506
Simon Wilsone9942c82011-10-13 13:57:25 -0700507 if (flags & PCM_NOIRQ) {
508
509 if (!(flags & PCM_MMAP)) {
510 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
511 goto fail;
512 }
513
514 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
515 pcm->noirq_frames_per_msec = config->rate / 1000;
516 }
517
518 if (flags & PCM_MMAP)
519 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
520 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
521 else
522 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
523 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
524
Simon Wilsonedff7082011-06-06 15:33:34 -0700525 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
526 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700527 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700528 }
529
Simon Wilsone9942c82011-10-13 13:57:25 -0700530 /* get our refined hw_params */
531 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
532 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
533 pcm->buffer_size = config->period_count * config->period_size;
534
535 if (flags & PCM_MMAP) {
536 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
537 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
538 if (pcm->mmap_buffer == MAP_FAILED) {
539 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
540 pcm_frames_to_bytes(pcm, pcm->buffer_size));
541 goto fail_close;
542 }
543 }
544
545
Simon Wilsonedff7082011-06-06 15:33:34 -0700546 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700547 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700548 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700549
550 if (!config->start_threshold)
Simon Wilsone9942c82011-10-13 13:57:25 -0700551 pcm->config.start_threshold = sparams.start_threshold =
552 config->period_count * config->period_size / 2;
Simon Wilsonc1239622011-07-27 15:08:34 -0700553 else
554 sparams.start_threshold = config->start_threshold;
555
Simon Wilsone9942c82011-10-13 13:57:25 -0700556 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800557 if (!config->stop_threshold) {
558 if (pcm->flags & PCM_IN)
559 pcm->config.stop_threshold = sparams.stop_threshold =
560 config->period_count * config->period_size * 10;
561 else
562 pcm->config.stop_threshold = sparams.stop_threshold =
563 config->period_count * config->period_size;
564 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700565 else
566 sparams.stop_threshold = config->stop_threshold;
567
Eric Laurent73b9c672011-10-14 11:12:24 -0700568 if (!pcm->config.avail_min) {
569 if (pcm->flags & PCM_MMAP)
570 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
571 else
572 pcm->config.avail_min = sparams.avail_min = 1;
573 } else
574 sparams.avail_min = config->avail_min;
575
Simon Wilsonedff7082011-06-06 15:33:34 -0700576 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
577 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700578 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700579 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700580
Simon Wilsondaa83292012-02-28 15:26:02 -0800581 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Simon Wilsone9942c82011-10-13 13:57:25 -0700582 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700583
584 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
585 oops(pcm, errno, "cannot set sw params");
586 goto fail;
587 }
588
Simon Wilsondd88f132011-07-25 10:58:30 -0700589 rc = pcm_hw_mmap_status(pcm);
590 if (rc < 0) {
591 oops(pcm, rc, "mmap status failed");
592 goto fail;
593 }
594
Simon Wilsonedff7082011-06-06 15:33:34 -0700595 pcm->underruns = 0;
596 return pcm;
597
598fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700599 if (flags & PCM_MMAP)
600 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
601fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700602 close(pcm->fd);
603 pcm->fd = -1;
604 return pcm;
605}
606
607int pcm_is_ready(struct pcm *pcm)
608{
609 return pcm->fd >= 0;
610}
Simon Wilson70d77082011-06-24 11:08:10 -0700611
612int pcm_start(struct pcm *pcm)
613{
614 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
615 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -0700616
617 if (pcm->flags & PCM_MMAP)
618 pcm_sync_ptr(pcm, 0);
619
Simon Wilson70d77082011-06-24 11:08:10 -0700620 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
621 return oops(pcm, errno, "cannot start channel");
622
Simon Wilsone9942c82011-10-13 13:57:25 -0700623 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -0700624 return 0;
625}
626
627int pcm_stop(struct pcm *pcm)
628{
629 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
630 return oops(pcm, errno, "cannot stop channel");
631
Simon Wilsone9942c82011-10-13 13:57:25 -0700632 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -0700633 return 0;
634}
635
Simon Wilsone9942c82011-10-13 13:57:25 -0700636static inline int pcm_mmap_playback_avail(struct pcm *pcm)
637{
638 int avail;
639
640 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
641
642 if (avail < 0)
643 avail += pcm->boundary;
644 else if (avail > (int)pcm->boundary)
645 avail -= pcm->boundary;
646
647 return avail;
648}
649
650static inline int pcm_mmap_capture_avail(struct pcm *pcm)
651{
652 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
653 if (avail < 0)
654 avail += pcm->boundary;
655 return avail;
656}
657
658static inline int pcm_mmap_avail(struct pcm *pcm)
659{
660 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
661 if (pcm->flags & PCM_IN)
662 return pcm_mmap_capture_avail(pcm);
663 else
664 return pcm_mmap_playback_avail(pcm);
665}
666
667static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
668{
669 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
670 appl_ptr += frames;
671
672 /* check for boundary wrap */
673 if (appl_ptr > pcm->boundary)
674 appl_ptr -= pcm->boundary;
675 pcm->mmap_control->appl_ptr = appl_ptr;
676}
677
678int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
679 unsigned int *frames)
680{
681 unsigned int continuous, copy_frames, avail;
682
683 /* return the mmap buffer */
684 *areas = pcm->mmap_buffer;
685
686 /* and the application offset in frames */
687 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
688
689 avail = pcm_mmap_avail(pcm);
690 if (avail > pcm->buffer_size)
691 avail = pcm->buffer_size;
692 continuous = pcm->buffer_size - *offset;
693
694 /* we can only copy frames if the are availabale and continuos */
695 copy_frames = *frames;
696 if (copy_frames > avail)
697 copy_frames = avail;
698 if (copy_frames > continuous)
699 copy_frames = continuous;
700 *frames = copy_frames;
701
702 return 0;
703}
704
705int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
706{
707 /* update the application pointer in userspace and kernel */
708 pcm_mmap_appl_forward(pcm, frames);
709 pcm_sync_ptr(pcm, 0);
710
711 return frames;
712}
713
714int pcm_avail_update(struct pcm *pcm)
715{
716 pcm_sync_ptr(pcm, 0);
717 return pcm_mmap_avail(pcm);
718}
719
720int pcm_state(struct pcm *pcm)
721{
722 int err = pcm_sync_ptr(pcm, 0);
723 if (err < 0)
724 return err;
725
726 return pcm->mmap_status->state;
727}
728
Eric Laurent73b9c672011-10-14 11:12:24 -0700729int pcm_set_avail_min(struct pcm *pcm, int avail_min)
730{
731 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
732 return -ENOSYS;
733
734 pcm->config.avail_min = avail_min;
735 return 0;
736}
737
Simon Wilsone9942c82011-10-13 13:57:25 -0700738int pcm_wait(struct pcm *pcm, int timeout)
739{
740 struct pollfd pfd;
741 unsigned short revents = 0;
742 int err;
743
744 pfd.fd = pcm->fd;
745 pfd.events = POLLOUT | POLLERR | POLLNVAL;
746
747 do {
748 /* let's wait for avail or timeout */
749 err = poll(&pfd, 1, timeout);
750 if (err < 0)
751 return -errno;
752
753 /* timeout ? */
754 if (err == 0)
755 return 0;
756
757 /* have we been interrupted ? */
758 if (errno == -EINTR)
759 continue;
760
761 /* check for any errors */
762 if (pfd.revents & (POLLERR | POLLNVAL)) {
763 switch (pcm_state(pcm)) {
764 case PCM_STATE_XRUN:
765 return -EPIPE;
766 case PCM_STATE_SUSPENDED:
767 return -ESTRPIPE;
768 case PCM_STATE_DISCONNECTED:
769 return -ENODEV;
770 default:
771 return -EIO;
772 }
773 }
774 /* poll again if fd not ready for IO */
775 } while (!(pfd.revents & (POLLIN | POLLOUT)));
776
777 return 1;
778}
779
Simon Wilsondaa83292012-02-28 15:26:02 -0800780int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -0700781{
782 int err = 0, frames, avail;
783 unsigned int offset = 0, count;
784
785 if (bytes == 0)
786 return 0;
787
788 count = pcm_bytes_to_frames(pcm, bytes);
789
790 while (count > 0) {
791
792 /* get the available space for writing new frames */
793 avail = pcm_avail_update(pcm);
794 if (avail < 0) {
795 fprintf(stderr, "cannot determine available mmap frames");
796 return err;
797 }
798
799 /* start the audio if we reach the threshold */
800 if (!pcm->running &&
801 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
802 if (pcm_start(pcm) < 0) {
803 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
804 (unsigned int)pcm->mmap_status->hw_ptr,
805 (unsigned int)pcm->mmap_control->appl_ptr,
806 avail);
807 return -errno;
808 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700809 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -0700810 }
811
812 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -0700813 if (pcm->running) {
814 /* enable waiting for avail_min threshold when less frames than we have to write
815 * are available. */
816 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
817 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700818
Eric Laurent73b9c672011-10-14 11:12:24 -0700819 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
820 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700821
Eric Laurent73b9c672011-10-14 11:12:24 -0700822 /* disable waiting for avail_min threshold to allow small amounts of data to be
823 * written without waiting as long as there is enough room in buffer. */
824 pcm->wait_for_avail_min = 0;
825
826 if (pcm->flags & PCM_NOIRQ)
827 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
828
829 err = pcm_wait(pcm, time);
830 if (err < 0) {
831 pcm->running = 0;
832 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
833 (unsigned int)pcm->mmap_status->hw_ptr,
834 (unsigned int)pcm->mmap_control->appl_ptr,
835 avail);
836 pcm->mmap_control->appl_ptr = 0;
837 return err;
838 }
839 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -0700840 }
Simon Wilsone9942c82011-10-13 13:57:25 -0700841 }
842
843 frames = count;
844 if (frames > avail)
845 frames = avail;
846
847 if (!frames)
848 break;
849
850 /* copy frames from buffer */
851 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
852 if (frames < 0) {
853 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
854 (unsigned int)pcm->mmap_status->hw_ptr,
855 (unsigned int)pcm->mmap_control->appl_ptr,
856 avail);
857 return frames;
858 }
859
860 offset += frames;
861 count -= frames;
862 }
863
864_end:
865 return 0;
866}