blob: f0751f74cea43cf7d4d0b9c64b63938ef2232bce [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* pcm.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070061#include <tinyalsa/limits.h>
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -070062#include "pcm_io.h"
63#include "snd_card_plugin.h"
Simon Wilson79d39652011-05-25 13:44:23 -070064
Taylor Holberton1f741562018-11-28 16:33:24 -050065#ifndef PARAM_MAX
Simon Wilson79d39652011-05-25 13:44:23 -070066#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Taylor Holberton1f741562018-11-28 16:33:24 -050067#endif /* PARAM_MAX */
68
69#ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
Liam Girdwood6be28f12011-10-13 12:59:51 -070070#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Taylor Holberton1f741562018-11-28 16:33:24 -050071#endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */
Simon Wilson79d39652011-05-25 13:44:23 -070072
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -070073
Simon Wilson79d39652011-05-25 13:44:23 -070074static inline int param_is_mask(int p)
75{
76 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
77 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
78}
79
80static inline int param_is_interval(int p)
81{
82 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
83 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
84}
85
Taylor Holberton2f387d22016-12-01 15:58:16 -080086static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
87{
Taylor Holberton25976dc2017-04-10 11:46:40 -070088 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080089}
90
Simon Wilson79d39652011-05-25 13:44:23 -070091static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
92{
93 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
94}
95
96static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
97{
98 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
99}
100
101static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
102{
103 if (bit >= SNDRV_MASK_MAX)
104 return;
105 if (param_is_mask(n)) {
106 struct snd_mask *m = param_to_mask(p, n);
107 m->bits[0] = 0;
108 m->bits[1] = 0;
109 m->bits[bit >> 5] |= (1 << (bit & 31));
110 }
111}
112
113static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
114{
115 if (param_is_interval(n)) {
116 struct snd_interval *i = param_to_interval(p, n);
117 i->min = val;
118 }
119}
120
Taylor Holberton2f387d22016-12-01 15:58:16 -0800121static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700122{
123 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800124 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700125 return i->min;
126 }
127 return 0;
128}
129
Taylor Holberton2f387d22016-12-01 15:58:16 -0800130static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700131{
132 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800133 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700134 return i->max;
135 }
136 return 0;
137}
138
Simon Wilson79d39652011-05-25 13:44:23 -0700139static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
140{
141 if (param_is_interval(n)) {
142 struct snd_interval *i = param_to_interval(p, n);
143 i->min = val;
144 i->max = val;
145 i->integer = 1;
146 }
147}
148
Liam Girdwood6be28f12011-10-13 12:59:51 -0700149static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
150{
151 if (param_is_interval(n)) {
152 struct snd_interval *i = param_to_interval(p, n);
153 if (i->integer)
154 return i->max;
155 }
156 return 0;
157}
158
Simon Wilson79d39652011-05-25 13:44:23 -0700159static void param_init(struct snd_pcm_hw_params *p)
160{
161 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700162
Simon Wilson79d39652011-05-25 13:44:23 -0700163 memset(p, 0, sizeof(*p));
164 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
165 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
166 struct snd_mask *m = param_to_mask(p, n);
167 m->bits[0] = ~0;
168 m->bits[1] = ~0;
169 }
170 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
171 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
172 struct snd_interval *i = param_to_interval(p, n);
173 i->min = 0;
174 i->max = ~0;
175 }
Simon Wilson43544882012-10-31 12:52:39 -0700176 p->rmask = ~0U;
177 p->cmask = 0;
178 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700179}
180
Taylor Holberton861da7a2017-04-10 12:05:51 -0700181static unsigned int pcm_format_to_alsa(enum pcm_format format)
182{
183 switch (format) {
184
185 case PCM_FORMAT_S8:
186 return SNDRV_PCM_FORMAT_S8;
187
188 default:
189 case PCM_FORMAT_S16_LE:
190 return SNDRV_PCM_FORMAT_S16_LE;
191 case PCM_FORMAT_S16_BE:
192 return SNDRV_PCM_FORMAT_S16_BE;
193
194 case PCM_FORMAT_S24_LE:
195 return SNDRV_PCM_FORMAT_S24_LE;
196 case PCM_FORMAT_S24_BE:
197 return SNDRV_PCM_FORMAT_S24_BE;
198
199 case PCM_FORMAT_S24_3LE:
200 return SNDRV_PCM_FORMAT_S24_3LE;
201 case PCM_FORMAT_S24_3BE:
202 return SNDRV_PCM_FORMAT_S24_3BE;
203
204 case PCM_FORMAT_S32_LE:
205 return SNDRV_PCM_FORMAT_S32_LE;
206 case PCM_FORMAT_S32_BE:
207 return SNDRV_PCM_FORMAT_S32_BE;
208 };
209}
210
Simon Wilson79d39652011-05-25 13:44:23 -0700211#define PCM_ERROR_MAX 128
212
Taylor Holberton6d58e012016-10-01 18:32:30 -0400213/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800214 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400215 */
Simon Wilson79d39652011-05-25 13:44:23 -0700216struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400217 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700218 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400219 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700220 unsigned int flags;
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200221 /** The number of (under/over)runs that have occured */
222 int xruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400223 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700224 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800225 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700226 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400227 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700228 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400229 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700230 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700231 struct snd_pcm_mmap_status *mmap_status;
232 struct snd_pcm_mmap_control *mmap_control;
233 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700234 void *mmap_buffer;
235 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800236 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530237 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800238 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200239 unsigned int subdevice;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700240 /** Pointer to the pcm ops, either hw or plugin */
241 const struct pcm_ops *ops;
242 /** Private data for pcm_hw or pcm_plugin */
243 void *data;
244 /** Pointer to the pcm node from snd card definition */
245 struct snd_node *snd_node;
Simon Wilson79d39652011-05-25 13:44:23 -0700246};
247
Taylor Holberton861da7a2017-04-10 12:05:51 -0700248static int oops(struct pcm *pcm, int e, const char *fmt, ...)
249{
250 va_list ap;
251 int sz;
252
253 va_start(ap, fmt);
254 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
255 va_end(ap);
256 sz = strlen(pcm->error);
257
258 if (errno)
259 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
260 ": %s", strerror(e));
261 return -1;
262}
263
Taylor Holberton6d58e012016-10-01 18:32:30 -0400264/** Gets the buffer size of the PCM.
265 * @param pcm A PCM handle.
266 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800267 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400268 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800269unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700270{
271 return pcm->buffer_size;
272}
273
Taylor Holberton77979a82016-12-01 20:04:04 -0800274/** Gets the channel count of the PCM.
275 * @param pcm A PCM handle.
276 * @return The channel count of the PCM.
277 * @ingroup libtinyalsa-pcm
278 */
279unsigned int pcm_get_channels(const struct pcm *pcm)
280{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700281 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800282}
283
Taylor Holberton08bb5902017-04-10 11:45:44 -0700284/** Gets the PCM configuration.
285 * @param pcm A PCM handle.
286 * @return The PCM configuration.
287 * This function only returns NULL if
288 * @p pcm is NULL.
289 * @ingroup libtinyalsa-pcm
290 * */
291const struct pcm_config * pcm_get_config(const struct pcm *pcm)
292{
293 if (pcm == NULL)
294 return NULL;
295 return &pcm->config;
296}
297
Taylor Holberton77979a82016-12-01 20:04:04 -0800298/** Gets the rate of the PCM.
299 * The rate is given in frames per second.
300 * @param pcm A PCM handle.
301 * @return The rate of the PCM.
302 * @ingroup libtinyalsa-pcm
303 */
304unsigned int pcm_get_rate(const struct pcm *pcm)
305{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700306 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800307}
308
309/** Gets the format of the PCM.
310 * @param pcm A PCM handle.
311 * @return The format of the PCM.
312 * @ingroup libtinyalsa-pcm
313 */
314enum pcm_format pcm_get_format(const struct pcm *pcm)
315{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700316 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800317}
318
Taylor Holberton6d58e012016-10-01 18:32:30 -0400319/** Gets the file descriptor of the PCM.
320 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800321 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400322 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800323 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400324 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800325int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400326{
327 return pcm->fd;
328}
329
Taylor Holberton6d58e012016-10-01 18:32:30 -0400330/** Gets the error message for the last error that occured.
331 * If no error occured and this function is called, the results are undefined.
332 * @param pcm A PCM handle.
333 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800334 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400335 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800336const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700337{
338 return pcm->error;
339}
340
Taylor Holberton861da7a2017-04-10 12:05:51 -0700341/** Sets the PCM configuration.
342 * @param pcm A PCM handle.
343 * @param config The configuration to use for the
344 * PCM. This parameter may be NULL, in which case
345 * the default configuration is used.
346 * @returns Zero on success, a negative errno value
347 * on failure.
348 * @ingroup libtinyalsa-pcm
349 * */
350int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
351{
352 if (pcm == NULL)
353 return -EFAULT;
354 else if (config == NULL) {
355 config = &pcm->config;
356 pcm->config.channels = 2;
357 pcm->config.rate = 48000;
358 pcm->config.period_size = 1024;
359 pcm->config.period_count = 4;
360 pcm->config.format = PCM_FORMAT_S16_LE;
361 pcm->config.start_threshold = config->period_count * config->period_size;
362 pcm->config.stop_threshold = config->period_count * config->period_size;
363 pcm->config.silence_threshold = 0;
364 } else
365 pcm->config = *config;
366
367 struct snd_pcm_hw_params params;
368 param_init(&params);
369 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
370 pcm_format_to_alsa(config->format));
Taylor Holberton861da7a2017-04-10 12:05:51 -0700371 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700372 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
373 config->channels);
374 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
375 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
376
377 if (pcm->flags & PCM_NOIRQ) {
378
379 if (!(pcm->flags & PCM_MMAP)) {
380 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
381 return -EINVAL;
382 }
383
384 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
385 pcm->noirq_frames_per_msec = config->rate / 1000;
386 }
387
388 if (pcm->flags & PCM_MMAP)
389 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
390 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
391 else
392 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
393 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
394
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700395 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700396 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200397 oops(pcm, errno, "cannot set hw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700398 return -errno_copy;
399 }
400
401 /* get our refined hw_params */
402 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
403 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
404 pcm->buffer_size = config->period_count * config->period_size;
405
406 if (pcm->flags & PCM_MMAP) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530407 pcm->mmap_buffer = pcm->ops->mmap(pcm->data, NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
408 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700409 if (pcm->mmap_buffer == MAP_FAILED) {
410 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200411 oops(pcm, errno, "failed to mmap buffer %d bytes\n",
Taylor Holberton861da7a2017-04-10 12:05:51 -0700412 pcm_frames_to_bytes(pcm, pcm->buffer_size));
413 return -errno_copy;
414 }
415 }
416
417 struct snd_pcm_sw_params sparams;
418 memset(&sparams, 0, sizeof(sparams));
419 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
420 sparams.period_step = 1;
Miguel GAIO05c64c32020-02-06 21:09:31 +0100421 sparams.avail_min = config->period_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700422
423 if (!config->start_threshold) {
424 if (pcm->flags & PCM_IN)
425 pcm->config.start_threshold = sparams.start_threshold = 1;
426 else
427 pcm->config.start_threshold = sparams.start_threshold =
428 config->period_count * config->period_size / 2;
429 } else
430 sparams.start_threshold = config->start_threshold;
431
432 /* pick a high stop threshold - todo: does this need further tuning */
433 if (!config->stop_threshold) {
434 if (pcm->flags & PCM_IN)
435 pcm->config.stop_threshold = sparams.stop_threshold =
436 config->period_count * config->period_size * 10;
437 else
438 pcm->config.stop_threshold = sparams.stop_threshold =
439 config->period_count * config->period_size;
440 }
441 else
442 sparams.stop_threshold = config->stop_threshold;
443
444 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
445 sparams.silence_size = 0;
446 sparams.silence_threshold = config->silence_threshold;
447 pcm->boundary = sparams.boundary = pcm->buffer_size;
448
449 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
450 pcm->boundary *= 2;
451
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700452 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700453 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200454 oops(pcm, errno, "cannot set sw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700455 return -errno_copy;
456 }
457
458 return 0;
459}
460
Taylor Holberton6d58e012016-10-01 18:32:30 -0400461/** Gets the subdevice on which the pcm has been opened.
462 * @param pcm A PCM handle.
463 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800464unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200465{
466 return pcm->subdevice;
467}
468
Taylor Holberton6d58e012016-10-01 18:32:30 -0400469/** Determines the number of bits occupied by a @ref pcm_format.
470 * @param format A PCM format.
471 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800472 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400473 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700474unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700475{
476 switch (format) {
477 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400478 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700479 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400480 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700481 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400482 case PCM_FORMAT_S24_3LE:
483 case PCM_FORMAT_S24_3BE:
484 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700485 default:
486 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400487 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700488 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400489 case PCM_FORMAT_S8:
490 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700491 };
492}
493
Taylor Holberton6d58e012016-10-01 18:32:30 -0400494/** Determines how many frames of a PCM can fit into a number of bytes.
495 * @param pcm A PCM handle.
496 * @param bytes The number of bytes.
497 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800498 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400499 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800500unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700501{
502 return bytes / (pcm->config.channels *
503 (pcm_format_to_bits(pcm->config.format) >> 3));
504}
505
Taylor Holberton6d58e012016-10-01 18:32:30 -0400506/** Determines how many bytes are occupied by a number of frames of a PCM.
507 * @param pcm A PCM handle.
508 * @param frames The number of frames of a PCM.
509 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800510 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400511 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800512unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700513{
514 return frames * pcm->config.channels *
515 (pcm_format_to_bits(pcm->config.format) >> 3);
516}
517
Taylor Holberton4f556062016-09-16 09:54:36 -0400518static int pcm_sync_ptr(struct pcm *pcm, int flags)
519{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200520 if (pcm->sync_ptr == NULL) {
521 /* status and control are mmaped */
522
523 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
524 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
525 oops(pcm, errno, "failed to sync hardware pointer");
526 return -1;
527 }
528 }
529 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700530 pcm->sync_ptr->flags = flags;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700531 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
532 pcm->sync_ptr) < 0) {
Taylor Holbertone123a652017-01-13 21:39:48 -0800533 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700534 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800535 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700536 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200537
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200538 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700539}
540
Taylor Holberton4f556062016-09-16 09:54:36 -0400541static int pcm_hw_mmap_status(struct pcm *pcm)
542{
Eric Laurent40b018e2011-06-18 10:10:23 -0700543 if (pcm->sync_ptr)
544 return 0;
545
546 int page_size = sysconf(_SC_PAGE_SIZE);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530547 pcm->mmap_status = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ, MAP_SHARED,
548 SNDRV_PCM_MMAP_OFFSET_STATUS);
Eric Laurent40b018e2011-06-18 10:10:23 -0700549 if (pcm->mmap_status == MAP_FAILED)
550 pcm->mmap_status = NULL;
551 if (!pcm->mmap_status)
552 goto mmap_error;
553
Rohit kumarf29b8df2020-08-19 15:19:33 +0530554 pcm->mmap_control = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ | PROT_WRITE,
555 MAP_SHARED, SNDRV_PCM_MMAP_OFFSET_CONTROL);
Eric Laurent40b018e2011-06-18 10:10:23 -0700556 if (pcm->mmap_control == MAP_FAILED)
557 pcm->mmap_control = NULL;
558 if (!pcm->mmap_control) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530559 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700560 pcm->mmap_status = NULL;
561 goto mmap_error;
562 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700563
564 return 0;
565
566mmap_error:
567
568 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
569 if (!pcm->sync_ptr)
570 return -ENOMEM;
571 pcm->mmap_status = &pcm->sync_ptr->s.status;
572 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700573
574 return 0;
575}
576
577static void pcm_hw_munmap_status(struct pcm *pcm) {
578 if (pcm->sync_ptr) {
579 free(pcm->sync_ptr);
580 pcm->sync_ptr = NULL;
581 } else {
582 int page_size = sysconf(_SC_PAGE_SIZE);
583 if (pcm->mmap_status)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530584 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700585 if (pcm->mmap_control)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530586 pcm->ops->munmap(pcm->data, pcm->mmap_control, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700587 }
588 pcm->mmap_status = NULL;
589 pcm->mmap_control = NULL;
590}
591
Simon Wilson79d39652011-05-25 13:44:23 -0700592static struct pcm bad_pcm = {
593 .fd = -1,
594};
595
Taylor Holberton6d58e012016-10-01 18:32:30 -0400596/** Gets the hardware parameters of a PCM, without created a PCM handle.
597 * @param card The card of the PCM.
598 * The default card is zero.
599 * @param device The device of the PCM.
600 * The default device is zero.
601 * @param flags Specifies whether the PCM is an input or output.
602 * May be one of the following:
603 * - @ref PCM_IN
604 * - @ref PCM_OUT
605 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800606 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400607 */
Simon Wilson43544882012-10-31 12:52:39 -0700608struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
609 unsigned int flags)
610{
611 struct snd_pcm_hw_params *params;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700612 void *snd_node = NULL, *data;
613 const struct pcm_ops *ops;
Simon Wilson43544882012-10-31 12:52:39 -0700614 int fd;
615
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700616 ops = &hw_ops;
617 fd = ops->open(card, device, flags, &data, snd_node);
Simon Wilson43544882012-10-31 12:52:39 -0700618
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700619#ifdef TINYALSA_USES_PLUGINS
Simon Wilson43544882012-10-31 12:52:39 -0700620 if (fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700621 int pcm_type;
622 snd_node = snd_utils_open_pcm(card, device);
623 pcm_type = snd_utils_get_node_type(snd_node);
624 if (!snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
625 fprintf(stderr, "no device (hw/plugin) for card(%u), device(%u)",
626 card, device);
627 goto err_open;
628 }
629 ops = &plug_ops;
630 fd = ops->open(card, device, flags, &data, snd_node);
631 }
632#endif
633 if (fd < 0) {
634 fprintf(stderr, "cannot open card(%d) device (%d): %s\n",
635 card, device, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700636 goto err_open;
637 }
638
639 params = calloc(1, sizeof(struct snd_pcm_hw_params));
640 if (!params)
641 goto err_calloc;
642
643 param_init(params);
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700644 if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
Simon Wilson43544882012-10-31 12:52:39 -0700645 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
646 goto err_hw_refine;
647 }
648
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700649#ifdef TINYALSA_USES_PLUGINS
650 if (snd_node)
651 snd_utils_close_dev_node(snd_node);
652#endif
653 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700654
655 return (struct pcm_params *)params;
656
657err_hw_refine:
658 free(params);
659err_calloc:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700660#ifdef TINYALSA_USES_PLUGINS
661 if (snd_node)
662 snd_utils_close_dev_node(snd_node);
663#endif
664 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700665err_open:
666 return NULL;
667}
668
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500669/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400670 * @param pcm_params Hardware parameters of a PCM.
671 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800672 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400673 */
Simon Wilson43544882012-10-31 12:52:39 -0700674void pcm_params_free(struct pcm_params *pcm_params)
675{
676 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
677
678 if (params)
679 free(params);
680}
681
682static int pcm_param_to_alsa(enum pcm_param param)
683{
684 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700685 case PCM_PARAM_ACCESS:
686 return SNDRV_PCM_HW_PARAM_ACCESS;
687 case PCM_PARAM_FORMAT:
688 return SNDRV_PCM_HW_PARAM_FORMAT;
689 case PCM_PARAM_SUBFORMAT:
690 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700691 case PCM_PARAM_SAMPLE_BITS:
692 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
693 break;
694 case PCM_PARAM_FRAME_BITS:
695 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
696 break;
697 case PCM_PARAM_CHANNELS:
698 return SNDRV_PCM_HW_PARAM_CHANNELS;
699 break;
700 case PCM_PARAM_RATE:
701 return SNDRV_PCM_HW_PARAM_RATE;
702 break;
703 case PCM_PARAM_PERIOD_TIME:
704 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
705 break;
706 case PCM_PARAM_PERIOD_SIZE:
707 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
708 break;
709 case PCM_PARAM_PERIOD_BYTES:
710 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
711 break;
712 case PCM_PARAM_PERIODS:
713 return SNDRV_PCM_HW_PARAM_PERIODS;
714 break;
715 case PCM_PARAM_BUFFER_TIME:
716 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
717 break;
718 case PCM_PARAM_BUFFER_SIZE:
719 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
720 break;
721 case PCM_PARAM_BUFFER_BYTES:
722 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
723 break;
724 case PCM_PARAM_TICK_TIME:
725 return SNDRV_PCM_HW_PARAM_TICK_TIME;
726 break;
727
728 default:
729 return -1;
730 }
731}
732
Taylor Holberton6d58e012016-10-01 18:32:30 -0400733/** Gets a mask from a PCM's hardware parameters.
734 * @param pcm_params A PCM's hardware parameters.
735 * @param param The parameter to get.
736 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
737 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800738 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400739 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800740const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700741 enum pcm_param param)
742{
743 int p;
744 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
745 if (params == NULL) {
746 return NULL;
747 }
748
749 p = pcm_param_to_alsa(param);
750 if (p < 0 || !param_is_mask(p)) {
751 return NULL;
752 }
753
Taylor Holberton2f387d22016-12-01 15:58:16 -0800754 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700755}
756
Taylor Holberton17a10242016-11-23 13:18:24 -0800757/** Get the minimum of a specified PCM parameter.
758 * @param pcm_params A PCM parameters structure.
759 * @param param The specified parameter to get the minimum of.
760 * @returns On success, the parameter minimum.
761 * On failure, zero.
762 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800763unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700764 enum pcm_param param)
765{
766 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
767 int p;
768
769 if (!params)
770 return 0;
771
772 p = pcm_param_to_alsa(param);
773 if (p < 0)
774 return 0;
775
776 return param_get_min(params, p);
777}
778
Taylor Holberton17a10242016-11-23 13:18:24 -0800779/** Get the maximum of a specified PCM parameter.
780 * @param pcm_params A PCM parameters structure.
781 * @param param The specified parameter to get the maximum of.
782 * @returns On success, the parameter maximum.
783 * On failure, zero.
784 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800785unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700786 enum pcm_param param)
787{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800788 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700789 int p;
790
791 if (!params)
792 return 0;
793
794 p = pcm_param_to_alsa(param);
795 if (p < 0)
796 return 0;
797
798 return param_get_max(params, p);
799}
800
Taylor Holberton6d58e012016-10-01 18:32:30 -0400801/** Closes a PCM returned by @ref pcm_open.
802 * @param pcm A PCM returned by @ref pcm_open.
803 * May not be NULL.
804 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800805 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400806 */
Simon Wilson79d39652011-05-25 13:44:23 -0700807int pcm_close(struct pcm *pcm)
808{
809 if (pcm == &bad_pcm)
810 return 0;
811
Eric Laurent40b018e2011-06-18 10:10:23 -0700812 pcm_hw_munmap_status(pcm);
813
Liam Girdwood6be28f12011-10-13 12:59:51 -0700814 if (pcm->flags & PCM_MMAP) {
815 pcm_stop(pcm);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530816 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -0700817 }
818
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700819 snd_utils_close_dev_node(pcm->snd_node);
820 pcm->ops->close(pcm->data);
Simon Wilson79d39652011-05-25 13:44:23 -0700821 pcm->buffer_size = 0;
822 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700823 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700824 return 0;
825}
826
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800827/** Opens a PCM by it's name.
828 * @param name The name of the PCM.
829 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
830 * @param flags Specify characteristics and functionality about the pcm.
831 * May be a bitwise AND of the following:
832 * - @ref PCM_IN
833 * - @ref PCM_OUT
834 * - @ref PCM_MMAP
835 * - @ref PCM_NOIRQ
836 * - @ref PCM_MONOTONIC
837 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800838 * @returns A PCM structure.
839 * If an error occurs allocating memory for the PCM, NULL is returned.
840 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
841 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800842 * @ingroup libtinyalsa-pcm
843 */
844struct pcm *pcm_open_by_name(const char *name,
845 unsigned int flags,
846 const struct pcm_config *config)
847{
848 unsigned int card, device;
849 if ((name[0] != 'h')
850 || (name[1] != 'w')
851 || (name[2] != ':')) {
852 return NULL;
853 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
854 return NULL;
855 }
856 return pcm_open(card, device, flags, config);
857}
858
Taylor Holberton6d58e012016-10-01 18:32:30 -0400859/** Opens a PCM.
860 * @param card The card that the pcm belongs to.
861 * The default card is zero.
862 * @param device The device that the pcm belongs to.
863 * The default device is zero.
864 * @param flags Specify characteristics and functionality about the pcm.
865 * May be a bitwise AND of the following:
866 * - @ref PCM_IN
867 * - @ref PCM_OUT
868 * - @ref PCM_MMAP
869 * - @ref PCM_NOIRQ
870 * - @ref PCM_MONOTONIC
871 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800872 * @returns A PCM structure.
873 * If an error occurs allocating memory for the PCM, NULL is returned.
874 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
875 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800876 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400877 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700878struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800879 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700880{
Simon Wilson79d39652011-05-25 13:44:23 -0700881 struct pcm *pcm;
882 struct snd_pcm_info info;
Eric Laurent40b018e2011-06-18 10:10:23 -0700883 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700884
885 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400886 if (!pcm)
887 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700888
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700889 /* Default to hw_ops, attemp plugin open only if hw (/dev/snd/pcm*) open fails */
890 pcm->ops = &hw_ops;
891 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, NULL);
Simon Wilson79d39652011-05-25 13:44:23 -0700892
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700893#ifdef TINYALSA_USES_PLUGINS
Simon Wilson79d39652011-05-25 13:44:23 -0700894 if (pcm->fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700895 int pcm_type;
896 pcm->snd_node = snd_utils_open_pcm(card, device);
897 pcm_type = snd_utils_get_node_type(pcm->snd_node);
898 if (!pcm->snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
899 oops(pcm, -ENODEV, "no device (hw/plugin) for card(%u), device(%u)",
900 card, device);
Miguel GAIO859adb22020-04-18 08:40:41 +0200901 goto fail_close_dev_node;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700902 }
903 pcm->ops = &plug_ops;
904 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
905 }
906#endif
907 if (pcm->fd < 0) {
908 oops(pcm, errno, "cannot open device (%u) for card (%u)",
909 device, card);
Miguel GAIO859adb22020-04-18 08:40:41 +0200910 goto fail_close_dev_node;
Simon Wilson79d39652011-05-25 13:44:23 -0700911 }
912
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700913 pcm->flags = flags;
914
915 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700916 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700917 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700918 }
David Wagner4cddf192014-04-02 15:12:54 +0200919 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700920
Taylor Holberton861da7a2017-04-10 12:05:51 -0700921 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700922 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700923
Eric Laurent40b018e2011-06-18 10:10:23 -0700924 rc = pcm_hw_mmap_status(pcm);
925 if (rc < 0) {
926 oops(pcm, rc, "mmap status failed");
927 goto fail;
928 }
929
Glenn Kasten81012402013-08-22 15:11:48 -0700930#ifdef SNDRV_PCM_IOCTL_TTSTAMP
931 if (pcm->flags & PCM_MONOTONIC) {
932 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700933 rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
Glenn Kasten81012402013-08-22 15:11:48 -0700934 if (rc < 0) {
935 oops(pcm, rc, "cannot set timestamp type");
936 goto fail;
937 }
938 }
939#endif
940
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -0300941 /* prepare here so the user does not need to do this later */
942 if (pcm_prepare(pcm))
943 goto fail;
944
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200945 pcm->xruns = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700946 return pcm;
947
948fail:
Ricardo Biehl Pasquali0bfac892019-01-04 14:45:35 -0200949 pcm_hw_munmap_status(pcm);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700950 if (flags & PCM_MMAP)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530951 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -0700952fail_close:
Miguel GAIO859adb22020-04-18 08:40:41 +0200953 pcm->ops->close(pcm->data);
954fail_close_dev_node:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700955#ifdef TINYALSA_USES_PLUGINS
956 if (pcm->snd_node)
957 snd_utils_close_dev_node(pcm->snd_node);
958#endif
Miguel GAIO859adb22020-04-18 08:40:41 +0200959 free(pcm);
Lubomir Rintel00f5aa12020-04-10 09:10:33 +0200960 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700961}
962
Taylor Holberton6d58e012016-10-01 18:32:30 -0400963/** Checks if a PCM file has been opened without error.
964 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -0800965 * May be NULL.
966 * @return If a PCM's file descriptor is not valid or the pointer is NULL, it returns zero.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400967 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800968 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400969 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800970int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700971{
Taylor Holbertone123a652017-01-13 21:39:48 -0800972 if (pcm != NULL) {
973 return pcm->fd >= 0;
974 }
975 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700976}
Simon Wilsond6458e62011-06-21 14:58:11 -0700977
Taylor Holberton558e5942016-12-04 13:42:28 -0800978/** Links two PCMs.
979 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
980 * If an error occurs, the error message will be written to @p pcm1.
981 * @param pcm1 A PCM handle.
982 * @param pcm2 Another PCM handle.
983 * @return On success, zero; on failure, a negative number.
984 * @ingroup libtinyalsa-pcm
985 */
986int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
987{
988 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
989 if (err == -1) {
990 return oops(pcm1, errno, "cannot link PCM");
991 }
992 return 0;
993}
994
995/** Unlinks a PCM.
996 * @see @ref pcm_link
997 * @param pcm A PCM handle.
998 * @return On success, zero; on failure, a negative number.
999 * @ingroup libtinyalsa-pcm
1000 */
1001int pcm_unlink(struct pcm *pcm)
1002{
1003 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1004 if (err == -1) {
1005 return oops(pcm, errno, "cannot unlink PCM");
1006 }
1007 return 0;
1008}
1009
Taylor Holberton6d58e012016-10-01 18:32:30 -04001010/** Prepares a PCM, if it has not been prepared already.
1011 * @param pcm A PCM handle.
1012 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001013 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001014 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301015int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001016{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001017 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001018 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001019
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001020 /* get appl_ptr and avail_min from kernel */
1021 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1022
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301023 return 0;
1024}
1025
Taylor Holberton6d58e012016-10-01 18:32:30 -04001026/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001027 * @param pcm A PCM handle.
1028 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001029 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001030 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301031int pcm_start(struct pcm *pcm)
1032{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001033 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -02001034 if (pcm_sync_ptr(pcm, 0) < 0)
1035 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001036
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001037 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001038 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001039 return oops(pcm, errno, "cannot start channel");
1040 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001041
1042 return 0;
1043}
1044
Taylor Holberton6d58e012016-10-01 18:32:30 -04001045/** Stops a PCM.
1046 * @param pcm A PCM handle.
1047 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001048 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001049 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001050int pcm_stop(struct pcm *pcm)
1051{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001052 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001053 return oops(pcm, errno, "cannot stop channel");
1054
1055 return 0;
1056}
1057
Liam Girdwood6be28f12011-10-13 12:59:51 -07001058static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1059{
1060 int avail;
1061
1062 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1063
1064 if (avail < 0)
1065 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001066 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001067 avail -= pcm->boundary;
1068
1069 return avail;
1070}
1071
1072static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1073{
1074 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1075 if (avail < 0)
1076 avail += pcm->boundary;
1077 return avail;
1078}
1079
1080static inline int pcm_mmap_avail(struct pcm *pcm)
1081{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001082 if (pcm->flags & PCM_IN)
1083 return pcm_mmap_capture_avail(pcm);
1084 else
1085 return pcm_mmap_playback_avail(pcm);
1086}
1087
1088static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1089{
1090 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1091 appl_ptr += frames;
1092
1093 /* check for boundary wrap */
1094 if (appl_ptr > pcm->boundary)
1095 appl_ptr -= pcm->boundary;
1096 pcm->mmap_control->appl_ptr = appl_ptr;
1097}
1098
1099int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1100 unsigned int *frames)
1101{
1102 unsigned int continuous, copy_frames, avail;
1103
1104 /* return the mmap buffer */
1105 *areas = pcm->mmap_buffer;
1106
1107 /* and the application offset in frames */
1108 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1109
1110 avail = pcm_mmap_avail(pcm);
1111 if (avail > pcm->buffer_size)
1112 avail = pcm->buffer_size;
1113 continuous = pcm->buffer_size - *offset;
1114
1115 /* we can only copy frames if the are availabale and continuos */
1116 copy_frames = *frames;
1117 if (copy_frames > avail)
1118 copy_frames = avail;
1119 if (copy_frames > continuous)
1120 copy_frames = continuous;
1121 *frames = copy_frames;
1122
1123 return 0;
1124}
1125
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001126static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1127 char *buf, unsigned int src_offset,
1128 unsigned int frames)
1129{
1130 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1131 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1132 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1133
1134 /* interleaved only atm */
1135 if (pcm->flags & PCM_IN)
1136 memcpy(buf + src_offset_bytes,
1137 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1138 size_bytes);
1139 else
1140 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1141 buf + src_offset_bytes,
1142 size_bytes);
1143 return 0;
1144}
1145
Liam Girdwood6be28f12011-10-13 12:59:51 -07001146int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1147{
Taylor Holberton72e44222016-11-22 09:54:47 -08001148 int ret;
1149
Taylor Holberton73466c02016-10-01 12:51:59 -04001150 /* not used */
1151 (void) offset;
1152
Liam Girdwood6be28f12011-10-13 12:59:51 -07001153 /* update the application pointer in userspace and kernel */
1154 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001155 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001156 if (ret != 0){
1157 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001158 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001159 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001160
1161 return frames;
1162}
1163
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001164static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1165 unsigned int offset, unsigned int size)
1166{
1167 void *pcm_areas;
1168 int commit;
1169 unsigned int pcm_offset, frames, count = 0;
1170
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001171 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001172 frames = size;
1173 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1174 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1175 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1176 if (commit < 0) {
1177 oops(pcm, commit, "failed to commit %d frames\n", frames);
1178 return commit;
1179 }
1180
1181 offset += commit;
1182 count += commit;
1183 size -= commit;
1184 }
1185 return count;
1186}
1187
Liam Girdwood6be28f12011-10-13 12:59:51 -07001188int pcm_avail_update(struct pcm *pcm)
1189{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001190 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001191 return pcm_mmap_avail(pcm);
1192}
1193
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001194/** Returns available frames in pcm buffer and corresponding time stamp.
1195 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1196 * otherwise the clock is CLOCK_REALTIME.
1197 * For an input stream, frames available are frames ready for the application to read.
1198 * For an output stream, frames available are the number of empty frames available for the application to write.
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001199 * @param pcm A PCM handle.
1200 * @param avail The number of available frames
1201 * @param tstamp The timestamp
1202 * @return On success, zero is returned; on failure, negative one.
1203 */
1204int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1205 struct timespec *tstamp)
1206{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001207 int checking;
1208 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001209
1210 if (!pcm_is_ready(pcm))
1211 return -1;
1212
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001213 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001214
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001215again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001216
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001217 tmp = pcm_avail_update(pcm);
1218 if (tmp < 0)
1219 return tmp; /* error */
1220
1221 if (checking && (unsigned int) tmp == *avail)
1222 return 0;
1223
1224 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001225 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001226
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001227 /*
1228 * When status is mmaped, get avail again to ensure
1229 * valid timestamp.
1230 */
1231 if (!pcm->sync_ptr) {
1232 checking = 1;
1233 goto again;
1234 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001235
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001236 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001237 return 0;
1238}
1239
Liam Girdwood6be28f12011-10-13 12:59:51 -07001240int pcm_state(struct pcm *pcm)
1241{
1242 int err = pcm_sync_ptr(pcm, 0);
1243 if (err < 0)
1244 return err;
1245
1246 return pcm->mmap_status->state;
1247}
1248
Taylor Holberton17a10242016-11-23 13:18:24 -08001249/** Waits for frames to be available for read or write operations.
1250 * @param pcm A PCM handle.
1251 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1252 * @returns If frames became available, one is returned.
1253 * If a timeout occured, zero is returned.
1254 * If an error occured, a negative number is returned.
1255 * @ingroup libtinyalsa-pcm
1256 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001257int pcm_wait(struct pcm *pcm, int timeout)
1258{
1259 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001260 int err;
1261
1262 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001263 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001264
1265 do {
1266 /* let's wait for avail or timeout */
Rohit kumarf29b8df2020-08-19 15:19:33 +05301267 err = pcm->ops->poll(pcm->data, &pfd, 1, timeout);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001268 if (err < 0)
1269 return -errno;
1270
1271 /* timeout ? */
1272 if (err == 0)
1273 return 0;
1274
1275 /* have we been interrupted ? */
1276 if (errno == -EINTR)
1277 continue;
1278
1279 /* check for any errors */
1280 if (pfd.revents & (POLLERR | POLLNVAL)) {
1281 switch (pcm_state(pcm)) {
1282 case PCM_STATE_XRUN:
1283 return -EPIPE;
1284 case PCM_STATE_SUSPENDED:
1285 return -ESTRPIPE;
1286 case PCM_STATE_DISCONNECTED:
1287 return -ENODEV;
1288 default:
1289 return -EIO;
1290 }
1291 }
1292 /* poll again if fd not ready for IO */
1293 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1294
1295 return 1;
1296}
1297
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001298/*
1299 * Transfer data to/from mmaped buffer. This imitates the
1300 * behavior of read/write system calls.
1301 *
1302 * However, this doesn't seems to offer any advantage over
1303 * the read/write syscalls. Should it be removed?
1304 */
1305int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001306{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001307 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001308
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001309 int state;
1310 unsigned int avail;
1311 unsigned int user_offset;
1312
1313 int err;
1314 int tmp;
1315
1316 is_playback = !(pcm->flags & PCM_IN);
1317
1318 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001319 return 0;
1320
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001321 /* update hardware pointer and get state */
1322 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1323 SNDRV_PCM_SYNC_PTR_APPL |
1324 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1325 if (err == -1)
1326 return -1;
1327 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001328
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001329 /*
1330 * If frames < start_threshold, wait indefinitely.
1331 * Another thread may start capture
1332 */
1333 if (!is_playback && state == PCM_STATE_PREPARED &&
1334 frames >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001335 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001336 if (err == -1)
1337 return -1;
1338 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001339 }
1340
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001341 avail = pcm_mmap_avail(pcm);
1342 user_offset = 0;
1343
1344 while (frames) {
1345 if (!avail) {
1346 if (pcm->flags & PCM_NONBLOCK) {
1347 errno = EAGAIN;
1348 break;
1349 }
1350
1351 /* wait for interrupt */
1352 err = pcm_wait(pcm, -1);
1353 if (err < 0) {
1354 errno = -err;
1355 break;
1356 }
1357
1358 /* get hardware pointer */
1359 avail = pcm_avail_update(pcm);
1360 }
1361
1362 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1363 if (tmp < 0)
1364 break;
1365
1366 user_offset += tmp;
1367 frames -= tmp;
1368 avail -= tmp;
1369
1370 /* start playback if written >= start_threshold */
1371 if (is_playback && state == PCM_STATE_PREPARED &&
1372 pcm->buffer_size - avail >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001373 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001374 if (err == -1)
1375 break;
1376 }
1377 }
1378
1379 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001380}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001381
1382int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1383{
1384 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1385 return -ENOSYS;
1386
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001387 return pcm_mmap_transfer(pcm, (void *)data,
1388 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001389}
1390
1391int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1392{
1393 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1394 return -ENOSYS;
1395
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001396 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001397}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301398
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001399static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1400{
1401 int is_playback;
1402
1403 struct snd_xferi transfer;
1404 int res;
1405
1406 is_playback = !(pcm->flags & PCM_IN);
1407
1408 transfer.buf = data;
1409 transfer.frames = frames;
1410 transfer.result = 0;
1411
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001412 res = pcm->ops->ioctl(pcm->data, is_playback
1413 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1414 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001415
1416 return res == 0 ? (int) transfer.result : -1;
1417}
1418
1419static int pcm_generic_transfer(struct pcm *pcm, void *data,
1420 unsigned int frames)
1421{
1422 int res;
1423
1424#if UINT_MAX > TINYALSA_FRAMES_MAX
1425 if (frames > TINYALSA_FRAMES_MAX)
1426 return -EINVAL;
1427#endif
1428 if (frames > INT_MAX)
1429 return -EINVAL;
1430
1431again:
1432
1433 if (pcm->flags & PCM_MMAP)
1434 res = pcm_mmap_transfer(pcm, data, frames);
1435 else
1436 res = pcm_rw_transfer(pcm, data, frames);
1437
1438 if (res < 0) {
1439 switch (errno) {
1440 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001441 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001442 /* fallthrough */
1443 case ESTRPIPE:
1444 /*
1445 * Try to restart if we are allowed to do so.
1446 * Otherwise, return error.
1447 */
1448 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1449 return -1;
1450 goto again;
1451 case EAGAIN:
1452 if (pcm->flags & PCM_NONBLOCK)
1453 return -1;
1454 /* fallthrough */
1455 default:
1456 return oops(pcm, errno, "cannot read/write stream data");
1457 }
1458 }
1459
1460 return res;
1461}
1462
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001463/** Writes audio samples to PCM.
1464 * If the PCM has not been started, it is started in this function.
1465 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1466 * @param pcm A PCM handle.
1467 * @param data The audio sample array
1468 * @param frame_count The number of frames occupied by the sample array.
1469 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1470 * or INT_MAX.
1471 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1472 * @ingroup libtinyalsa-pcm
1473 */
1474int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1475{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001476 if (pcm->flags & PCM_IN)
1477 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001478
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001479 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001480}
1481
1482/** Reads audio samples from PCM.
1483 * If the PCM has not been started, it is started in this function.
1484 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1485 * @param pcm A PCM handle.
1486 * @param data The audio sample array
1487 * @param frame_count The number of frames occupied by the sample array.
1488 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1489 * or INT_MAX.
1490 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1491 * @ingroup libtinyalsa-pcm
1492 */
1493int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1494{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001495 if (!(pcm->flags & PCM_IN))
1496 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001497
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001498 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001499}
1500
1501/** Writes audio samples to PCM.
1502 * If the PCM has not been started, it is started in this function.
1503 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1504 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1505 * @param pcm A PCM handle.
1506 * @param data The audio sample array
1507 * @param count The number of bytes occupied by the sample array.
1508 * @return On success, this function returns zero; otherwise, a negative number.
1509 * @deprecated
1510 * @ingroup libtinyalsa-pcm
1511 */
1512int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1513{
Kui Wang4655fed2020-11-01 23:27:53 +08001514 unsigned int requested_frames = pcm_bytes_to_frames(pcm, count);
1515 int ret = pcm_writei(pcm, data, requested_frames);
1516
1517 if (ret < 0)
1518 return ret;
1519
1520 return ((unsigned int )ret == requested_frames) ? 0 : -EIO;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001521}
1522
1523/** Reads audio samples from PCM.
1524 * If the PCM has not been started, it is started in this function.
1525 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1526 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1527 * @param pcm A PCM handle.
1528 * @param data The audio sample array
1529 * @param count The number of bytes occupied by the sample array.
1530 * @return On success, this function returns zero; otherwise, a negative number.
1531 * @deprecated
1532 * @ingroup libtinyalsa-pcm
1533 */
1534int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1535{
Kui Wang4655fed2020-11-01 23:27:53 +08001536 unsigned int requested_frames = pcm_bytes_to_frames(pcm, count);
1537 int ret = pcm_readi(pcm, data, requested_frames);
1538
1539 if (ret < 0)
1540 return ret;
1541
1542 return ((unsigned int )ret == requested_frames) ? 0 : -EIO;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001543}
1544
Taylor Holberton17a10242016-11-23 13:18:24 -08001545/** Gets the delay of the PCM, in terms of frames.
1546 * @param pcm A PCM handle.
1547 * @returns On success, the delay of the PCM.
1548 * On failure, a negative number.
1549 * @ingroup libtinyalsa-pcm
1550 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301551long pcm_get_delay(struct pcm *pcm)
1552{
1553 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1554 return -1;
1555
1556 return pcm->pcm_delay;
1557}