blob: 5ec68ec1831ac3bd5f3d341c284ea4f86134f192 [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
Andy Hung70530a62014-03-26 18:29:07 -070054/* Logs information into a string; follows snprintf() in that
55 * offset may be greater than size, and though no characters are copied
56 * into string, characters are still counted into offset. */
57#define STRLOG(string, offset, size, ...) \
58 do { int temp, clipoffset = offset > size ? size : offset; \
59 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
60 if (temp > 0) offset += temp; } while (0)
61
62#ifndef ARRAY_SIZE
63#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64#endif
65
66/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
67static const char * const access_lookup[] = {
68 "MMAP_INTERLEAVED",
69 "MMAP_NONINTERLEAVED",
70 "MMAP_COMPLEX",
71 "RW_INTERLEAVED",
72 "RW_NONINTERLEAVED",
73};
74
75/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
76static const char * const format_lookup[] = {
77 /*[0] =*/ "S8",
78 "U8",
79 "S16_LE",
80 "S16_BE",
81 "U16_LE",
82 "U16_BE",
83 "S24_LE",
84 "S24_BE",
85 "U24_LE",
86 "U24_BE",
87 "S32_LE",
88 "S32_BE",
89 "U32_LE",
90 "U32_BE",
91 "FLOAT_LE",
92 "FLOAT_BE",
93 "FLOAT64_LE",
94 "FLOAT64_BE",
95 "IEC958_SUBFRAME_LE",
96 "IEC958_SUBFRAME_BE",
97 "MU_LAW",
98 "A_LAW",
99 "IMA_ADPCM",
100 "MPEG",
101 /*[24] =*/ "GSM",
102 /* gap */
103 [31] = "SPECIAL",
104 "S24_3LE",
105 "S24_3BE",
106 "U24_3LE",
107 "U24_3BE",
108 "S20_3LE",
109 "S20_3BE",
110 "U20_3LE",
111 "U20_3BE",
112 "S18_3LE",
113 "S18_3BE",
114 "U18_3LE",
115 /*[43] =*/ "U18_3BE",
116#if 0
117 /* recent additions, may not be present on local asound.h */
118 "G723_24",
119 "G723_24_1B",
120 "G723_40",
121 "G723_40_1B",
122 "DSD_U8",
123 "DSD_U16_LE",
124#endif
125};
126
127/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
128static const char * const subformat_lookup[] = {
129 "STD",
130};
131
Simon Wilsonedff7082011-06-06 15:33:34 -0700132static inline int param_is_mask(int p)
133{
134 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
135 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
136}
137
138static inline int param_is_interval(int p)
139{
140 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
141 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
142}
143
144static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
145{
146 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
147}
148
149static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
150{
151 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
152}
153
154static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
155{
156 if (bit >= SNDRV_MASK_MAX)
157 return;
158 if (param_is_mask(n)) {
159 struct snd_mask *m = param_to_mask(p, n);
160 m->bits[0] = 0;
161 m->bits[1] = 0;
162 m->bits[bit >> 5] |= (1 << (bit & 31));
163 }
164}
165
166static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
167{
168 if (param_is_interval(n)) {
169 struct snd_interval *i = param_to_interval(p, n);
170 i->min = val;
171 }
172}
173
Simon Wilson42fc2d32012-12-03 11:18:57 -0800174static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
175{
176 if (param_is_interval(n)) {
177 struct snd_interval *i = param_to_interval(p, n);
178 return i->min;
179 }
180 return 0;
181}
182
183static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
184{
185 if (param_is_interval(n)) {
186 struct snd_interval *i = param_to_interval(p, n);
187 return i->max;
188 }
189 return 0;
190}
191
Simon Wilsonedff7082011-06-06 15:33:34 -0700192static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
193{
194 if (param_is_interval(n)) {
195 struct snd_interval *i = param_to_interval(p, n);
196 i->min = val;
197 i->max = val;
198 i->integer = 1;
199 }
200}
201
Simon Wilsone9942c82011-10-13 13:57:25 -0700202static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
203{
204 if (param_is_interval(n)) {
205 struct snd_interval *i = param_to_interval(p, n);
206 if (i->integer)
207 return i->max;
208 }
209 return 0;
210}
211
Simon Wilsonedff7082011-06-06 15:33:34 -0700212static void param_init(struct snd_pcm_hw_params *p)
213{
214 int n;
215
216 memset(p, 0, sizeof(*p));
217 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
218 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
219 struct snd_mask *m = param_to_mask(p, n);
220 m->bits[0] = ~0;
221 m->bits[1] = ~0;
222 }
223 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
224 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
225 struct snd_interval *i = param_to_interval(p, n);
226 i->min = 0;
227 i->max = ~0;
228 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800229 p->rmask = ~0U;
230 p->cmask = 0;
231 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700232}
233
234#define PCM_ERROR_MAX 128
235
236struct pcm {
237 int fd;
238 unsigned int flags;
239 int running:1;
240 int underruns;
241 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700242 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700243 char error[PCM_ERROR_MAX];
244 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700245 struct snd_pcm_mmap_status *mmap_status;
246 struct snd_pcm_mmap_control *mmap_control;
247 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700248 void *mmap_buffer;
249 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700250 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700251};
252
253unsigned int pcm_get_buffer_size(struct pcm *pcm)
254{
255 return pcm->buffer_size;
256}
257
258const char* pcm_get_error(struct pcm *pcm)
259{
260 return pcm->error;
261}
262
263static int oops(struct pcm *pcm, int e, const char *fmt, ...)
264{
265 va_list ap;
266 int sz;
267
268 va_start(ap, fmt);
269 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
270 va_end(ap);
271 sz = strlen(pcm->error);
272
273 if (errno)
274 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
275 ": %s", strerror(e));
276 return -1;
277}
278
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700279static unsigned int pcm_format_to_alsa(enum pcm_format format)
280{
281 switch (format) {
282 case PCM_FORMAT_S32_LE:
283 return SNDRV_PCM_FORMAT_S32_LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800284 case PCM_FORMAT_S8:
285 return SNDRV_PCM_FORMAT_S8;
Glenn Kastend9837d02014-01-31 07:56:33 -0800286 case PCM_FORMAT_S24_3LE:
287 return SNDRV_PCM_FORMAT_S24_3LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800288 case PCM_FORMAT_S24_LE:
289 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700290 default:
291 case PCM_FORMAT_S16_LE:
292 return SNDRV_PCM_FORMAT_S16_LE;
293 };
294}
295
Simon Wilson36ea2d82013-07-17 11:10:45 -0700296unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700297{
298 switch (format) {
299 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700300 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700301 return 32;
Glenn Kastend9837d02014-01-31 07:56:33 -0800302 case PCM_FORMAT_S24_3LE:
303 return 24;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700304 default:
305 case PCM_FORMAT_S16_LE:
306 return 16;
307 };
308}
309
Simon Wilsone9942c82011-10-13 13:57:25 -0700310unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
311{
312 return bytes / (pcm->config.channels *
313 (pcm_format_to_bits(pcm->config.format) >> 3));
314}
315
316unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
317{
318 return frames * pcm->config.channels *
319 (pcm_format_to_bits(pcm->config.format) >> 3);
320}
321
Simon Wilsondd88f132011-07-25 10:58:30 -0700322static int pcm_sync_ptr(struct pcm *pcm, int flags) {
323 if (pcm->sync_ptr) {
324 pcm->sync_ptr->flags = flags;
325 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
326 return -1;
327 }
328 return 0;
329}
330
331static int pcm_hw_mmap_status(struct pcm *pcm) {
332
333 if (pcm->sync_ptr)
334 return 0;
335
336 int page_size = sysconf(_SC_PAGE_SIZE);
337 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
338 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
339 if (pcm->mmap_status == MAP_FAILED)
340 pcm->mmap_status = NULL;
341 if (!pcm->mmap_status)
342 goto mmap_error;
343
344 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
345 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
346 if (pcm->mmap_control == MAP_FAILED)
347 pcm->mmap_control = NULL;
348 if (!pcm->mmap_control) {
349 munmap(pcm->mmap_status, page_size);
350 pcm->mmap_status = NULL;
351 goto mmap_error;
352 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700353 if (pcm->flags & PCM_MMAP)
354 pcm->mmap_control->avail_min = pcm->config.avail_min;
355 else
356 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700357
358 return 0;
359
360mmap_error:
361
362 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
363 if (!pcm->sync_ptr)
364 return -ENOMEM;
365 pcm->mmap_status = &pcm->sync_ptr->s.status;
366 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent73b9c672011-10-14 11:12:24 -0700367 if (pcm->flags & PCM_MMAP)
368 pcm->mmap_control->avail_min = pcm->config.avail_min;
369 else
370 pcm->mmap_control->avail_min = 1;
371
Simon Wilsondd88f132011-07-25 10:58:30 -0700372 pcm_sync_ptr(pcm, 0);
373
374 return 0;
375}
376
377static void pcm_hw_munmap_status(struct pcm *pcm) {
378 if (pcm->sync_ptr) {
379 free(pcm->sync_ptr);
380 pcm->sync_ptr = NULL;
381 } else {
382 int page_size = sysconf(_SC_PAGE_SIZE);
383 if (pcm->mmap_status)
384 munmap(pcm->mmap_status, page_size);
385 if (pcm->mmap_control)
386 munmap(pcm->mmap_control, page_size);
387 }
388 pcm->mmap_status = NULL;
389 pcm->mmap_control = NULL;
390}
391
Simon Wilsone9942c82011-10-13 13:57:25 -0700392static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentc98da792013-09-16 14:31:17 -0700393 char *buf, unsigned int src_offset,
Simon Wilsone9942c82011-10-13 13:57:25 -0700394 unsigned int frames)
395{
396 int size_bytes = pcm_frames_to_bytes(pcm, frames);
397 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
398 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
399
400 /* interleaved only atm */
Eric Laurentc98da792013-09-16 14:31:17 -0700401 if (pcm->flags & PCM_IN)
402 memcpy(buf + src_offset_bytes,
403 (char*)pcm->mmap_buffer + pcm_offset_bytes,
404 size_bytes);
405 else
406 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
407 buf + src_offset_bytes,
408 size_bytes);
Simon Wilsone9942c82011-10-13 13:57:25 -0700409 return 0;
410}
411
Eric Laurentc98da792013-09-16 14:31:17 -0700412static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Simon Wilsone9942c82011-10-13 13:57:25 -0700413 unsigned int offset, unsigned int size)
414{
415 void *pcm_areas;
416 int commit;
417 unsigned int pcm_offset, frames, count = 0;
418
419 while (size > 0) {
420 frames = size;
421 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentc98da792013-09-16 14:31:17 -0700422 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -0700423 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
424 if (commit < 0) {
425 oops(pcm, commit, "failed to commit %d frames\n", frames);
426 return commit;
427 }
428
429 offset += commit;
430 count += commit;
431 size -= commit;
432 }
433 return count;
434}
435
Simon Wilsondd88f132011-07-25 10:58:30 -0700436int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
437 struct timespec *tstamp)
438{
439 int frames;
440 int rc;
441 snd_pcm_uframes_t hw_ptr;
442
443 if (!pcm_is_ready(pcm))
444 return -1;
445
446 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
447 if (rc < 0)
448 return -1;
449
Simon Wilson8dd366f2011-11-17 13:16:52 -0800450 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
451 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800452 return -1;
453
Simon Wilsondd88f132011-07-25 10:58:30 -0700454 *tstamp = pcm->mmap_status->tstamp;
455 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
456 return -1;
457
458 hw_ptr = pcm->mmap_status->hw_ptr;
459 if (pcm->flags & PCM_IN)
460 frames = hw_ptr - pcm->mmap_control->appl_ptr;
461 else
462 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
463
464 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700465 frames += pcm->boundary;
466 else if (frames > (int)pcm->boundary)
467 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700468
469 *avail = (unsigned int)frames;
470
471 return 0;
472}
473
Simon Wilsondaa83292012-02-28 15:26:02 -0800474int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700475{
476 struct snd_xferi x;
477
478 if (pcm->flags & PCM_IN)
479 return -EINVAL;
480
Simon Wilsondaa83292012-02-28 15:26:02 -0800481 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700482 x.frames = count / (pcm->config.channels *
483 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700484
485 for (;;) {
486 if (!pcm->running) {
487 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
488 return oops(pcm, errno, "cannot prepare channel");
489 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
490 return oops(pcm, errno, "cannot write initial data");
491 pcm->running = 1;
492 return 0;
493 }
494 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
495 pcm->running = 0;
496 if (errno == EPIPE) {
John Grossman673253a2012-04-03 16:37:38 -0700497 /* we failed to make our window -- try to restart if we are
498 * allowed to do so. Otherwise, simply allow the EPIPE error to
499 * propagate up to the app level */
Simon Wilsonedff7082011-06-06 15:33:34 -0700500 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700501 if (pcm->flags & PCM_NORESTART)
502 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700503 continue;
504 }
505 return oops(pcm, errno, "cannot write stream data");
506 }
507 return 0;
508 }
509}
510
511int pcm_read(struct pcm *pcm, void *data, unsigned int count)
512{
513 struct snd_xferi x;
514
515 if (!(pcm->flags & PCM_IN))
516 return -EINVAL;
517
518 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700519 x.frames = count / (pcm->config.channels *
520 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700521
522 for (;;) {
523 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700524 if (pcm_start(pcm) < 0) {
525 fprintf(stderr, "start error");
526 return -errno;
527 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700528 }
529 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
530 pcm->running = 0;
531 if (errno == EPIPE) {
532 /* we failed to make our window -- try to restart */
533 pcm->underruns++;
534 continue;
535 }
536 return oops(pcm, errno, "cannot read stream data");
537 }
538 return 0;
539 }
540}
541
542static struct pcm bad_pcm = {
543 .fd = -1,
544};
545
Simon Wilson42fc2d32012-12-03 11:18:57 -0800546struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
547 unsigned int flags)
548{
549 struct snd_pcm_hw_params *params;
550 char fn[256];
551 int fd;
552
553 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
554 flags & PCM_IN ? 'c' : 'p');
555
556 fd = open(fn, O_RDWR);
557 if (fd < 0) {
558 fprintf(stderr, "cannot open device '%s'\n", fn);
559 goto err_open;
560 }
561
562 params = calloc(1, sizeof(struct snd_pcm_hw_params));
563 if (!params)
564 goto err_calloc;
565
566 param_init(params);
567 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
568 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
569 goto err_hw_refine;
570 }
571
572 close(fd);
573
574 return (struct pcm_params *)params;
575
576err_hw_refine:
577 free(params);
578err_calloc:
579 close(fd);
580err_open:
581 return NULL;
582}
583
584void pcm_params_free(struct pcm_params *pcm_params)
585{
586 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
587
588 if (params)
589 free(params);
590}
591
592static int pcm_param_to_alsa(enum pcm_param param)
593{
594 switch (param) {
Andy Hunga5b44d92014-03-10 18:08:15 -0700595 case PCM_PARAM_ACCESS:
596 return SNDRV_PCM_HW_PARAM_ACCESS;
597 case PCM_PARAM_FORMAT:
598 return SNDRV_PCM_HW_PARAM_FORMAT;
599 case PCM_PARAM_SUBFORMAT:
600 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800601 case PCM_PARAM_SAMPLE_BITS:
602 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
603 break;
604 case PCM_PARAM_FRAME_BITS:
605 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
606 break;
607 case PCM_PARAM_CHANNELS:
608 return SNDRV_PCM_HW_PARAM_CHANNELS;
609 break;
610 case PCM_PARAM_RATE:
611 return SNDRV_PCM_HW_PARAM_RATE;
612 break;
613 case PCM_PARAM_PERIOD_TIME:
614 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
615 break;
616 case PCM_PARAM_PERIOD_SIZE:
617 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
618 break;
619 case PCM_PARAM_PERIOD_BYTES:
620 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
621 break;
622 case PCM_PARAM_PERIODS:
623 return SNDRV_PCM_HW_PARAM_PERIODS;
624 break;
625 case PCM_PARAM_BUFFER_TIME:
626 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
627 break;
628 case PCM_PARAM_BUFFER_SIZE:
629 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
630 break;
631 case PCM_PARAM_BUFFER_BYTES:
632 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
633 break;
634 case PCM_PARAM_TICK_TIME:
635 return SNDRV_PCM_HW_PARAM_TICK_TIME;
636 break;
637
638 default:
639 return -1;
640 }
641}
642
Andy Hunga5b44d92014-03-10 18:08:15 -0700643struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
644 enum pcm_param param)
645{
646 int p;
647 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
648 if (params == NULL) {
649 return NULL;
650 }
651
652 p = pcm_param_to_alsa(param);
653 if (p < 0 || !param_is_mask(p)) {
654 return NULL;
655 }
656
657 return (struct pcm_mask *)param_to_mask(params, p);
658}
659
Simon Wilson42fc2d32012-12-03 11:18:57 -0800660unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
661 enum pcm_param param)
662{
663 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
664 int p;
665
666 if (!params)
667 return 0;
668
669 p = pcm_param_to_alsa(param);
670 if (p < 0)
671 return 0;
672
673 return param_get_min(params, p);
674}
675
676unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
677 enum pcm_param param)
678{
679 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
680 int p;
681
682 if (!params)
683 return 0;
684
685 p = pcm_param_to_alsa(param);
686 if (p < 0)
687 return 0;
688
689 return param_get_max(params, p);
690}
691
Andy Hung70530a62014-03-26 18:29:07 -0700692static int pcm_mask_test(struct pcm_mask *m, unsigned int index)
693{
694 const unsigned int bitshift = 5; /* for 32 bit integer */
695 const unsigned int bitmask = (1 << bitshift) - 1;
696 unsigned int element;
697
698 element = index >> bitshift;
699 if (element >= ARRAY_SIZE(m->bits))
700 return 0; /* for safety, but should never occur */
701 return (m->bits[element] >> (index & bitmask)) & 1;
702}
703
704static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size,
705 char *mask_name,
706 const char * const *bit_array_name, size_t bit_array_size)
707{
708 unsigned int i;
709 unsigned int offset = 0;
710
711 if (m == NULL)
712 return 0;
713 if (bit_array_size < 32) {
714 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
715 } else { /* spans two or more bitfields, print with an array index */
716 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
717 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
718 mask_name, i, m->bits[i]);
719 }
720 }
721 for (i = 0; i < bit_array_size; ++i) {
722 if (pcm_mask_test(m, i)) {
723 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
724 }
725 }
726 return offset;
727}
728
729int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
730{
731 struct pcm_mask *m;
732 unsigned int min, max;
733 unsigned int clipoffset, offset;
734
735 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
736 offset = pcm_mask_to_string(m, string, size,
737 "Access", access_lookup, ARRAY_SIZE(access_lookup));
738 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
739 clipoffset = offset > size ? size : offset;
740 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
741 "Format", format_lookup, ARRAY_SIZE(format_lookup));
742 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
743 clipoffset = offset > size ? size : offset;
744 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
745 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
746 min = pcm_params_get_min(params, PCM_PARAM_RATE);
747 max = pcm_params_get_max(params, PCM_PARAM_RATE);
748 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
749 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
750 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
751 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
752 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
753 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
754 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
755 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
756 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
757 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
758 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
759 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
760 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
761 return offset;
762}
763
764int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
765{
766 unsigned int alsa_format = pcm_format_to_alsa(format);
767
768 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
769 return 0; /* caution: format not recognized is equivalent to S16_LE */
770 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
771}
772
Simon Wilsonedff7082011-06-06 15:33:34 -0700773int pcm_close(struct pcm *pcm)
774{
775 if (pcm == &bad_pcm)
776 return 0;
777
Simon Wilsondd88f132011-07-25 10:58:30 -0700778 pcm_hw_munmap_status(pcm);
779
Simon Wilsone9942c82011-10-13 13:57:25 -0700780 if (pcm->flags & PCM_MMAP) {
781 pcm_stop(pcm);
782 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
783 }
784
Simon Wilsonedff7082011-06-06 15:33:34 -0700785 if (pcm->fd >= 0)
786 close(pcm->fd);
787 pcm->running = 0;
788 pcm->buffer_size = 0;
789 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700790 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700791 return 0;
792}
793
Simon Wilsonedff7082011-06-06 15:33:34 -0700794struct pcm *pcm_open(unsigned int card, unsigned int device,
795 unsigned int flags, struct pcm_config *config)
796{
797 struct pcm *pcm;
798 struct snd_pcm_info info;
799 struct snd_pcm_hw_params params;
800 struct snd_pcm_sw_params sparams;
801 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700802 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700803
804 pcm = calloc(1, sizeof(struct pcm));
805 if (!pcm || !config)
806 return &bad_pcm; /* TODO: could support default config here */
807
808 pcm->config = *config;
809
810 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
811 flags & PCM_IN ? 'c' : 'p');
812
813 pcm->flags = flags;
814 pcm->fd = open(fn, O_RDWR);
815 if (pcm->fd < 0) {
816 oops(pcm, errno, "cannot open device '%s'", fn);
817 return pcm;
818 }
819
820 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
821 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700822 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700823 }
824
825 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700826 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
827 pcm_format_to_alsa(config->format));
828 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
829 SNDRV_PCM_SUBFORMAT_STD);
830 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
831 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
832 pcm_format_to_bits(config->format));
833 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
834 pcm_format_to_bits(config->format) * config->channels);
835 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
836 config->channels);
837 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
838 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
839
Simon Wilsone9942c82011-10-13 13:57:25 -0700840 if (flags & PCM_NOIRQ) {
841
842 if (!(flags & PCM_MMAP)) {
843 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
844 goto fail;
845 }
846
847 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
848 pcm->noirq_frames_per_msec = config->rate / 1000;
849 }
850
851 if (flags & PCM_MMAP)
852 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
853 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
854 else
855 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
856 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
857
Simon Wilsonedff7082011-06-06 15:33:34 -0700858 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
859 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700860 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700861 }
862
Simon Wilsone9942c82011-10-13 13:57:25 -0700863 /* get our refined hw_params */
864 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
865 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
866 pcm->buffer_size = config->period_count * config->period_size;
867
868 if (flags & PCM_MMAP) {
869 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
870 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
871 if (pcm->mmap_buffer == MAP_FAILED) {
872 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
873 pcm_frames_to_bytes(pcm, pcm->buffer_size));
874 goto fail_close;
875 }
876 }
877
878
Simon Wilsonedff7082011-06-06 15:33:34 -0700879 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700880 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700881 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700882
Eric Laurentff2e5422012-08-22 16:18:14 -0700883 if (!config->start_threshold) {
884 if (pcm->flags & PCM_IN)
885 pcm->config.start_threshold = sparams.start_threshold = 1;
886 else
887 pcm->config.start_threshold = sparams.start_threshold =
888 config->period_count * config->period_size / 2;
889 } else
Simon Wilsonc1239622011-07-27 15:08:34 -0700890 sparams.start_threshold = config->start_threshold;
891
Simon Wilsone9942c82011-10-13 13:57:25 -0700892 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800893 if (!config->stop_threshold) {
894 if (pcm->flags & PCM_IN)
895 pcm->config.stop_threshold = sparams.stop_threshold =
896 config->period_count * config->period_size * 10;
897 else
898 pcm->config.stop_threshold = sparams.stop_threshold =
899 config->period_count * config->period_size;
900 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700901 else
902 sparams.stop_threshold = config->stop_threshold;
903
Eric Laurent73b9c672011-10-14 11:12:24 -0700904 if (!pcm->config.avail_min) {
905 if (pcm->flags & PCM_MMAP)
906 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
907 else
908 pcm->config.avail_min = sparams.avail_min = 1;
909 } else
910 sparams.avail_min = config->avail_min;
911
Simon Wilsonedff7082011-06-06 15:33:34 -0700912 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
913 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700914 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700915 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700916
Simon Wilsondaa83292012-02-28 15:26:02 -0800917 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Simon Wilsone9942c82011-10-13 13:57:25 -0700918 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700919
920 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
921 oops(pcm, errno, "cannot set sw params");
922 goto fail;
923 }
924
Simon Wilsondd88f132011-07-25 10:58:30 -0700925 rc = pcm_hw_mmap_status(pcm);
926 if (rc < 0) {
927 oops(pcm, rc, "mmap status failed");
928 goto fail;
929 }
930
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700931#ifdef SNDRV_PCM_IOCTL_TTSTAMP
932 if (pcm->flags & PCM_MONOTONIC) {
933 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
934 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
935 if (rc < 0) {
936 oops(pcm, rc, "cannot set timestamp type");
937 goto fail;
938 }
939 }
940#endif
941
Simon Wilsonedff7082011-06-06 15:33:34 -0700942 pcm->underruns = 0;
943 return pcm;
944
945fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700946 if (flags & PCM_MMAP)
947 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
948fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700949 close(pcm->fd);
950 pcm->fd = -1;
951 return pcm;
952}
953
954int pcm_is_ready(struct pcm *pcm)
955{
956 return pcm->fd >= 0;
957}
Simon Wilson70d77082011-06-24 11:08:10 -0700958
959int pcm_start(struct pcm *pcm)
960{
961 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
962 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -0700963
964 if (pcm->flags & PCM_MMAP)
965 pcm_sync_ptr(pcm, 0);
966
Simon Wilson70d77082011-06-24 11:08:10 -0700967 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
968 return oops(pcm, errno, "cannot start channel");
969
Simon Wilsone9942c82011-10-13 13:57:25 -0700970 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -0700971 return 0;
972}
973
974int pcm_stop(struct pcm *pcm)
975{
976 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
977 return oops(pcm, errno, "cannot stop channel");
978
Simon Wilsone9942c82011-10-13 13:57:25 -0700979 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -0700980 return 0;
981}
982
Simon Wilsone9942c82011-10-13 13:57:25 -0700983static inline int pcm_mmap_playback_avail(struct pcm *pcm)
984{
985 int avail;
986
987 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
988
989 if (avail < 0)
990 avail += pcm->boundary;
991 else if (avail > (int)pcm->boundary)
992 avail -= pcm->boundary;
993
994 return avail;
995}
996
997static inline int pcm_mmap_capture_avail(struct pcm *pcm)
998{
999 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1000 if (avail < 0)
1001 avail += pcm->boundary;
1002 return avail;
1003}
1004
1005static inline int pcm_mmap_avail(struct pcm *pcm)
1006{
1007 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1008 if (pcm->flags & PCM_IN)
1009 return pcm_mmap_capture_avail(pcm);
1010 else
1011 return pcm_mmap_playback_avail(pcm);
1012}
1013
1014static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1015{
1016 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1017 appl_ptr += frames;
1018
1019 /* check for boundary wrap */
1020 if (appl_ptr > pcm->boundary)
1021 appl_ptr -= pcm->boundary;
1022 pcm->mmap_control->appl_ptr = appl_ptr;
1023}
1024
1025int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1026 unsigned int *frames)
1027{
1028 unsigned int continuous, copy_frames, avail;
1029
1030 /* return the mmap buffer */
1031 *areas = pcm->mmap_buffer;
1032
1033 /* and the application offset in frames */
1034 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1035
1036 avail = pcm_mmap_avail(pcm);
1037 if (avail > pcm->buffer_size)
1038 avail = pcm->buffer_size;
1039 continuous = pcm->buffer_size - *offset;
1040
1041 /* we can only copy frames if the are availabale and continuos */
1042 copy_frames = *frames;
1043 if (copy_frames > avail)
1044 copy_frames = avail;
1045 if (copy_frames > continuous)
1046 copy_frames = continuous;
1047 *frames = copy_frames;
1048
1049 return 0;
1050}
1051
1052int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1053{
1054 /* update the application pointer in userspace and kernel */
1055 pcm_mmap_appl_forward(pcm, frames);
1056 pcm_sync_ptr(pcm, 0);
1057
1058 return frames;
1059}
1060
1061int pcm_avail_update(struct pcm *pcm)
1062{
1063 pcm_sync_ptr(pcm, 0);
1064 return pcm_mmap_avail(pcm);
1065}
1066
1067int pcm_state(struct pcm *pcm)
1068{
1069 int err = pcm_sync_ptr(pcm, 0);
1070 if (err < 0)
1071 return err;
1072
1073 return pcm->mmap_status->state;
1074}
1075
Eric Laurent73b9c672011-10-14 11:12:24 -07001076int pcm_set_avail_min(struct pcm *pcm, int avail_min)
1077{
1078 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
1079 return -ENOSYS;
1080
1081 pcm->config.avail_min = avail_min;
1082 return 0;
1083}
1084
Simon Wilsone9942c82011-10-13 13:57:25 -07001085int pcm_wait(struct pcm *pcm, int timeout)
1086{
1087 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -07001088 int err;
1089
1090 pfd.fd = pcm->fd;
1091 pfd.events = POLLOUT | POLLERR | POLLNVAL;
1092
1093 do {
1094 /* let's wait for avail or timeout */
1095 err = poll(&pfd, 1, timeout);
1096 if (err < 0)
1097 return -errno;
1098
1099 /* timeout ? */
1100 if (err == 0)
1101 return 0;
1102
1103 /* have we been interrupted ? */
1104 if (errno == -EINTR)
1105 continue;
1106
1107 /* check for any errors */
1108 if (pfd.revents & (POLLERR | POLLNVAL)) {
1109 switch (pcm_state(pcm)) {
1110 case PCM_STATE_XRUN:
1111 return -EPIPE;
1112 case PCM_STATE_SUSPENDED:
1113 return -ESTRPIPE;
1114 case PCM_STATE_DISCONNECTED:
1115 return -ENODEV;
1116 default:
1117 return -EIO;
1118 }
1119 }
1120 /* poll again if fd not ready for IO */
1121 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1122
1123 return 1;
1124}
1125
Eric Laurentc98da792013-09-16 14:31:17 -07001126int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -07001127{
1128 int err = 0, frames, avail;
1129 unsigned int offset = 0, count;
1130
1131 if (bytes == 0)
1132 return 0;
1133
1134 count = pcm_bytes_to_frames(pcm, bytes);
1135
1136 while (count > 0) {
1137
1138 /* get the available space for writing new frames */
1139 avail = pcm_avail_update(pcm);
1140 if (avail < 0) {
1141 fprintf(stderr, "cannot determine available mmap frames");
1142 return err;
1143 }
1144
1145 /* start the audio if we reach the threshold */
1146 if (!pcm->running &&
1147 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1148 if (pcm_start(pcm) < 0) {
1149 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1150 (unsigned int)pcm->mmap_status->hw_ptr,
1151 (unsigned int)pcm->mmap_control->appl_ptr,
1152 avail);
1153 return -errno;
1154 }
Eric Laurent73b9c672011-10-14 11:12:24 -07001155 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -07001156 }
1157
1158 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -07001159 if (pcm->running) {
1160 /* enable waiting for avail_min threshold when less frames than we have to write
1161 * are available. */
1162 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1163 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001164
Eric Laurent73b9c672011-10-14 11:12:24 -07001165 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1166 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001167
Eric Laurent73b9c672011-10-14 11:12:24 -07001168 /* disable waiting for avail_min threshold to allow small amounts of data to be
1169 * written without waiting as long as there is enough room in buffer. */
1170 pcm->wait_for_avail_min = 0;
1171
1172 if (pcm->flags & PCM_NOIRQ)
1173 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1174
1175 err = pcm_wait(pcm, time);
1176 if (err < 0) {
1177 pcm->running = 0;
1178 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1179 (unsigned int)pcm->mmap_status->hw_ptr,
1180 (unsigned int)pcm->mmap_control->appl_ptr,
1181 avail);
1182 pcm->mmap_control->appl_ptr = 0;
1183 return err;
1184 }
1185 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -07001186 }
Simon Wilsone9942c82011-10-13 13:57:25 -07001187 }
1188
1189 frames = count;
1190 if (frames > avail)
1191 frames = avail;
1192
1193 if (!frames)
1194 break;
1195
1196 /* copy frames from buffer */
Eric Laurentc98da792013-09-16 14:31:17 -07001197 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -07001198 if (frames < 0) {
1199 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1200 (unsigned int)pcm->mmap_status->hw_ptr,
1201 (unsigned int)pcm->mmap_control->appl_ptr,
1202 avail);
1203 return frames;
1204 }
1205
1206 offset += frames;
1207 count -= frames;
1208 }
1209
Simon Wilsone9942c82011-10-13 13:57:25 -07001210 return 0;
1211}
Eric Laurentc98da792013-09-16 14:31:17 -07001212
1213int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1214{
1215 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1216 return -ENOSYS;
1217
1218 return pcm_mmap_transfer(pcm, (void *)data, count);
1219}
1220
1221int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1222{
1223 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1224 return -ENOSYS;
1225
1226 return pcm_mmap_transfer(pcm, data, count);
1227}