blob: 612d06850b7f04f00434b847d25d171c915f7608 [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
52
Andy Hung70530a62014-03-26 18:29:07 -070053/* Logs information into a string; follows snprintf() in that
54 * offset may be greater than size, and though no characters are copied
55 * into string, characters are still counted into offset. */
56#define STRLOG(string, offset, size, ...) \
57 do { int temp, clipoffset = offset > size ? size : offset; \
58 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
59 if (temp > 0) offset += temp; } while (0)
60
61#ifndef ARRAY_SIZE
62#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
63#endif
64
65/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
66static const char * const access_lookup[] = {
67 "MMAP_INTERLEAVED",
68 "MMAP_NONINTERLEAVED",
69 "MMAP_COMPLEX",
70 "RW_INTERLEAVED",
71 "RW_NONINTERLEAVED",
72};
73
74/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
75static const char * const format_lookup[] = {
76 /*[0] =*/ "S8",
77 "U8",
78 "S16_LE",
79 "S16_BE",
80 "U16_LE",
81 "U16_BE",
82 "S24_LE",
83 "S24_BE",
84 "U24_LE",
85 "U24_BE",
86 "S32_LE",
87 "S32_BE",
88 "U32_LE",
89 "U32_BE",
90 "FLOAT_LE",
91 "FLOAT_BE",
92 "FLOAT64_LE",
93 "FLOAT64_BE",
94 "IEC958_SUBFRAME_LE",
95 "IEC958_SUBFRAME_BE",
96 "MU_LAW",
97 "A_LAW",
98 "IMA_ADPCM",
99 "MPEG",
100 /*[24] =*/ "GSM",
101 /* gap */
102 [31] = "SPECIAL",
103 "S24_3LE",
104 "S24_3BE",
105 "U24_3LE",
106 "U24_3BE",
107 "S20_3LE",
108 "S20_3BE",
109 "U20_3LE",
110 "U20_3BE",
111 "S18_3LE",
112 "S18_3BE",
113 "U18_3LE",
114 /*[43] =*/ "U18_3BE",
115#if 0
116 /* recent additions, may not be present on local asound.h */
117 "G723_24",
118 "G723_24_1B",
119 "G723_40",
120 "G723_40_1B",
121 "DSD_U8",
122 "DSD_U16_LE",
123#endif
124};
125
126/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
127static const char * const subformat_lookup[] = {
128 "STD",
129};
130
Simon Wilsonedff7082011-06-06 15:33:34 -0700131static inline int param_is_mask(int p)
132{
133 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
134 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
135}
136
137static inline int param_is_interval(int p)
138{
139 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
140 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
141}
142
143static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
144{
145 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
146}
147
148static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
149{
150 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
151}
152
153static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
154{
155 if (bit >= SNDRV_MASK_MAX)
156 return;
157 if (param_is_mask(n)) {
158 struct snd_mask *m = param_to_mask(p, n);
159 m->bits[0] = 0;
160 m->bits[1] = 0;
161 m->bits[bit >> 5] |= (1 << (bit & 31));
162 }
163}
164
165static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
166{
167 if (param_is_interval(n)) {
168 struct snd_interval *i = param_to_interval(p, n);
169 i->min = val;
170 }
171}
172
Simon Wilson42fc2d32012-12-03 11:18:57 -0800173static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
174{
175 if (param_is_interval(n)) {
176 struct snd_interval *i = param_to_interval(p, n);
177 return i->min;
178 }
179 return 0;
180}
181
Paul McLeanb25ece42014-03-21 15:27:34 -0700182static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
183{
184 if (param_is_interval(n)) {
185 struct snd_interval *i = param_to_interval(p, n);
186 i->max = val;
187 }
188}
189
Simon Wilson42fc2d32012-12-03 11:18:57 -0800190static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
191{
192 if (param_is_interval(n)) {
193 struct snd_interval *i = param_to_interval(p, n);
194 return i->max;
195 }
196 return 0;
197}
198
Simon Wilsonedff7082011-06-06 15:33:34 -0700199static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
200{
201 if (param_is_interval(n)) {
202 struct snd_interval *i = param_to_interval(p, n);
203 i->min = val;
204 i->max = val;
205 i->integer = 1;
206 }
207}
208
Simon Wilsone9942c82011-10-13 13:57:25 -0700209static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
210{
211 if (param_is_interval(n)) {
212 struct snd_interval *i = param_to_interval(p, n);
213 if (i->integer)
214 return i->max;
215 }
216 return 0;
217}
218
Simon Wilsonedff7082011-06-06 15:33:34 -0700219static void param_init(struct snd_pcm_hw_params *p)
220{
221 int n;
222
223 memset(p, 0, sizeof(*p));
224 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
225 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
226 struct snd_mask *m = param_to_mask(p, n);
227 m->bits[0] = ~0;
228 m->bits[1] = ~0;
229 }
230 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
231 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
232 struct snd_interval *i = param_to_interval(p, n);
233 i->min = 0;
234 i->max = ~0;
235 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800236 p->rmask = ~0U;
237 p->cmask = 0;
238 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700239}
240
241#define PCM_ERROR_MAX 128
242
243struct pcm {
244 int fd;
245 unsigned int flags;
246 int running:1;
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530247 int prepared:1;
Simon Wilsonedff7082011-06-06 15:33:34 -0700248 int underruns;
249 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700250 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700251 char error[PCM_ERROR_MAX];
252 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700253 struct snd_pcm_mmap_status *mmap_status;
254 struct snd_pcm_mmap_control *mmap_control;
255 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700256 void *mmap_buffer;
257 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700258 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700259};
260
261unsigned int pcm_get_buffer_size(struct pcm *pcm)
262{
263 return pcm->buffer_size;
264}
265
266const char* pcm_get_error(struct pcm *pcm)
267{
268 return pcm->error;
269}
270
271static int oops(struct pcm *pcm, int e, const char *fmt, ...)
272{
273 va_list ap;
274 int sz;
275
276 va_start(ap, fmt);
277 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
278 va_end(ap);
279 sz = strlen(pcm->error);
280
281 if (errno)
282 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
283 ": %s", strerror(e));
284 return -1;
285}
286
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700287static unsigned int pcm_format_to_alsa(enum pcm_format format)
288{
289 switch (format) {
290 case PCM_FORMAT_S32_LE:
291 return SNDRV_PCM_FORMAT_S32_LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800292 case PCM_FORMAT_S8:
293 return SNDRV_PCM_FORMAT_S8;
Glenn Kastend9837d02014-01-31 07:56:33 -0800294 case PCM_FORMAT_S24_3LE:
295 return SNDRV_PCM_FORMAT_S24_3LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800296 case PCM_FORMAT_S24_LE:
297 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700298 default:
299 case PCM_FORMAT_S16_LE:
300 return SNDRV_PCM_FORMAT_S16_LE;
301 };
302}
303
Simon Wilson36ea2d82013-07-17 11:10:45 -0700304unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700305{
306 switch (format) {
307 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700308 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700309 return 32;
Glenn Kastend9837d02014-01-31 07:56:33 -0800310 case PCM_FORMAT_S24_3LE:
311 return 24;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700312 default:
313 case PCM_FORMAT_S16_LE:
314 return 16;
315 };
316}
317
Simon Wilsone9942c82011-10-13 13:57:25 -0700318unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
319{
320 return bytes / (pcm->config.channels *
321 (pcm_format_to_bits(pcm->config.format) >> 3));
322}
323
324unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
325{
326 return frames * pcm->config.channels *
327 (pcm_format_to_bits(pcm->config.format) >> 3);
328}
329
Simon Wilsondd88f132011-07-25 10:58:30 -0700330static int pcm_sync_ptr(struct pcm *pcm, int flags) {
331 if (pcm->sync_ptr) {
332 pcm->sync_ptr->flags = flags;
333 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
334 return -1;
335 }
336 return 0;
337}
338
339static int pcm_hw_mmap_status(struct pcm *pcm) {
340
341 if (pcm->sync_ptr)
342 return 0;
343
344 int page_size = sysconf(_SC_PAGE_SIZE);
345 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
346 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
347 if (pcm->mmap_status == MAP_FAILED)
348 pcm->mmap_status = NULL;
349 if (!pcm->mmap_status)
350 goto mmap_error;
351
352 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
353 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
354 if (pcm->mmap_control == MAP_FAILED)
355 pcm->mmap_control = NULL;
356 if (!pcm->mmap_control) {
357 munmap(pcm->mmap_status, page_size);
358 pcm->mmap_status = NULL;
359 goto mmap_error;
360 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700361 if (pcm->flags & PCM_MMAP)
362 pcm->mmap_control->avail_min = pcm->config.avail_min;
363 else
364 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700365
366 return 0;
367
368mmap_error:
369
370 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
371 if (!pcm->sync_ptr)
372 return -ENOMEM;
373 pcm->mmap_status = &pcm->sync_ptr->s.status;
374 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent73b9c672011-10-14 11:12:24 -0700375 if (pcm->flags & PCM_MMAP)
376 pcm->mmap_control->avail_min = pcm->config.avail_min;
377 else
378 pcm->mmap_control->avail_min = 1;
379
Simon Wilsondd88f132011-07-25 10:58:30 -0700380 pcm_sync_ptr(pcm, 0);
381
382 return 0;
383}
384
385static void pcm_hw_munmap_status(struct pcm *pcm) {
386 if (pcm->sync_ptr) {
387 free(pcm->sync_ptr);
388 pcm->sync_ptr = NULL;
389 } else {
390 int page_size = sysconf(_SC_PAGE_SIZE);
391 if (pcm->mmap_status)
392 munmap(pcm->mmap_status, page_size);
393 if (pcm->mmap_control)
394 munmap(pcm->mmap_control, page_size);
395 }
396 pcm->mmap_status = NULL;
397 pcm->mmap_control = NULL;
398}
399
Simon Wilsone9942c82011-10-13 13:57:25 -0700400static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentc98da792013-09-16 14:31:17 -0700401 char *buf, unsigned int src_offset,
Simon Wilsone9942c82011-10-13 13:57:25 -0700402 unsigned int frames)
403{
404 int size_bytes = pcm_frames_to_bytes(pcm, frames);
405 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
406 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
407
408 /* interleaved only atm */
Eric Laurentc98da792013-09-16 14:31:17 -0700409 if (pcm->flags & PCM_IN)
410 memcpy(buf + src_offset_bytes,
411 (char*)pcm->mmap_buffer + pcm_offset_bytes,
412 size_bytes);
413 else
414 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
415 buf + src_offset_bytes,
416 size_bytes);
Simon Wilsone9942c82011-10-13 13:57:25 -0700417 return 0;
418}
419
Eric Laurentc98da792013-09-16 14:31:17 -0700420static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Simon Wilsone9942c82011-10-13 13:57:25 -0700421 unsigned int offset, unsigned int size)
422{
423 void *pcm_areas;
424 int commit;
425 unsigned int pcm_offset, frames, count = 0;
426
427 while (size > 0) {
428 frames = size;
429 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentc98da792013-09-16 14:31:17 -0700430 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -0700431 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
432 if (commit < 0) {
433 oops(pcm, commit, "failed to commit %d frames\n", frames);
434 return commit;
435 }
436
437 offset += commit;
438 count += commit;
439 size -= commit;
440 }
441 return count;
442}
443
Simon Wilsondd88f132011-07-25 10:58:30 -0700444int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
445 struct timespec *tstamp)
446{
447 int frames;
448 int rc;
449 snd_pcm_uframes_t hw_ptr;
450
451 if (!pcm_is_ready(pcm))
452 return -1;
453
454 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
455 if (rc < 0)
456 return -1;
457
Simon Wilson8dd366f2011-11-17 13:16:52 -0800458 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
459 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800460 return -1;
461
Simon Wilsondd88f132011-07-25 10:58:30 -0700462 *tstamp = pcm->mmap_status->tstamp;
463 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
464 return -1;
465
466 hw_ptr = pcm->mmap_status->hw_ptr;
467 if (pcm->flags & PCM_IN)
468 frames = hw_ptr - pcm->mmap_control->appl_ptr;
469 else
470 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
471
472 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700473 frames += pcm->boundary;
474 else if (frames > (int)pcm->boundary)
475 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700476
477 *avail = (unsigned int)frames;
478
479 return 0;
480}
481
Simon Wilsondaa83292012-02-28 15:26:02 -0800482int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700483{
484 struct snd_xferi x;
485
486 if (pcm->flags & PCM_IN)
487 return -EINVAL;
488
Simon Wilsondaa83292012-02-28 15:26:02 -0800489 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700490 x.frames = count / (pcm->config.channels *
491 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700492
493 for (;;) {
494 if (!pcm->running) {
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530495 int prepare_error = pcm_prepare(pcm);
496 if (prepare_error)
497 return prepare_error;
Simon Wilsonedff7082011-06-06 15:33:34 -0700498 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
499 return oops(pcm, errno, "cannot write initial data");
500 pcm->running = 1;
501 return 0;
502 }
503 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530504 pcm->prepared = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700505 pcm->running = 0;
506 if (errno == EPIPE) {
John Grossman673253a2012-04-03 16:37:38 -0700507 /* we failed to make our window -- try to restart if we are
508 * allowed to do so. Otherwise, simply allow the EPIPE error to
509 * propagate up to the app level */
Simon Wilsonedff7082011-06-06 15:33:34 -0700510 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700511 if (pcm->flags & PCM_NORESTART)
512 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700513 continue;
514 }
515 return oops(pcm, errno, "cannot write stream data");
516 }
517 return 0;
518 }
519}
520
521int pcm_read(struct pcm *pcm, void *data, unsigned int count)
522{
523 struct snd_xferi x;
524
525 if (!(pcm->flags & PCM_IN))
526 return -EINVAL;
527
528 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700529 x.frames = count / (pcm->config.channels *
530 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700531
532 for (;;) {
533 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700534 if (pcm_start(pcm) < 0) {
535 fprintf(stderr, "start error");
536 return -errno;
537 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700538 }
539 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530540 pcm->prepared = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700541 pcm->running = 0;
542 if (errno == EPIPE) {
543 /* we failed to make our window -- try to restart */
544 pcm->underruns++;
545 continue;
546 }
547 return oops(pcm, errno, "cannot read stream data");
548 }
549 return 0;
550 }
551}
552
553static struct pcm bad_pcm = {
554 .fd = -1,
555};
556
Simon Wilson42fc2d32012-12-03 11:18:57 -0800557struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
558 unsigned int flags)
559{
560 struct snd_pcm_hw_params *params;
561 char fn[256];
562 int fd;
563
564 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
565 flags & PCM_IN ? 'c' : 'p');
566
567 fd = open(fn, O_RDWR);
568 if (fd < 0) {
569 fprintf(stderr, "cannot open device '%s'\n", fn);
570 goto err_open;
571 }
572
573 params = calloc(1, sizeof(struct snd_pcm_hw_params));
574 if (!params)
575 goto err_calloc;
576
577 param_init(params);
578 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
579 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
580 goto err_hw_refine;
581 }
582
583 close(fd);
584
585 return (struct pcm_params *)params;
586
587err_hw_refine:
588 free(params);
589err_calloc:
590 close(fd);
591err_open:
592 return NULL;
593}
594
595void pcm_params_free(struct pcm_params *pcm_params)
596{
597 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
598
599 if (params)
600 free(params);
601}
602
603static int pcm_param_to_alsa(enum pcm_param param)
604{
605 switch (param) {
Andy Hunga5b44d92014-03-10 18:08:15 -0700606 case PCM_PARAM_ACCESS:
607 return SNDRV_PCM_HW_PARAM_ACCESS;
608 case PCM_PARAM_FORMAT:
609 return SNDRV_PCM_HW_PARAM_FORMAT;
610 case PCM_PARAM_SUBFORMAT:
611 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800612 case PCM_PARAM_SAMPLE_BITS:
613 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
614 break;
615 case PCM_PARAM_FRAME_BITS:
616 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
617 break;
618 case PCM_PARAM_CHANNELS:
619 return SNDRV_PCM_HW_PARAM_CHANNELS;
620 break;
621 case PCM_PARAM_RATE:
622 return SNDRV_PCM_HW_PARAM_RATE;
623 break;
624 case PCM_PARAM_PERIOD_TIME:
625 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
626 break;
627 case PCM_PARAM_PERIOD_SIZE:
628 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
629 break;
630 case PCM_PARAM_PERIOD_BYTES:
631 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
632 break;
633 case PCM_PARAM_PERIODS:
634 return SNDRV_PCM_HW_PARAM_PERIODS;
635 break;
636 case PCM_PARAM_BUFFER_TIME:
637 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
638 break;
639 case PCM_PARAM_BUFFER_SIZE:
640 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
641 break;
642 case PCM_PARAM_BUFFER_BYTES:
643 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
644 break;
645 case PCM_PARAM_TICK_TIME:
646 return SNDRV_PCM_HW_PARAM_TICK_TIME;
647 break;
648
649 default:
650 return -1;
651 }
652}
653
Andy Hunga5b44d92014-03-10 18:08:15 -0700654struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
655 enum pcm_param param)
656{
657 int p;
658 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
659 if (params == NULL) {
660 return NULL;
661 }
662
663 p = pcm_param_to_alsa(param);
664 if (p < 0 || !param_is_mask(p)) {
665 return NULL;
666 }
667
668 return (struct pcm_mask *)param_to_mask(params, p);
669}
670
Simon Wilson42fc2d32012-12-03 11:18:57 -0800671unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
672 enum pcm_param param)
673{
674 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
675 int p;
676
677 if (!params)
678 return 0;
679
680 p = pcm_param_to_alsa(param);
681 if (p < 0)
682 return 0;
683
684 return param_get_min(params, p);
685}
686
Paul McLeanb25ece42014-03-21 15:27:34 -0700687void pcm_params_set_min(struct pcm_params *pcm_params,
688 enum pcm_param param, unsigned int val)
689{
690 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
691 int p;
692
693 if (!params)
694 return;
695
696 p = pcm_param_to_alsa(param);
697 if (p < 0)
698 return;
699
700 param_set_min(params, p, val);
701}
702
Simon Wilson42fc2d32012-12-03 11:18:57 -0800703unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
704 enum pcm_param param)
705{
706 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
707 int p;
708
709 if (!params)
710 return 0;
711
712 p = pcm_param_to_alsa(param);
713 if (p < 0)
714 return 0;
715
716 return param_get_max(params, p);
717}
718
Paul McLeanb25ece42014-03-21 15:27:34 -0700719void pcm_params_set_max(struct pcm_params *pcm_params,
720 enum pcm_param param, unsigned int val)
721{
722 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
723 int p;
724
725 if (!params)
726 return;
727
728 p = pcm_param_to_alsa(param);
729 if (p < 0)
730 return;
731
732 param_set_max(params, p, val);
733}
734
Andy Hung70530a62014-03-26 18:29:07 -0700735static int pcm_mask_test(struct pcm_mask *m, unsigned int index)
736{
737 const unsigned int bitshift = 5; /* for 32 bit integer */
738 const unsigned int bitmask = (1 << bitshift) - 1;
739 unsigned int element;
740
741 element = index >> bitshift;
742 if (element >= ARRAY_SIZE(m->bits))
743 return 0; /* for safety, but should never occur */
744 return (m->bits[element] >> (index & bitmask)) & 1;
745}
746
747static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size,
748 char *mask_name,
749 const char * const *bit_array_name, size_t bit_array_size)
750{
751 unsigned int i;
752 unsigned int offset = 0;
753
754 if (m == NULL)
755 return 0;
756 if (bit_array_size < 32) {
757 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
758 } else { /* spans two or more bitfields, print with an array index */
759 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
760 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
761 mask_name, i, m->bits[i]);
762 }
763 }
764 for (i = 0; i < bit_array_size; ++i) {
765 if (pcm_mask_test(m, i)) {
766 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
767 }
768 }
769 return offset;
770}
771
772int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
773{
774 struct pcm_mask *m;
775 unsigned int min, max;
776 unsigned int clipoffset, offset;
777
778 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
779 offset = pcm_mask_to_string(m, string, size,
780 "Access", access_lookup, ARRAY_SIZE(access_lookup));
781 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
782 clipoffset = offset > size ? size : offset;
783 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
784 "Format", format_lookup, ARRAY_SIZE(format_lookup));
785 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
786 clipoffset = offset > size ? size : offset;
787 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
788 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
789 min = pcm_params_get_min(params, PCM_PARAM_RATE);
790 max = pcm_params_get_max(params, PCM_PARAM_RATE);
791 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
792 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
793 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
794 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
795 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
796 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
797 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
798 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
799 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
800 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
801 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
802 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
803 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
804 return offset;
805}
806
807int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
808{
809 unsigned int alsa_format = pcm_format_to_alsa(format);
810
811 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
812 return 0; /* caution: format not recognized is equivalent to S16_LE */
813 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
814}
815
Simon Wilsonedff7082011-06-06 15:33:34 -0700816int pcm_close(struct pcm *pcm)
817{
818 if (pcm == &bad_pcm)
819 return 0;
820
Simon Wilsondd88f132011-07-25 10:58:30 -0700821 pcm_hw_munmap_status(pcm);
822
Simon Wilsone9942c82011-10-13 13:57:25 -0700823 if (pcm->flags & PCM_MMAP) {
824 pcm_stop(pcm);
825 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
826 }
827
Simon Wilsonedff7082011-06-06 15:33:34 -0700828 if (pcm->fd >= 0)
829 close(pcm->fd);
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530830 pcm->prepared = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700831 pcm->running = 0;
832 pcm->buffer_size = 0;
833 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700834 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700835 return 0;
836}
837
Simon Wilsonedff7082011-06-06 15:33:34 -0700838struct pcm *pcm_open(unsigned int card, unsigned int device,
839 unsigned int flags, struct pcm_config *config)
840{
841 struct pcm *pcm;
842 struct snd_pcm_info info;
843 struct snd_pcm_hw_params params;
844 struct snd_pcm_sw_params sparams;
845 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700846 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700847
848 pcm = calloc(1, sizeof(struct pcm));
849 if (!pcm || !config)
850 return &bad_pcm; /* TODO: could support default config here */
851
852 pcm->config = *config;
853
854 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
855 flags & PCM_IN ? 'c' : 'p');
856
857 pcm->flags = flags;
vivek mehta51f47ff2016-07-15 13:12:52 -0700858 pcm->fd = open(fn, O_RDWR|O_NONBLOCK);
Simon Wilsonedff7082011-06-06 15:33:34 -0700859 if (pcm->fd < 0) {
860 oops(pcm, errno, "cannot open device '%s'", fn);
861 return pcm;
862 }
863
vivek mehta51f47ff2016-07-15 13:12:52 -0700864 if (fcntl(pcm->fd, F_SETFL, fcntl(pcm->fd, F_GETFL) &
865 ~O_NONBLOCK) < 0) {
866 oops(pcm, errno, "failed to reset blocking mode '%s'", fn);
867 goto fail_close;
868 }
869
Simon Wilsonedff7082011-06-06 15:33:34 -0700870 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
871 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700872 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700873 }
874
875 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700876 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
877 pcm_format_to_alsa(config->format));
878 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
879 SNDRV_PCM_SUBFORMAT_STD);
880 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
881 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
882 pcm_format_to_bits(config->format));
883 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
884 pcm_format_to_bits(config->format) * config->channels);
885 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
886 config->channels);
887 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
888 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
889
Simon Wilsone9942c82011-10-13 13:57:25 -0700890 if (flags & PCM_NOIRQ) {
Simon Wilsone9942c82011-10-13 13:57:25 -0700891 if (!(flags & PCM_MMAP)) {
892 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
vivek mehta51f47ff2016-07-15 13:12:52 -0700893 goto fail_close;
Simon Wilsone9942c82011-10-13 13:57:25 -0700894 }
895
896 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
897 pcm->noirq_frames_per_msec = config->rate / 1000;
898 }
899
900 if (flags & PCM_MMAP)
901 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
Paul McLeanb25ece42014-03-21 15:27:34 -0700902 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
Simon Wilsone9942c82011-10-13 13:57:25 -0700903 else
904 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
Paul McLeanb25ece42014-03-21 15:27:34 -0700905 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
Simon Wilsone9942c82011-10-13 13:57:25 -0700906
Simon Wilsonedff7082011-06-06 15:33:34 -0700907 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
908 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700909 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700910 }
911
Simon Wilsone9942c82011-10-13 13:57:25 -0700912 /* get our refined hw_params */
913 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
914 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
915 pcm->buffer_size = config->period_count * config->period_size;
916
917 if (flags & PCM_MMAP) {
918 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
919 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
920 if (pcm->mmap_buffer == MAP_FAILED) {
921 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
922 pcm_frames_to_bytes(pcm, pcm->buffer_size));
923 goto fail_close;
924 }
925 }
926
Simon Wilsonedff7082011-06-06 15:33:34 -0700927 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700928 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700929 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700930
Eric Laurentff2e5422012-08-22 16:18:14 -0700931 if (!config->start_threshold) {
932 if (pcm->flags & PCM_IN)
933 pcm->config.start_threshold = sparams.start_threshold = 1;
934 else
935 pcm->config.start_threshold = sparams.start_threshold =
936 config->period_count * config->period_size / 2;
937 } else
Simon Wilsonc1239622011-07-27 15:08:34 -0700938 sparams.start_threshold = config->start_threshold;
939
Simon Wilsone9942c82011-10-13 13:57:25 -0700940 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800941 if (!config->stop_threshold) {
942 if (pcm->flags & PCM_IN)
943 pcm->config.stop_threshold = sparams.stop_threshold =
944 config->period_count * config->period_size * 10;
945 else
946 pcm->config.stop_threshold = sparams.stop_threshold =
947 config->period_count * config->period_size;
948 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700949 else
950 sparams.stop_threshold = config->stop_threshold;
951
Eric Laurent73b9c672011-10-14 11:12:24 -0700952 if (!pcm->config.avail_min) {
953 if (pcm->flags & PCM_MMAP)
954 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
955 else
956 pcm->config.avail_min = sparams.avail_min = 1;
957 } else
958 sparams.avail_min = config->avail_min;
959
Simon Wilsonedff7082011-06-06 15:33:34 -0700960 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
Simon Wilsonc1239622011-07-27 15:08:34 -0700961 sparams.silence_threshold = config->silence_threshold;
Christopher R. Palmer0e64b6c2015-11-28 07:32:13 -0500962#ifdef IGNORE_SILENCE_SIZE
963 sparams.silence_size = 0;
964#else
Maneet Singhe25fe0b2015-06-11 17:34:40 -0700965 sparams.silence_size = config->silence_size;
Christopher R. Palmer0e64b6c2015-11-28 07:32:13 -0500966#endif
Simon Wilsone9942c82011-10-13 13:57:25 -0700967 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700968
Simon Wilsondaa83292012-02-28 15:26:02 -0800969 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Paul McLeanb25ece42014-03-21 15:27:34 -0700970 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700971
972 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
973 oops(pcm, errno, "cannot set sw params");
974 goto fail;
975 }
976
Simon Wilsondd88f132011-07-25 10:58:30 -0700977 rc = pcm_hw_mmap_status(pcm);
978 if (rc < 0) {
979 oops(pcm, rc, "mmap status failed");
980 goto fail;
981 }
982
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700983#ifdef SNDRV_PCM_IOCTL_TTSTAMP
984 if (pcm->flags & PCM_MONOTONIC) {
985 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
986 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
987 if (rc < 0) {
988 oops(pcm, rc, "cannot set timestamp type");
989 goto fail;
990 }
991 }
992#endif
993
Simon Wilsonedff7082011-06-06 15:33:34 -0700994 pcm->underruns = 0;
995 return pcm;
996
997fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700998 if (flags & PCM_MMAP)
999 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1000fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -07001001 close(pcm->fd);
1002 pcm->fd = -1;
1003 return pcm;
1004}
1005
1006int pcm_is_ready(struct pcm *pcm)
1007{
1008 return pcm->fd >= 0;
1009}
Simon Wilson70d77082011-06-24 11:08:10 -07001010
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301011int pcm_prepare(struct pcm *pcm)
Simon Wilson70d77082011-06-24 11:08:10 -07001012{
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301013 if (pcm->prepared)
1014 return 0;
1015
Simon Wilson70d77082011-06-24 11:08:10 -07001016 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1017 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -07001018
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301019 pcm->prepared = 1;
1020 return 0;
1021}
1022
1023int pcm_start(struct pcm *pcm)
1024{
1025 int prepare_error = pcm_prepare(pcm);
1026 if (prepare_error)
1027 return prepare_error;
1028
Simon Wilsone9942c82011-10-13 13:57:25 -07001029 if (pcm->flags & PCM_MMAP)
1030 pcm_sync_ptr(pcm, 0);
1031
Simon Wilson70d77082011-06-24 11:08:10 -07001032 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1033 return oops(pcm, errno, "cannot start channel");
1034
Simon Wilsone9942c82011-10-13 13:57:25 -07001035 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -07001036 return 0;
1037}
1038
1039int pcm_stop(struct pcm *pcm)
1040{
1041 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1042 return oops(pcm, errno, "cannot stop channel");
1043
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301044 pcm->prepared = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -07001045 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -07001046 return 0;
1047}
1048
Simon Wilsone9942c82011-10-13 13:57:25 -07001049static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1050{
1051 int avail;
1052
1053 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1054
1055 if (avail < 0)
1056 avail += pcm->boundary;
1057 else if (avail > (int)pcm->boundary)
1058 avail -= pcm->boundary;
1059
1060 return avail;
1061}
1062
1063static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1064{
1065 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1066 if (avail < 0)
1067 avail += pcm->boundary;
1068 return avail;
1069}
1070
Dylan Reid9074cfc2015-09-01 11:01:57 -07001071int pcm_mmap_avail(struct pcm *pcm)
Simon Wilsone9942c82011-10-13 13:57:25 -07001072{
1073 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1074 if (pcm->flags & PCM_IN)
1075 return pcm_mmap_capture_avail(pcm);
1076 else
1077 return pcm_mmap_playback_avail(pcm);
1078}
1079
1080static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1081{
1082 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1083 appl_ptr += frames;
1084
1085 /* check for boundary wrap */
1086 if (appl_ptr > pcm->boundary)
1087 appl_ptr -= pcm->boundary;
1088 pcm->mmap_control->appl_ptr = appl_ptr;
1089}
1090
1091int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1092 unsigned int *frames)
1093{
1094 unsigned int continuous, copy_frames, avail;
1095
1096 /* return the mmap buffer */
1097 *areas = pcm->mmap_buffer;
1098
1099 /* and the application offset in frames */
1100 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1101
1102 avail = pcm_mmap_avail(pcm);
1103 if (avail > pcm->buffer_size)
1104 avail = pcm->buffer_size;
1105 continuous = pcm->buffer_size - *offset;
1106
1107 /* we can only copy frames if the are availabale and continuos */
1108 copy_frames = *frames;
1109 if (copy_frames > avail)
1110 copy_frames = avail;
1111 if (copy_frames > continuous)
1112 copy_frames = continuous;
1113 *frames = copy_frames;
1114
1115 return 0;
1116}
1117
Glenn Kastenfde0bf12016-02-08 14:42:36 -08001118int pcm_mmap_commit(struct pcm *pcm, unsigned int offset __attribute__((unused)), unsigned int frames)
Simon Wilsone9942c82011-10-13 13:57:25 -07001119{
1120 /* update the application pointer in userspace and kernel */
1121 pcm_mmap_appl_forward(pcm, frames);
1122 pcm_sync_ptr(pcm, 0);
1123
1124 return frames;
1125}
1126
1127int pcm_avail_update(struct pcm *pcm)
1128{
1129 pcm_sync_ptr(pcm, 0);
1130 return pcm_mmap_avail(pcm);
1131}
1132
1133int pcm_state(struct pcm *pcm)
1134{
1135 int err = pcm_sync_ptr(pcm, 0);
1136 if (err < 0)
1137 return err;
1138
1139 return pcm->mmap_status->state;
1140}
1141
Eric Laurent73b9c672011-10-14 11:12:24 -07001142int pcm_set_avail_min(struct pcm *pcm, int avail_min)
1143{
1144 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
1145 return -ENOSYS;
1146
1147 pcm->config.avail_min = avail_min;
1148 return 0;
1149}
1150
Simon Wilsone9942c82011-10-13 13:57:25 -07001151int pcm_wait(struct pcm *pcm, int timeout)
1152{
1153 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -07001154 int err;
1155
1156 pfd.fd = pcm->fd;
1157 pfd.events = POLLOUT | POLLERR | POLLNVAL;
1158
1159 do {
1160 /* let's wait for avail or timeout */
1161 err = poll(&pfd, 1, timeout);
1162 if (err < 0)
1163 return -errno;
1164
1165 /* timeout ? */
1166 if (err == 0)
1167 return 0;
1168
1169 /* have we been interrupted ? */
1170 if (errno == -EINTR)
1171 continue;
1172
1173 /* check for any errors */
1174 if (pfd.revents & (POLLERR | POLLNVAL)) {
1175 switch (pcm_state(pcm)) {
1176 case PCM_STATE_XRUN:
1177 return -EPIPE;
1178 case PCM_STATE_SUSPENDED:
1179 return -ESTRPIPE;
1180 case PCM_STATE_DISCONNECTED:
1181 return -ENODEV;
1182 default:
1183 return -EIO;
1184 }
1185 }
1186 /* poll again if fd not ready for IO */
1187 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1188
1189 return 1;
1190}
1191
Dylan Reidcefba1e2015-09-01 11:03:01 -07001192int pcm_get_poll_fd(struct pcm *pcm)
1193{
1194 return pcm->fd;
1195}
1196
Eric Laurentc98da792013-09-16 14:31:17 -07001197int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -07001198{
1199 int err = 0, frames, avail;
1200 unsigned int offset = 0, count;
1201
1202 if (bytes == 0)
1203 return 0;
1204
1205 count = pcm_bytes_to_frames(pcm, bytes);
1206
1207 while (count > 0) {
1208
1209 /* get the available space for writing new frames */
1210 avail = pcm_avail_update(pcm);
1211 if (avail < 0) {
1212 fprintf(stderr, "cannot determine available mmap frames");
1213 return err;
1214 }
1215
1216 /* start the audio if we reach the threshold */
1217 if (!pcm->running &&
1218 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1219 if (pcm_start(pcm) < 0) {
1220 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1221 (unsigned int)pcm->mmap_status->hw_ptr,
1222 (unsigned int)pcm->mmap_control->appl_ptr,
1223 avail);
1224 return -errno;
1225 }
Eric Laurent73b9c672011-10-14 11:12:24 -07001226 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -07001227 }
1228
1229 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -07001230 if (pcm->running) {
1231 /* enable waiting for avail_min threshold when less frames than we have to write
1232 * are available. */
1233 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1234 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001235
Eric Laurent73b9c672011-10-14 11:12:24 -07001236 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1237 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001238
Eric Laurent73b9c672011-10-14 11:12:24 -07001239 /* disable waiting for avail_min threshold to allow small amounts of data to be
1240 * written without waiting as long as there is enough room in buffer. */
1241 pcm->wait_for_avail_min = 0;
1242
1243 if (pcm->flags & PCM_NOIRQ)
1244 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1245
1246 err = pcm_wait(pcm, time);
1247 if (err < 0) {
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301248 pcm->prepared = 0;
Eric Laurent73b9c672011-10-14 11:12:24 -07001249 pcm->running = 0;
1250 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1251 (unsigned int)pcm->mmap_status->hw_ptr,
1252 (unsigned int)pcm->mmap_control->appl_ptr,
1253 avail);
1254 pcm->mmap_control->appl_ptr = 0;
1255 return err;
1256 }
1257 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -07001258 }
Simon Wilsone9942c82011-10-13 13:57:25 -07001259 }
1260
1261 frames = count;
1262 if (frames > avail)
1263 frames = avail;
1264
1265 if (!frames)
1266 break;
1267
1268 /* copy frames from buffer */
Eric Laurentc98da792013-09-16 14:31:17 -07001269 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -07001270 if (frames < 0) {
1271 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1272 (unsigned int)pcm->mmap_status->hw_ptr,
1273 (unsigned int)pcm->mmap_control->appl_ptr,
1274 avail);
1275 return frames;
1276 }
1277
1278 offset += frames;
1279 count -= frames;
1280 }
1281
Simon Wilsone9942c82011-10-13 13:57:25 -07001282 return 0;
1283}
Eric Laurentc98da792013-09-16 14:31:17 -07001284
1285int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1286{
1287 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1288 return -ENOSYS;
1289
1290 return pcm_mmap_transfer(pcm, (void *)data, count);
1291}
1292
1293int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1294{
1295 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1296 return -ENOSYS;
1297
1298 return pcm_mmap_transfer(pcm, data, count);
1299}
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -07001300
1301int pcm_ioctl(struct pcm *pcm, int request, ...)
1302{
1303 va_list ap;
1304 void * arg;
1305
1306 if (!pcm_is_ready(pcm))
1307 return -1;
1308
1309 va_start(ap, request);
1310 arg = va_arg(ap, void *);
1311 va_end(ap);
1312
1313 return ioctl(pcm->fd, request, arg);
1314}