blob: 1709be990dbe4ea2019dd90dcd87d15246201247 [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
David Lie95e65a2020-12-09 02:11:18 +000049#include <tinyalsa/asoundlib.h>
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -070050#include "pcm_io.h"
51#include "snd_utils.h"
52
53enum {
54 PCM_NODE_TYPE_HW = 0,
55 PCM_NODE_TYPE_PLUGIN,
56};
Simon Wilsonedff7082011-06-06 15:33:34 -070057
58#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
59
Andy Hung70530a62014-03-26 18:29:07 -070060/* Logs information into a string; follows snprintf() in that
61 * offset may be greater than size, and though no characters are copied
62 * into string, characters are still counted into offset. */
63#define STRLOG(string, offset, size, ...) \
64 do { int temp, clipoffset = offset > size ? size : offset; \
65 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
66 if (temp > 0) offset += temp; } while (0)
67
68#ifndef ARRAY_SIZE
69#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
70#endif
71
72/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
73static const char * const access_lookup[] = {
74 "MMAP_INTERLEAVED",
75 "MMAP_NONINTERLEAVED",
76 "MMAP_COMPLEX",
77 "RW_INTERLEAVED",
78 "RW_NONINTERLEAVED",
79};
80
81/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
82static const char * const format_lookup[] = {
83 /*[0] =*/ "S8",
84 "U8",
85 "S16_LE",
86 "S16_BE",
87 "U16_LE",
88 "U16_BE",
89 "S24_LE",
90 "S24_BE",
91 "U24_LE",
92 "U24_BE",
93 "S32_LE",
94 "S32_BE",
95 "U32_LE",
96 "U32_BE",
97 "FLOAT_LE",
98 "FLOAT_BE",
99 "FLOAT64_LE",
100 "FLOAT64_BE",
101 "IEC958_SUBFRAME_LE",
102 "IEC958_SUBFRAME_BE",
103 "MU_LAW",
104 "A_LAW",
105 "IMA_ADPCM",
106 "MPEG",
107 /*[24] =*/ "GSM",
108 /* gap */
109 [31] = "SPECIAL",
110 "S24_3LE",
111 "S24_3BE",
112 "U24_3LE",
113 "U24_3BE",
114 "S20_3LE",
115 "S20_3BE",
116 "U20_3LE",
117 "U20_3BE",
118 "S18_3LE",
119 "S18_3BE",
120 "U18_3LE",
121 /*[43] =*/ "U18_3BE",
122#if 0
123 /* recent additions, may not be present on local asound.h */
124 "G723_24",
125 "G723_24_1B",
126 "G723_40",
127 "G723_40_1B",
128 "DSD_U8",
129 "DSD_U16_LE",
130#endif
131};
132
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700133extern struct pcm_ops hw_ops;
134extern struct pcm_ops plug_ops;
135
Andy Hung70530a62014-03-26 18:29:07 -0700136/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
137static const char * const subformat_lookup[] = {
138 "STD",
139};
140
Simon Wilsonedff7082011-06-06 15:33:34 -0700141static inline int param_is_mask(int p)
142{
143 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
144 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
145}
146
147static inline int param_is_interval(int p)
148{
149 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
150 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
151}
152
153static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
154{
155 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
156}
157
158static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
159{
160 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
161}
162
163static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
164{
165 if (bit >= SNDRV_MASK_MAX)
166 return;
167 if (param_is_mask(n)) {
168 struct snd_mask *m = param_to_mask(p, n);
169 m->bits[0] = 0;
170 m->bits[1] = 0;
171 m->bits[bit >> 5] |= (1 << (bit & 31));
172 }
173}
174
175static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
176{
177 if (param_is_interval(n)) {
178 struct snd_interval *i = param_to_interval(p, n);
179 i->min = val;
180 }
181}
182
David Lie95e65a2020-12-09 02:11:18 +0000183static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
Simon Wilson42fc2d32012-12-03 11:18:57 -0800184{
185 if (param_is_interval(n)) {
David Lie95e65a2020-12-09 02:11:18 +0000186 struct snd_interval *i = param_to_interval(p, n);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800187 return i->min;
188 }
189 return 0;
190}
191
David Lie95e65a2020-12-09 02:11:18 +0000192static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
Paul McLeanb25ece42014-03-21 15:27:34 -0700193{
194 if (param_is_interval(n)) {
David Lie95e65a2020-12-09 02:11:18 +0000195 struct snd_interval *i = param_to_interval(p, n);
196 i->max = val;
197 }
198}
199
200static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
201{
202 if (param_is_interval(n)) {
203 struct snd_interval *i = param_to_interval(p, n);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800204 return i->max;
205 }
206 return 0;
207}
208
Simon Wilsonedff7082011-06-06 15:33:34 -0700209static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
210{
211 if (param_is_interval(n)) {
212 struct snd_interval *i = param_to_interval(p, n);
213 i->min = val;
214 i->max = val;
215 i->integer = 1;
216 }
217}
218
Simon Wilsone9942c82011-10-13 13:57:25 -0700219static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
220{
221 if (param_is_interval(n)) {
222 struct snd_interval *i = param_to_interval(p, n);
223 if (i->integer)
224 return i->max;
225 }
226 return 0;
227}
228
Simon Wilsonedff7082011-06-06 15:33:34 -0700229static void param_init(struct snd_pcm_hw_params *p)
230{
231 int n;
232
233 memset(p, 0, sizeof(*p));
234 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
235 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
236 struct snd_mask *m = param_to_mask(p, n);
237 m->bits[0] = ~0;
238 m->bits[1] = ~0;
239 }
240 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
241 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
242 struct snd_interval *i = param_to_interval(p, n);
243 i->min = 0;
244 i->max = ~0;
245 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800246 p->rmask = ~0U;
247 p->cmask = 0;
248 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700249}
250
251#define PCM_ERROR_MAX 128
252
253struct pcm {
254 int fd;
255 unsigned int flags;
David Lie95e65a2020-12-09 02:11:18 +0000256 int running:1;
257 int prepared:1;
258 int underruns;
Simon Wilsonedff7082011-06-06 15:33:34 -0700259 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700260 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700261 char error[PCM_ERROR_MAX];
262 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700263 struct snd_pcm_mmap_status *mmap_status;
264 struct snd_pcm_mmap_control *mmap_control;
265 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700266 void *mmap_buffer;
267 unsigned int noirq_frames_per_msec;
David Lie95e65a2020-12-09 02:11:18 +0000268 int wait_for_avail_min;
Gaio, MiguelX080294e2014-11-21 00:59:57 -0800269 unsigned int subdevice;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700270
271 struct pcm_ops *ops;
272 void *data;
273 void *snd_node;
Simon Wilsonedff7082011-06-06 15:33:34 -0700274};
275
David Lie95e65a2020-12-09 02:11:18 +0000276unsigned int pcm_get_buffer_size(struct pcm *pcm)
277{
278 return pcm->buffer_size;
279}
280
281const char* pcm_get_error(struct pcm *pcm)
282{
283 return pcm->error;
284}
285
286unsigned int pcm_get_subdevice(struct pcm *pcm)
287{
288 return pcm->subdevice;
289}
290
Simon Wilsonedff7082011-06-06 15:33:34 -0700291static int oops(struct pcm *pcm, int e, const char *fmt, ...)
292{
293 va_list ap;
294 int sz;
295
296 va_start(ap, fmt);
297 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
298 va_end(ap);
299 sz = strlen(pcm->error);
300
John Muir50028cd2017-01-23 13:38:31 -0800301 if (e)
Simon Wilsonedff7082011-06-06 15:33:34 -0700302 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
303 ": %s", strerror(e));
304 return -1;
305}
306
David Lie95e65a2020-12-09 02:11:18 +0000307static unsigned int pcm_format_to_alsa(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700308{
David Lie95e65a2020-12-09 02:11:18 +0000309 switch (format) {
310 case PCM_FORMAT_S32_LE:
311 return SNDRV_PCM_FORMAT_S32_LE;
312 case PCM_FORMAT_S8:
313 return SNDRV_PCM_FORMAT_S8;
314 case PCM_FORMAT_S24_3LE:
315 return SNDRV_PCM_FORMAT_S24_3LE;
316 case PCM_FORMAT_S24_LE:
317 return SNDRV_PCM_FORMAT_S24_LE;
318 default:
319 case PCM_FORMAT_S16_LE:
320 return SNDRV_PCM_FORMAT_S16_LE;
321 };
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700322}
323
Simon Wilson36ea2d82013-07-17 11:10:45 -0700324unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700325{
326 switch (format) {
327 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700328 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700329 return 32;
Glenn Kastend9837d02014-01-31 07:56:33 -0800330 case PCM_FORMAT_S24_3LE:
331 return 24;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700332 default:
333 case PCM_FORMAT_S16_LE:
334 return 16;
335 };
336}
337
David Lie95e65a2020-12-09 02:11:18 +0000338unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -0700339{
340 return bytes / (pcm->config.channels *
341 (pcm_format_to_bits(pcm->config.format) >> 3));
342}
343
David Lie95e65a2020-12-09 02:11:18 +0000344unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
Simon Wilsone9942c82011-10-13 13:57:25 -0700345{
346 return frames * pcm->config.channels *
347 (pcm_format_to_bits(pcm->config.format) >> 3);
348}
349
David Lie95e65a2020-12-09 02:11:18 +0000350static int pcm_sync_ptr(struct pcm *pcm, int flags) {
351 if (pcm->sync_ptr) {
Simon Wilsondd88f132011-07-25 10:58:30 -0700352 pcm->sync_ptr->flags = flags;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700353 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
354 pcm->sync_ptr) < 0)
Simon Wilsondd88f132011-07-25 10:58:30 -0700355 return -1;
356 }
357 return 0;
358}
359
David Lie95e65a2020-12-09 02:11:18 +0000360static int pcm_hw_mmap_status(struct pcm *pcm) {
361
Simon Wilsondd88f132011-07-25 10:58:30 -0700362 if (pcm->sync_ptr)
363 return 0;
364
365 int page_size = sysconf(_SC_PAGE_SIZE);
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530366 pcm->mmap_status = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
367 SNDRV_PCM_MMAP_OFFSET_STATUS);
Simon Wilsondd88f132011-07-25 10:58:30 -0700368 if (pcm->mmap_status == MAP_FAILED)
369 pcm->mmap_status = NULL;
370 if (!pcm->mmap_status)
371 goto mmap_error;
372
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530373 pcm->mmap_control = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ | PROT_WRITE,
374 MAP_FILE | MAP_SHARED, SNDRV_PCM_MMAP_OFFSET_CONTROL);
Simon Wilsondd88f132011-07-25 10:58:30 -0700375 if (pcm->mmap_control == MAP_FAILED)
376 pcm->mmap_control = NULL;
377 if (!pcm->mmap_control) {
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530378 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Simon Wilsondd88f132011-07-25 10:58:30 -0700379 pcm->mmap_status = NULL;
380 goto mmap_error;
381 }
David Lie95e65a2020-12-09 02:11:18 +0000382 if (pcm->flags & PCM_MMAP)
383 pcm->mmap_control->avail_min = pcm->config.avail_min;
384 else
385 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700386
387 return 0;
388
389mmap_error:
390
391 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
392 if (!pcm->sync_ptr)
393 return -ENOMEM;
394 pcm->mmap_status = &pcm->sync_ptr->s.status;
395 pcm->mmap_control = &pcm->sync_ptr->c.control;
David Lie95e65a2020-12-09 02:11:18 +0000396 if (pcm->flags & PCM_MMAP)
397 pcm->mmap_control->avail_min = pcm->config.avail_min;
398 else
399 pcm->mmap_control->avail_min = 1;
400
401 pcm_sync_ptr(pcm, 0);
Simon Wilsondd88f132011-07-25 10:58:30 -0700402
403 return 0;
404}
405
406static void pcm_hw_munmap_status(struct pcm *pcm) {
407 if (pcm->sync_ptr) {
408 free(pcm->sync_ptr);
409 pcm->sync_ptr = NULL;
410 } else {
411 int page_size = sysconf(_SC_PAGE_SIZE);
412 if (pcm->mmap_status)
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530413 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Simon Wilsondd88f132011-07-25 10:58:30 -0700414 if (pcm->mmap_control)
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530415 pcm->ops->munmap(pcm->data, pcm->mmap_control, page_size);
Simon Wilsondd88f132011-07-25 10:58:30 -0700416 }
417 pcm->mmap_status = NULL;
418 pcm->mmap_control = NULL;
419}
420
David Lie95e65a2020-12-09 02:11:18 +0000421static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
422 char *buf, unsigned int src_offset,
423 unsigned int frames)
424{
425 int size_bytes = pcm_frames_to_bytes(pcm, frames);
426 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
427 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
428
429 /* interleaved only atm */
430 if (pcm->flags & PCM_IN)
431 memcpy(buf + src_offset_bytes,
432 (char*)pcm->mmap_buffer + pcm_offset_bytes,
433 size_bytes);
434 else
435 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
436 buf + src_offset_bytes,
437 size_bytes);
438 return 0;
439}
440
441static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
442 unsigned int offset, unsigned int size)
443{
444 void *pcm_areas;
445 int commit;
446 unsigned int pcm_offset, frames, count = 0;
447
448 while (size > 0) {
449 frames = size;
450 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
451 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
452 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
453 if (commit < 0) {
454 oops(pcm, errno, "failed to commit %d frames\n", frames);
455 return commit;
456 }
457
458 offset += commit;
459 count += commit;
460 size -= commit;
461 }
462 return count;
463}
464
465int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
466 struct timespec *tstamp)
467{
468 int frames;
469 int rc;
470 snd_pcm_uframes_t hw_ptr;
471
472 if (!pcm_is_ready(pcm))
473 return -1;
474
475 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
476 if (rc < 0)
477 return -1;
478
479 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
480 (pcm->mmap_status->state != PCM_STATE_DRAINING))
481 return -1;
482
483 *tstamp = pcm->mmap_status->tstamp;
484 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
485 return -1;
486
487 hw_ptr = pcm->mmap_status->hw_ptr;
488 if (pcm->flags & PCM_IN)
489 frames = hw_ptr - pcm->mmap_control->appl_ptr;
490 else
491 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
492
493 if (frames < 0)
494 frames += pcm->boundary;
495 else if (frames > (int)pcm->boundary)
496 frames -= pcm->boundary;
497
498 *avail = (unsigned int)frames;
499
500 return 0;
501}
502
503int pcm_mmap_get_hw_ptr(struct pcm* pcm, unsigned int *hw_ptr, struct timespec *tstamp)
504{
505 int frames;
506 int rc;
507
508 if (pcm == NULL || hw_ptr == NULL || tstamp == NULL)
509 return oops(pcm, EINVAL, "pcm %p, hw_ptr %p, tstamp %p", pcm, hw_ptr, tstamp);
510
511 if (!pcm_is_ready(pcm))
512 return oops(pcm, errno, "pcm_is_ready failed");
513
514 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
515 if (rc < 0)
516 return oops(pcm, errno, "pcm_sync_ptr failed");
517
518 if (pcm->mmap_status == NULL)
519 return oops(pcm, EINVAL, "pcm %p, mmap_status is NULL", pcm);
520
521 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
522 (pcm->mmap_status->state != PCM_STATE_DRAINING))
523 return oops(pcm, ENOSYS, "invalid stream state %d", pcm->mmap_status->state);
524
525 *tstamp = pcm->mmap_status->tstamp;
526 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
527 return oops(pcm, errno, "invalid time stamp");
528
529 *hw_ptr = pcm->mmap_status->hw_ptr;
530
531 return 0;
532}
533
534int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
535{
536 struct snd_xferi x;
537
538 if (pcm->flags & PCM_IN)
539 return -EINVAL;
540
541 x.buf = (void*)data;
542 x.frames = count / (pcm->config.channels *
543 pcm_format_to_bits(pcm->config.format) / 8);
544
545 for (;;) {
546 if (!pcm->running) {
547 int prepare_error = pcm_prepare(pcm);
548 if (prepare_error)
549 return prepare_error;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700550 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
David Lie95e65a2020-12-09 02:11:18 +0000551 return oops(pcm, errno, "cannot write initial data");
552 pcm->running = 1;
553 return 0;
554 }
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700555 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
David Lie95e65a2020-12-09 02:11:18 +0000556 pcm->prepared = 0;
557 pcm->running = 0;
558 if (errno == EPIPE) {
559 /* we failed to make our window -- try to restart if we are
560 * allowed to do so. Otherwise, simply allow the EPIPE error to
561 * propagate up to the app level */
562 pcm->underruns++;
563 if (pcm->flags & PCM_NORESTART)
564 return -EPIPE;
565 continue;
566 }
567 return oops(pcm, errno, "cannot write stream data");
568 }
569 return 0;
570 }
571}
572
573int pcm_read(struct pcm *pcm, void *data, unsigned int count)
574{
575 struct snd_xferi x;
576
577 if (!(pcm->flags & PCM_IN))
578 return -EINVAL;
579
580 x.buf = data;
581 x.frames = count / (pcm->config.channels *
582 pcm_format_to_bits(pcm->config.format) / 8);
583
584 for (;;) {
585 if (!pcm->running) {
586 if (pcm_start(pcm) < 0) {
587 fprintf(stderr, "start error");
588 return -errno;
589 }
590 }
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700591 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
David Lie95e65a2020-12-09 02:11:18 +0000592 pcm->prepared = 0;
593 pcm->running = 0;
594 if (errno == EPIPE) {
595 /* we failed to make our window -- try to restart */
596 pcm->underruns++;
597 continue;
598 }
599 return oops(pcm, errno, "cannot read stream data");
600 }
601 return 0;
602 }
603}
604
Simon Wilsonedff7082011-06-06 15:33:34 -0700605static struct pcm bad_pcm = {
606 .fd = -1,
607};
608
Simon Wilson42fc2d32012-12-03 11:18:57 -0800609struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
610 unsigned int flags)
611{
612 struct snd_pcm_hw_params *params;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700613 enum snd_node_type pcm_type;
614 struct pcm_ops *ops;
615 void *snd_node, *data;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800616 int fd;
617
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700618 snd_node = snd_utils_get_dev_node(card, device, NODE_PCM);
619 pcm_type = snd_utils_get_node_type(snd_node);
620 if (pcm_type == SND_NODE_TYPE_PLUGIN)
621 ops = &plug_ops;
622 else
623 ops = &hw_ops;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800624
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700625 fd = ops->open(card, device, flags, &data, snd_node);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800626 if (fd < 0) {
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700627 fprintf(stderr, "cannot open device %u for card %u\n",
628 device, card);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800629 goto err_open;
630 }
631
632 params = calloc(1, sizeof(struct snd_pcm_hw_params));
633 if (!params)
634 goto err_calloc;
635
636 param_init(params);
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700637 if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
Simon Wilson42fc2d32012-12-03 11:18:57 -0800638 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
639 goto err_hw_refine;
640 }
641
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700642 snd_utils_put_dev_node(snd_node);
643 ops->close(data);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800644
645 return (struct pcm_params *)params;
646
647err_hw_refine:
648 free(params);
649err_calloc:
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700650 ops->close(data);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800651err_open:
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700652 snd_utils_put_dev_node(snd_node);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800653 return NULL;
654}
655
656void pcm_params_free(struct pcm_params *pcm_params)
657{
658 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
659
660 if (params)
661 free(params);
662}
663
664static int pcm_param_to_alsa(enum pcm_param param)
665{
666 switch (param) {
Andy Hunga5b44d92014-03-10 18:08:15 -0700667 case PCM_PARAM_ACCESS:
668 return SNDRV_PCM_HW_PARAM_ACCESS;
669 case PCM_PARAM_FORMAT:
670 return SNDRV_PCM_HW_PARAM_FORMAT;
671 case PCM_PARAM_SUBFORMAT:
672 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800673 case PCM_PARAM_SAMPLE_BITS:
674 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
675 break;
676 case PCM_PARAM_FRAME_BITS:
677 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
678 break;
679 case PCM_PARAM_CHANNELS:
680 return SNDRV_PCM_HW_PARAM_CHANNELS;
681 break;
682 case PCM_PARAM_RATE:
683 return SNDRV_PCM_HW_PARAM_RATE;
684 break;
685 case PCM_PARAM_PERIOD_TIME:
686 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
687 break;
688 case PCM_PARAM_PERIOD_SIZE:
689 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
690 break;
691 case PCM_PARAM_PERIOD_BYTES:
692 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
693 break;
694 case PCM_PARAM_PERIODS:
695 return SNDRV_PCM_HW_PARAM_PERIODS;
696 break;
697 case PCM_PARAM_BUFFER_TIME:
698 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
699 break;
700 case PCM_PARAM_BUFFER_SIZE:
701 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
702 break;
703 case PCM_PARAM_BUFFER_BYTES:
704 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
705 break;
706 case PCM_PARAM_TICK_TIME:
707 return SNDRV_PCM_HW_PARAM_TICK_TIME;
708 break;
709
710 default:
711 return -1;
712 }
713}
714
David Lie95e65a2020-12-09 02:11:18 +0000715struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hunga5b44d92014-03-10 18:08:15 -0700716 enum pcm_param param)
717{
718 int p;
719 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
720 if (params == NULL) {
721 return NULL;
722 }
723
724 p = pcm_param_to_alsa(param);
725 if (p < 0 || !param_is_mask(p)) {
726 return NULL;
727 }
728
David Lie95e65a2020-12-09 02:11:18 +0000729 return (struct pcm_mask *)param_to_mask(params, p);
Andy Hunga5b44d92014-03-10 18:08:15 -0700730}
731
Glenn Kasten4213b7b2020-09-22 14:54:43 -0700732unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson42fc2d32012-12-03 11:18:57 -0800733 enum pcm_param param)
734{
735 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
736 int p;
737
738 if (!params)
739 return 0;
740
741 p = pcm_param_to_alsa(param);
742 if (p < 0)
743 return 0;
744
745 return param_get_min(params, p);
746}
747
David Lie95e65a2020-12-09 02:11:18 +0000748void pcm_params_set_min(struct pcm_params *pcm_params,
749 enum pcm_param param, unsigned int val)
750{
751 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
752 int p;
753
754 if (!params)
755 return;
756
757 p = pcm_param_to_alsa(param);
758 if (p < 0)
759 return;
760
761 param_set_min(params, p, val);
762}
763
Glenn Kasten4213b7b2020-09-22 14:54:43 -0700764unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson42fc2d32012-12-03 11:18:57 -0800765 enum pcm_param param)
766{
David Lie95e65a2020-12-09 02:11:18 +0000767 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800768 int p;
769
770 if (!params)
771 return 0;
772
773 p = pcm_param_to_alsa(param);
774 if (p < 0)
775 return 0;
776
777 return param_get_max(params, p);
778}
779
David Lie95e65a2020-12-09 02:11:18 +0000780void pcm_params_set_max(struct pcm_params *pcm_params,
781 enum pcm_param param, unsigned int val)
782{
783 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
784 int p;
785
786 if (!params)
787 return;
788
789 p = pcm_param_to_alsa(param);
790 if (p < 0)
791 return;
792
793 param_set_max(params, p, val);
794}
795
796static int pcm_mask_test(struct pcm_mask *m, unsigned int index)
Andy Hung70530a62014-03-26 18:29:07 -0700797{
798 const unsigned int bitshift = 5; /* for 32 bit integer */
799 const unsigned int bitmask = (1 << bitshift) - 1;
800 unsigned int element;
801
802 element = index >> bitshift;
803 if (element >= ARRAY_SIZE(m->bits))
804 return 0; /* for safety, but should never occur */
805 return (m->bits[element] >> (index & bitmask)) & 1;
806}
807
David Lie95e65a2020-12-09 02:11:18 +0000808static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size,
Andy Hung70530a62014-03-26 18:29:07 -0700809 char *mask_name,
810 const char * const *bit_array_name, size_t bit_array_size)
811{
812 unsigned int i;
813 unsigned int offset = 0;
814
815 if (m == NULL)
816 return 0;
817 if (bit_array_size < 32) {
818 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
819 } else { /* spans two or more bitfields, print with an array index */
820 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
821 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
822 mask_name, i, m->bits[i]);
823 }
824 }
825 for (i = 0; i < bit_array_size; ++i) {
826 if (pcm_mask_test(m, i)) {
827 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
828 }
829 }
830 return offset;
831}
832
833int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
834{
David Lie95e65a2020-12-09 02:11:18 +0000835 struct pcm_mask *m;
Andy Hung70530a62014-03-26 18:29:07 -0700836 unsigned int min, max;
837 unsigned int clipoffset, offset;
838
839 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
840 offset = pcm_mask_to_string(m, string, size,
841 "Access", access_lookup, ARRAY_SIZE(access_lookup));
842 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
843 clipoffset = offset > size ? size : offset;
844 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
845 "Format", format_lookup, ARRAY_SIZE(format_lookup));
846 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
847 clipoffset = offset > size ? size : offset;
848 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
849 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
850 min = pcm_params_get_min(params, PCM_PARAM_RATE);
851 max = pcm_params_get_max(params, PCM_PARAM_RATE);
852 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
853 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
854 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
855 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
856 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
857 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
858 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
859 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
860 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
861 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
862 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
863 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
864 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
865 return offset;
866}
867
868int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
869{
870 unsigned int alsa_format = pcm_format_to_alsa(format);
871
872 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
873 return 0; /* caution: format not recognized is equivalent to S16_LE */
874 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
875}
876
Simon Wilsonedff7082011-06-06 15:33:34 -0700877int pcm_close(struct pcm *pcm)
878{
879 if (pcm == &bad_pcm)
880 return 0;
881
Simon Wilsondd88f132011-07-25 10:58:30 -0700882 pcm_hw_munmap_status(pcm);
883
Simon Wilsone9942c82011-10-13 13:57:25 -0700884 if (pcm->flags & PCM_MMAP) {
885 pcm_stop(pcm);
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530886 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Simon Wilsone9942c82011-10-13 13:57:25 -0700887 }
888
Weiyin Jiang1d532072020-03-14 15:36:40 +0800889 if (pcm->data)
890 pcm->ops->close(pcm->data);
891 if (pcm->snd_node)
892 snd_utils_put_dev_node(pcm->snd_node);
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700893
David Lie95e65a2020-12-09 02:11:18 +0000894 pcm->prepared = 0;
895 pcm->running = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700896 pcm->buffer_size = 0;
897 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700898 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700899 return 0;
900}
901
Simon Wilsonedff7082011-06-06 15:33:34 -0700902struct pcm *pcm_open(unsigned int card, unsigned int device,
David Lie95e65a2020-12-09 02:11:18 +0000903 unsigned int flags, struct pcm_config *config)
Simon Wilsonedff7082011-06-06 15:33:34 -0700904{
905 struct pcm *pcm;
906 struct snd_pcm_info info;
David Lie95e65a2020-12-09 02:11:18 +0000907 struct snd_pcm_hw_params params;
908 struct snd_pcm_sw_params sparams;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700909 int rc, pcm_type;
Simon Wilsonedff7082011-06-06 15:33:34 -0700910
David Lie95e65a2020-12-09 02:11:18 +0000911 if (!config) {
912 return &bad_pcm; /* TODO: could support default config here */
913 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700914 pcm = calloc(1, sizeof(struct pcm));
David Li70f656f2021-06-30 16:57:41 +0800915 if (!pcm) {
916 oops(&bad_pcm, ENOMEM, "can't allocate PCM object");
David Lie95e65a2020-12-09 02:11:18 +0000917 return &bad_pcm; /* TODO: could support default config here */
David Li70f656f2021-06-30 16:57:41 +0800918 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700919
David Lie95e65a2020-12-09 02:11:18 +0000920 pcm->config = *config;
Simon Wilsonedff7082011-06-06 15:33:34 -0700921 pcm->flags = flags;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700922
923 pcm->snd_node = snd_utils_get_dev_node(card, device, NODE_PCM);
924 pcm_type = snd_utils_get_node_type(pcm->snd_node);
925 if (pcm_type == SND_NODE_TYPE_PLUGIN)
926 pcm->ops = &plug_ops;
927 else
928 pcm->ops = &hw_ops;
929
930 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
David Lie95e65a2020-12-09 02:11:18 +0000931 if (pcm->fd < 0) {
David Li70f656f2021-06-30 16:57:41 +0800932 oops(&bad_pcm, errno, "cannot open device %u for card %u",
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700933 device, card);
934 goto fail_open;
David Lie95e65a2020-12-09 02:11:18 +0000935 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700936
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700937 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
David Li70f656f2021-06-30 16:57:41 +0800938 oops(&bad_pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700939 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700940 }
Gaio, MiguelX080294e2014-11-21 00:59:57 -0800941 pcm->subdevice = info.subdevice;
Simon Wilsonedff7082011-06-06 15:33:34 -0700942
David Lie95e65a2020-12-09 02:11:18 +0000943 param_init(&params);
944 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
945 pcm_format_to_alsa(config->format));
946 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
947 SNDRV_PCM_SUBFORMAT_STD);
948 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
949 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
950 pcm_format_to_bits(config->format));
951 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
952 pcm_format_to_bits(config->format) * config->channels);
953 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
954 config->channels);
955 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
956 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
957
958 if (flags & PCM_NOIRQ) {
959 if (!(flags & PCM_MMAP)) {
David Li70f656f2021-06-30 16:57:41 +0800960 oops(&bad_pcm, EINVAL, "noirq only currently supported with mmap().");
David Lie95e65a2020-12-09 02:11:18 +0000961 goto fail_close;
962 }
963
964 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
965 pcm->noirq_frames_per_msec = config->rate / 1000;
966 }
967
968 if (flags & PCM_MMAP)
969 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
970 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
971 else
972 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
973 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
974
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -0700975 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
David Li70f656f2021-06-30 16:57:41 +0800976 oops(&bad_pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700977 goto fail_close;
David Lie95e65a2020-12-09 02:11:18 +0000978 }
979
980 /* get our refined hw_params */
981 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
982 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
983 pcm->buffer_size = config->period_count * config->period_size;
984
985 if (flags & PCM_MMAP) {
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +0530986 pcm->mmap_buffer = pcm->ops->mmap(pcm->data, NULL,
987 pcm_frames_to_bytes(pcm, pcm->buffer_size),
988 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, 0);
David Lie95e65a2020-12-09 02:11:18 +0000989 if (pcm->mmap_buffer == MAP_FAILED) {
David Li70f656f2021-06-30 16:57:41 +0800990 oops(&bad_pcm, errno, "failed to mmap buffer %d bytes\n",
David Lie95e65a2020-12-09 02:11:18 +0000991 pcm_frames_to_bytes(pcm, pcm->buffer_size));
992 goto fail_close;
993 }
994 }
995
996 memset(&sparams, 0, sizeof(sparams));
997 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
998 sparams.period_step = 1;
999
1000 if (!config->start_threshold) {
1001 if (pcm->flags & PCM_IN)
1002 pcm->config.start_threshold = sparams.start_threshold = 1;
1003 else
1004 pcm->config.start_threshold = sparams.start_threshold =
1005 config->period_count * config->period_size / 2;
1006 } else
1007 sparams.start_threshold = config->start_threshold;
1008
1009 /* pick a high stop threshold - todo: does this need further tuning */
1010 if (!config->stop_threshold) {
1011 if (pcm->flags & PCM_IN)
1012 pcm->config.stop_threshold = sparams.stop_threshold =
1013 config->period_count * config->period_size * 10;
1014 else
1015 pcm->config.stop_threshold = sparams.stop_threshold =
1016 config->period_count * config->period_size;
1017 }
1018 else
1019 sparams.stop_threshold = config->stop_threshold;
1020
1021 if (!pcm->config.avail_min) {
1022 if (pcm->flags & PCM_MMAP)
1023 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
1024 else
1025 pcm->config.avail_min = sparams.avail_min = 1;
1026 } else
1027 sparams.avail_min = config->avail_min;
1028
1029 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
1030 sparams.silence_threshold = config->silence_threshold;
1031 sparams.silence_size = config->silence_size;
1032 pcm->boundary = sparams.boundary = pcm->buffer_size;
1033
1034 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
1035 pcm->boundary *= 2;
1036
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001037 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
David Li70f656f2021-06-30 16:57:41 +08001038 oops(&bad_pcm, errno, "cannot set sw params");
David Lie95e65a2020-12-09 02:11:18 +00001039 goto fail;
1040 }
Simon Wilsonedff7082011-06-06 15:33:34 -07001041
Simon Wilsondd88f132011-07-25 10:58:30 -07001042 rc = pcm_hw_mmap_status(pcm);
1043 if (rc < 0) {
David Li70f656f2021-06-30 16:57:41 +08001044 oops(&bad_pcm, errno, "mmap status failed");
Simon Wilsondd88f132011-07-25 10:58:30 -07001045 goto fail;
1046 }
1047
Glenn Kasten6b0a2062013-08-22 15:11:48 -07001048#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1049 if (pcm->flags & PCM_MONOTONIC) {
1050 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001051 rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
Glenn Kasten6b0a2062013-08-22 15:11:48 -07001052 if (rc < 0) {
David Li70f656f2021-06-30 16:57:41 +08001053 oops(&bad_pcm, errno, "cannot set timestamp type");
Glenn Kasten6b0a2062013-08-22 15:11:48 -07001054 goto fail;
1055 }
1056 }
1057#endif
1058
David Lie95e65a2020-12-09 02:11:18 +00001059 pcm->underruns = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -07001060 return pcm;
1061
1062fail:
Simon Wilsone9942c82011-10-13 13:57:25 -07001063 if (flags & PCM_MMAP)
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +05301064 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Simon Wilsone9942c82011-10-13 13:57:25 -07001065fail_close:
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001066 pcm->ops->close(pcm->data);
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001067
1068fail_open:
1069 snd_utils_put_dev_node(pcm->snd_node);
David Li16d36362021-03-08 11:39:52 +00001070 free(pcm);
1071 return &bad_pcm;
Simon Wilsonedff7082011-06-06 15:33:34 -07001072}
1073
David Lie95e65a2020-12-09 02:11:18 +00001074int pcm_is_ready(struct pcm *pcm)
Simon Wilsonedff7082011-06-06 15:33:34 -07001075{
David Lie95e65a2020-12-09 02:11:18 +00001076 return pcm->fd >= 0;
Simon Wilsonedff7082011-06-06 15:33:34 -07001077}
Simon Wilson70d77082011-06-24 11:08:10 -07001078
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301079int pcm_prepare(struct pcm *pcm)
Simon Wilson70d77082011-06-24 11:08:10 -07001080{
David Lie95e65a2020-12-09 02:11:18 +00001081 if (pcm->prepared)
1082 return 0;
1083
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001084 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilson70d77082011-06-24 11:08:10 -07001085 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -07001086
David Lie95e65a2020-12-09 02:11:18 +00001087 pcm->prepared = 1;
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301088 return 0;
1089}
1090
1091int pcm_start(struct pcm *pcm)
1092{
David Lie95e65a2020-12-09 02:11:18 +00001093 int prepare_error = pcm_prepare(pcm);
1094 if (prepare_error)
1095 return prepare_error;
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +05301096
David Lie95e65a2020-12-09 02:11:18 +00001097 if (pcm->flags & PCM_MMAP)
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001098 pcm_sync_ptr(pcm, 0);
Simon Wilsone9942c82011-10-13 13:57:25 -07001099
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001100 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
David Lie95e65a2020-12-09 02:11:18 +00001101 return oops(pcm, errno, "cannot start channel");
1102
1103 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -07001104 return 0;
1105}
1106
1107int pcm_stop(struct pcm *pcm)
1108{
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001109 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilson70d77082011-06-24 11:08:10 -07001110 return oops(pcm, errno, "cannot stop channel");
1111
David Lie95e65a2020-12-09 02:11:18 +00001112 pcm->prepared = 0;
1113 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -07001114 return 0;
1115}
1116
Simon Wilsone9942c82011-10-13 13:57:25 -07001117static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1118{
1119 int avail;
1120
1121 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1122
1123 if (avail < 0)
1124 avail += pcm->boundary;
David Lie95e65a2020-12-09 02:11:18 +00001125 else if (avail > (int)pcm->boundary)
Simon Wilsone9942c82011-10-13 13:57:25 -07001126 avail -= pcm->boundary;
1127
1128 return avail;
1129}
1130
1131static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1132{
1133 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1134 if (avail < 0)
1135 avail += pcm->boundary;
1136 return avail;
1137}
1138
Dylan Reid9074cfc2015-09-01 11:01:57 -07001139int pcm_mmap_avail(struct pcm *pcm)
Simon Wilsone9942c82011-10-13 13:57:25 -07001140{
David Lie95e65a2020-12-09 02:11:18 +00001141 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
Simon Wilsone9942c82011-10-13 13:57:25 -07001142 if (pcm->flags & PCM_IN)
1143 return pcm_mmap_capture_avail(pcm);
1144 else
1145 return pcm_mmap_playback_avail(pcm);
1146}
1147
1148static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1149{
1150 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1151 appl_ptr += frames;
1152
1153 /* check for boundary wrap */
1154 if (appl_ptr > pcm->boundary)
1155 appl_ptr -= pcm->boundary;
1156 pcm->mmap_control->appl_ptr = appl_ptr;
1157}
1158
1159int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1160 unsigned int *frames)
1161{
1162 unsigned int continuous, copy_frames, avail;
1163
1164 /* return the mmap buffer */
1165 *areas = pcm->mmap_buffer;
1166
1167 /* and the application offset in frames */
1168 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1169
1170 avail = pcm_mmap_avail(pcm);
1171 if (avail > pcm->buffer_size)
1172 avail = pcm->buffer_size;
1173 continuous = pcm->buffer_size - *offset;
1174
1175 /* we can only copy frames if the are availabale and continuos */
1176 copy_frames = *frames;
1177 if (copy_frames > avail)
1178 copy_frames = avail;
1179 if (copy_frames > continuous)
1180 copy_frames = continuous;
1181 *frames = copy_frames;
1182
1183 return 0;
1184}
1185
David Lie95e65a2020-12-09 02:11:18 +00001186int pcm_mmap_commit(struct pcm *pcm, unsigned int offset __attribute__((unused)), unsigned int frames)
Simon Wilsone9942c82011-10-13 13:57:25 -07001187{
1188 /* update the application pointer in userspace and kernel */
1189 pcm_mmap_appl_forward(pcm, frames);
David Lie95e65a2020-12-09 02:11:18 +00001190 pcm_sync_ptr(pcm, 0);
Simon Wilsone9942c82011-10-13 13:57:25 -07001191
1192 return frames;
1193}
1194
1195int pcm_avail_update(struct pcm *pcm)
1196{
David Lie95e65a2020-12-09 02:11:18 +00001197 pcm_sync_ptr(pcm, 0);
Simon Wilsone9942c82011-10-13 13:57:25 -07001198 return pcm_mmap_avail(pcm);
1199}
1200
1201int pcm_state(struct pcm *pcm)
1202{
1203 int err = pcm_sync_ptr(pcm, 0);
1204 if (err < 0)
1205 return err;
1206
1207 return pcm->mmap_status->state;
1208}
1209
David Lie95e65a2020-12-09 02:11:18 +00001210int pcm_set_avail_min(struct pcm *pcm, int avail_min)
1211{
1212 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
1213 return -ENOSYS;
1214
1215 pcm->config.avail_min = avail_min;
1216 return 0;
1217}
1218
Simon Wilsone9942c82011-10-13 13:57:25 -07001219int pcm_wait(struct pcm *pcm, int timeout)
1220{
1221 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -07001222 int err;
1223
1224 pfd.fd = pcm->fd;
David Lie95e65a2020-12-09 02:11:18 +00001225 pfd.events = POLLOUT | POLLERR | POLLNVAL;
Simon Wilsone9942c82011-10-13 13:57:25 -07001226
1227 do {
1228 /* let's wait for avail or timeout */
Phani Kumar Uppalapati3c70c562019-10-23 16:08:08 +05301229 err = pcm->ops->poll(pcm->data, &pfd, 1, timeout);
Simon Wilsone9942c82011-10-13 13:57:25 -07001230 if (err < 0)
1231 return -errno;
1232
1233 /* timeout ? */
1234 if (err == 0)
1235 return 0;
1236
1237 /* have we been interrupted ? */
1238 if (errno == -EINTR)
1239 continue;
1240
1241 /* check for any errors */
1242 if (pfd.revents & (POLLERR | POLLNVAL)) {
1243 switch (pcm_state(pcm)) {
1244 case PCM_STATE_XRUN:
1245 return -EPIPE;
1246 case PCM_STATE_SUSPENDED:
1247 return -ESTRPIPE;
1248 case PCM_STATE_DISCONNECTED:
1249 return -ENODEV;
1250 default:
1251 return -EIO;
1252 }
1253 }
1254 /* poll again if fd not ready for IO */
1255 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1256
1257 return 1;
1258}
1259
David Lie95e65a2020-12-09 02:11:18 +00001260int pcm_get_poll_fd(struct pcm *pcm)
Dylan Reidcefba1e2015-09-01 11:03:01 -07001261{
David Lie95e65a2020-12-09 02:11:18 +00001262 return pcm->fd;
1263}
Dylan Reidcefba1e2015-09-01 11:03:01 -07001264
David Lie95e65a2020-12-09 02:11:18 +00001265int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
1266{
1267 int err = 0, frames, avail;
1268 unsigned int offset = 0, count;
Simon Wilsone9942c82011-10-13 13:57:25 -07001269
David Lie95e65a2020-12-09 02:11:18 +00001270 if (bytes == 0)
Simon Wilsone9942c82011-10-13 13:57:25 -07001271 return 0;
1272
David Lie95e65a2020-12-09 02:11:18 +00001273 count = pcm_bytes_to_frames(pcm, bytes);
Simon Wilsone9942c82011-10-13 13:57:25 -07001274
David Lie95e65a2020-12-09 02:11:18 +00001275 while (count > 0) {
Simon Wilsone9942c82011-10-13 13:57:25 -07001276
David Lie95e65a2020-12-09 02:11:18 +00001277 /* get the available space for writing new frames */
1278 avail = pcm_avail_update(pcm);
1279 if (avail < 0) {
1280 fprintf(stderr, "cannot determine available mmap frames");
1281 return err;
David Lie0f41062020-11-05 12:27:29 +00001282 }
1283
David Lie95e65a2020-12-09 02:11:18 +00001284 /* start the audio if we reach the threshold */
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001285 if (!pcm->running &&
David Lie95e65a2020-12-09 02:11:18 +00001286 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1287 if (pcm_start(pcm) < 0) {
1288 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1289 (unsigned int)pcm->mmap_status->hw_ptr,
1290 (unsigned int)pcm->mmap_control->appl_ptr,
1291 avail);
1292 return -errno;
1293 }
1294 pcm->wait_for_avail_min = 0;
1295 }
1296
1297 /* sleep until we have space to write new frames */
1298 if (pcm->running) {
1299 /* enable waiting for avail_min threshold when less frames than we have to write
1300 * are available. */
1301 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1302 pcm->wait_for_avail_min = 1;
1303
1304 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1305 int time = -1;
1306
1307 /* disable waiting for avail_min threshold to allow small amounts of data to be
1308 * written without waiting as long as there is enough room in buffer. */
1309 pcm->wait_for_avail_min = 0;
1310
1311 if (pcm->flags & PCM_NOIRQ)
1312 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1313
1314 err = pcm_wait(pcm, time);
1315 if (err < 0) {
1316 pcm->prepared = 0;
1317 pcm->running = 0;
1318 oops(pcm, errno, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1319 (unsigned int)pcm->mmap_status->hw_ptr,
1320 (unsigned int)pcm->mmap_control->appl_ptr,
1321 avail);
1322 pcm->mmap_control->appl_ptr = 0;
1323 return err;
1324 }
1325 continue;
1326 }
1327 }
1328
1329 frames = count;
1330 if (frames > avail)
1331 frames = avail;
1332
1333 if (!frames)
David Lie0f41062020-11-05 12:27:29 +00001334 break;
1335
David Lie95e65a2020-12-09 02:11:18 +00001336 /* copy frames from buffer */
1337 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
1338 if (frames < 0) {
1339 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1340 (unsigned int)pcm->mmap_status->hw_ptr,
1341 (unsigned int)pcm->mmap_control->appl_ptr,
1342 avail);
1343 return frames;
David Lie0f41062020-11-05 12:27:29 +00001344 }
David Lie95e65a2020-12-09 02:11:18 +00001345
1346 offset += frames;
1347 count -= frames;
David Lie0f41062020-11-05 12:27:29 +00001348 }
1349
David Lie95e65a2020-12-09 02:11:18 +00001350 return 0;
Simon Wilsone9942c82011-10-13 13:57:25 -07001351}
Eric Laurentc98da792013-09-16 14:31:17 -07001352
1353int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1354{
1355 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1356 return -ENOSYS;
1357
David Lie95e65a2020-12-09 02:11:18 +00001358 return pcm_mmap_transfer(pcm, (void *)data, count);
Eric Laurentc98da792013-09-16 14:31:17 -07001359}
1360
1361int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1362{
1363 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1364 return -ENOSYS;
1365
David Lie95e65a2020-12-09 02:11:18 +00001366 return pcm_mmap_transfer(pcm, data, count);
Eric Laurentc98da792013-09-16 14:31:17 -07001367}
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -07001368
David Lie95e65a2020-12-09 02:11:18 +00001369int pcm_ioctl(struct pcm *pcm, int request, ...)
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -07001370{
David Lie95e65a2020-12-09 02:11:18 +00001371 va_list ap;
1372 void * arg;
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -07001373
1374 if (!pcm_is_ready(pcm))
1375 return -1;
1376
David Lie95e65a2020-12-09 02:11:18 +00001377 va_start(ap, request);
1378 arg = va_arg(ap, void *);
1379 va_end(ap);
1380
Bhalchandra Gajareeb387d72019-08-21 15:07:28 -07001381 return pcm->ops->ioctl(pcm->data, request, arg);
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -07001382}