Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1 | /* 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 Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 36 | #include <poll.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 37 | |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/time.h> |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 41 | #include <limits.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 42 | |
| 43 | #include <linux/ioctl.h> |
| 44 | #define __force |
| 45 | #define __bitwise |
| 46 | #define __user |
| 47 | #include <sound/asound.h> |
| 48 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 49 | #include <tinyalsa/asoundlib.h> |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 50 | #include "pcm_io.h" |
| 51 | #include "snd_utils.h" |
| 52 | |
| 53 | enum { |
| 54 | PCM_NODE_TYPE_HW = 0, |
| 55 | PCM_NODE_TYPE_PLUGIN, |
| 56 | }; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 57 | |
| 58 | #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL |
| 59 | |
Andy Hung | 70530a6 | 2014-03-26 18:29:07 -0700 | [diff] [blame] | 60 | /* 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. */ |
| 73 | static 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. */ |
| 82 | static 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 Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 133 | extern struct pcm_ops hw_ops; |
| 134 | extern struct pcm_ops plug_ops; |
| 135 | |
Andy Hung | 70530a6 | 2014-03-26 18:29:07 -0700 | [diff] [blame] | 136 | /* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */ |
| 137 | static const char * const subformat_lookup[] = { |
| 138 | "STD", |
| 139 | }; |
| 140 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 141 | static 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 | |
| 147 | static 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 | |
| 153 | static 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 | |
| 158 | static 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 | |
| 163 | static 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 | |
| 175 | static 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 183 | static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n) |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 184 | { |
| 185 | if (param_is_interval(n)) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 186 | struct snd_interval *i = param_to_interval(p, n); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 187 | return i->min; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 192 | static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val) |
Paul McLean | b25ece4 | 2014-03-21 15:27:34 -0700 | [diff] [blame] | 193 | { |
| 194 | if (param_is_interval(n)) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 195 | struct snd_interval *i = param_to_interval(p, n); |
| 196 | i->max = val; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static 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 Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 204 | return i->max; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 209 | static 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 Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 219 | static 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 Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 229 | static 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 Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 246 | p->rmask = ~0U; |
| 247 | p->cmask = 0; |
| 248 | p->info = ~0U; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | #define PCM_ERROR_MAX 128 |
| 252 | |
| 253 | struct pcm { |
| 254 | int fd; |
| 255 | unsigned int flags; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 256 | int running:1; |
| 257 | int prepared:1; |
| 258 | int underruns; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 259 | unsigned int buffer_size; |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 260 | unsigned int boundary; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 261 | char error[PCM_ERROR_MAX]; |
| 262 | struct pcm_config config; |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 263 | struct snd_pcm_mmap_status *mmap_status; |
| 264 | struct snd_pcm_mmap_control *mmap_control; |
| 265 | struct snd_pcm_sync_ptr *sync_ptr; |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 266 | void *mmap_buffer; |
| 267 | unsigned int noirq_frames_per_msec; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 268 | int wait_for_avail_min; |
Gaio, MiguelX | 080294e | 2014-11-21 00:59:57 -0800 | [diff] [blame] | 269 | unsigned int subdevice; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 270 | |
| 271 | struct pcm_ops *ops; |
| 272 | void *data; |
| 273 | void *snd_node; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 276 | unsigned int pcm_get_buffer_size(struct pcm *pcm) |
| 277 | { |
| 278 | return pcm->buffer_size; |
| 279 | } |
| 280 | |
| 281 | const char* pcm_get_error(struct pcm *pcm) |
| 282 | { |
| 283 | return pcm->error; |
| 284 | } |
| 285 | |
| 286 | unsigned int pcm_get_subdevice(struct pcm *pcm) |
| 287 | { |
| 288 | return pcm->subdevice; |
| 289 | } |
| 290 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 291 | static 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 Muir | 50028cd | 2017-01-23 13:38:31 -0800 | [diff] [blame] | 301 | if (e) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 302 | snprintf(pcm->error + sz, PCM_ERROR_MAX - sz, |
| 303 | ": %s", strerror(e)); |
| 304 | return -1; |
| 305 | } |
| 306 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 307 | static unsigned int pcm_format_to_alsa(enum pcm_format format) |
Simon Wilson | 83b1d6d | 2011-06-15 17:20:55 -0700 | [diff] [blame] | 308 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 309 | 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 Wilson | 83b1d6d | 2011-06-15 17:20:55 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 324 | unsigned int pcm_format_to_bits(enum pcm_format format) |
Simon Wilson | 83b1d6d | 2011-06-15 17:20:55 -0700 | [diff] [blame] | 325 | { |
| 326 | switch (format) { |
| 327 | case PCM_FORMAT_S32_LE: |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 328 | case PCM_FORMAT_S24_LE: |
Simon Wilson | 83b1d6d | 2011-06-15 17:20:55 -0700 | [diff] [blame] | 329 | return 32; |
Glenn Kasten | d9837d0 | 2014-01-31 07:56:33 -0800 | [diff] [blame] | 330 | case PCM_FORMAT_S24_3LE: |
| 331 | return 24; |
Simon Wilson | 83b1d6d | 2011-06-15 17:20:55 -0700 | [diff] [blame] | 332 | default: |
| 333 | case PCM_FORMAT_S16_LE: |
| 334 | return 16; |
| 335 | }; |
| 336 | } |
| 337 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 338 | unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 339 | { |
| 340 | return bytes / (pcm->config.channels * |
| 341 | (pcm_format_to_bits(pcm->config.format) >> 3)); |
| 342 | } |
| 343 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 344 | unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 345 | { |
| 346 | return frames * pcm->config.channels * |
| 347 | (pcm_format_to_bits(pcm->config.format) >> 3); |
| 348 | } |
| 349 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 350 | static int pcm_sync_ptr(struct pcm *pcm, int flags) { |
| 351 | if (pcm->sync_ptr) { |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 352 | pcm->sync_ptr->flags = flags; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 353 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR, |
| 354 | pcm->sync_ptr) < 0) |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 360 | static int pcm_hw_mmap_status(struct pcm *pcm) { |
| 361 | |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 362 | if (pcm->sync_ptr) |
| 363 | return 0; |
| 364 | |
| 365 | int page_size = sysconf(_SC_PAGE_SIZE); |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 366 | pcm->mmap_status = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED, |
| 367 | SNDRV_PCM_MMAP_OFFSET_STATUS); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 368 | if (pcm->mmap_status == MAP_FAILED) |
| 369 | pcm->mmap_status = NULL; |
| 370 | if (!pcm->mmap_status) |
| 371 | goto mmap_error; |
| 372 | |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 373 | 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 Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 375 | if (pcm->mmap_control == MAP_FAILED) |
| 376 | pcm->mmap_control = NULL; |
| 377 | if (!pcm->mmap_control) { |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 378 | pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 379 | pcm->mmap_status = NULL; |
| 380 | goto mmap_error; |
| 381 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 382 | 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 Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | |
| 389 | mmap_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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 396 | 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 Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static 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 Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 413 | pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 414 | if (pcm->mmap_control) |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 415 | pcm->ops->munmap(pcm->data, pcm->mmap_control, page_size); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 416 | } |
| 417 | pcm->mmap_status = NULL; |
| 418 | pcm->mmap_control = NULL; |
| 419 | } |
| 420 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 421 | static 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 | |
| 441 | static 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 | |
| 465 | int 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 | |
| 503 | int 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 | |
| 534 | int 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 Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 550 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 551 | return oops(pcm, errno, "cannot write initial data"); |
| 552 | pcm->running = 1; |
| 553 | return 0; |
| 554 | } |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 555 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 556 | 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 | |
| 573 | int 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 Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 591 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 592 | 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 Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 605 | static struct pcm bad_pcm = { |
| 606 | .fd = -1, |
| 607 | }; |
| 608 | |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 609 | struct pcm_params *pcm_params_get(unsigned int card, unsigned int device, |
| 610 | unsigned int flags) |
| 611 | { |
| 612 | struct snd_pcm_hw_params *params; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 613 | enum snd_node_type pcm_type; |
| 614 | struct pcm_ops *ops; |
| 615 | void *snd_node, *data; |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 616 | int fd; |
| 617 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 618 | 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 Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 624 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 625 | fd = ops->open(card, device, flags, &data, snd_node); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 626 | if (fd < 0) { |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 627 | fprintf(stderr, "cannot open device %u for card %u\n", |
| 628 | device, card); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 629 | 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 Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 637 | if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) { |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 638 | fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno); |
| 639 | goto err_hw_refine; |
| 640 | } |
| 641 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 642 | snd_utils_put_dev_node(snd_node); |
| 643 | ops->close(data); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 644 | |
| 645 | return (struct pcm_params *)params; |
| 646 | |
| 647 | err_hw_refine: |
| 648 | free(params); |
| 649 | err_calloc: |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 650 | ops->close(data); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 651 | err_open: |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 652 | snd_utils_put_dev_node(snd_node); |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | void 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 | |
| 664 | static int pcm_param_to_alsa(enum pcm_param param) |
| 665 | { |
| 666 | switch (param) { |
Andy Hung | a5b44d9 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 667 | 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 Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 673 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 715 | struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params, |
Andy Hung | a5b44d9 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 716 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 729 | return (struct pcm_mask *)param_to_mask(params, p); |
Andy Hung | a5b44d9 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Glenn Kasten | 4213b7b | 2020-09-22 14:54:43 -0700 | [diff] [blame] | 732 | unsigned int pcm_params_get_min(const struct pcm_params *pcm_params, |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 733 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 748 | void 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 Kasten | 4213b7b | 2020-09-22 14:54:43 -0700 | [diff] [blame] | 764 | unsigned int pcm_params_get_max(const struct pcm_params *pcm_params, |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 765 | enum pcm_param param) |
| 766 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 767 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
Simon Wilson | 42fc2d3 | 2012-12-03 11:18:57 -0800 | [diff] [blame] | 768 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 780 | void 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 | |
| 796 | static int pcm_mask_test(struct pcm_mask *m, unsigned int index) |
Andy Hung | 70530a6 | 2014-03-26 18:29:07 -0700 | [diff] [blame] | 797 | { |
| 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 808 | static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size, |
Andy Hung | 70530a6 | 2014-03-26 18:29:07 -0700 | [diff] [blame] | 809 | 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 | |
| 833 | int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size) |
| 834 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 835 | struct pcm_mask *m; |
Andy Hung | 70530a6 | 2014-03-26 18:29:07 -0700 | [diff] [blame] | 836 | 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 | |
| 868 | int 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 Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 877 | int pcm_close(struct pcm *pcm) |
| 878 | { |
| 879 | if (pcm == &bad_pcm) |
| 880 | return 0; |
| 881 | |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 882 | pcm_hw_munmap_status(pcm); |
| 883 | |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 884 | if (pcm->flags & PCM_MMAP) { |
| 885 | pcm_stop(pcm); |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 886 | pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Weiyin Jiang | 1d53207 | 2020-03-14 15:36:40 +0800 | [diff] [blame] | 889 | if (pcm->data) |
| 890 | pcm->ops->close(pcm->data); |
| 891 | if (pcm->snd_node) |
| 892 | snd_utils_put_dev_node(pcm->snd_node); |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 893 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 894 | pcm->prepared = 0; |
| 895 | pcm->running = 0; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 896 | pcm->buffer_size = 0; |
| 897 | pcm->fd = -1; |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 898 | free(pcm); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 902 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 903 | unsigned int flags, struct pcm_config *config) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 904 | { |
| 905 | struct pcm *pcm; |
| 906 | struct snd_pcm_info info; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 907 | struct snd_pcm_hw_params params; |
| 908 | struct snd_pcm_sw_params sparams; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 909 | int rc, pcm_type; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 910 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 911 | if (!config) { |
| 912 | return &bad_pcm; /* TODO: could support default config here */ |
| 913 | } |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 914 | pcm = calloc(1, sizeof(struct pcm)); |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 915 | if (!pcm) { |
| 916 | oops(&bad_pcm, ENOMEM, "can't allocate PCM object"); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 917 | return &bad_pcm; /* TODO: could support default config here */ |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 918 | } |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 919 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 920 | pcm->config = *config; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 921 | pcm->flags = flags; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 922 | |
| 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 931 | if (pcm->fd < 0) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 932 | oops(&bad_pcm, errno, "cannot open device %u for card %u", |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 933 | device, card); |
| 934 | goto fail_open; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 935 | } |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 936 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 937 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 938 | oops(&bad_pcm, errno, "cannot get info"); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 939 | goto fail_close; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 940 | } |
Gaio, MiguelX | 080294e | 2014-11-21 00:59:57 -0800 | [diff] [blame] | 941 | pcm->subdevice = info.subdevice; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 942 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 943 | param_init(¶ms); |
| 944 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 945 | pcm_format_to_alsa(config->format)); |
| 946 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 947 | SNDRV_PCM_SUBFORMAT_STD); |
| 948 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 949 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 950 | pcm_format_to_bits(config->format)); |
| 951 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 952 | pcm_format_to_bits(config->format) * config->channels); |
| 953 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 954 | config->channels); |
| 955 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 956 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 957 | |
| 958 | if (flags & PCM_NOIRQ) { |
| 959 | if (!(flags & PCM_MMAP)) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 960 | oops(&bad_pcm, EINVAL, "noirq only currently supported with mmap()."); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 961 | 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(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 970 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 971 | else |
| 972 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 973 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 974 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 975 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 976 | oops(&bad_pcm, errno, "cannot set hw params"); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 977 | goto fail_close; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* get our refined hw_params */ |
| 981 | config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 982 | config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 983 | pcm->buffer_size = config->period_count * config->period_size; |
| 984 | |
| 985 | if (flags & PCM_MMAP) { |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 986 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 989 | if (pcm->mmap_buffer == MAP_FAILED) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 990 | oops(&bad_pcm, errno, "failed to mmap buffer %d bytes\n", |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 991 | 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 Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1037 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 1038 | oops(&bad_pcm, errno, "cannot set sw params"); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1039 | goto fail; |
| 1040 | } |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1041 | |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 1042 | rc = pcm_hw_mmap_status(pcm); |
| 1043 | if (rc < 0) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 1044 | oops(&bad_pcm, errno, "mmap status failed"); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 1045 | goto fail; |
| 1046 | } |
| 1047 | |
Glenn Kasten | 6b0a206 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 1048 | #ifdef SNDRV_PCM_IOCTL_TTSTAMP |
| 1049 | if (pcm->flags & PCM_MONOTONIC) { |
| 1050 | int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1051 | rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg); |
Glenn Kasten | 6b0a206 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 1052 | if (rc < 0) { |
David Li | 70f656f | 2021-06-30 16:57:41 +0800 | [diff] [blame] | 1053 | oops(&bad_pcm, errno, "cannot set timestamp type"); |
Glenn Kasten | 6b0a206 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 1054 | goto fail; |
| 1055 | } |
| 1056 | } |
| 1057 | #endif |
| 1058 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1059 | pcm->underruns = 0; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1060 | return pcm; |
| 1061 | |
| 1062 | fail: |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1063 | if (flags & PCM_MMAP) |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 1064 | pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1065 | fail_close: |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1066 | pcm->ops->close(pcm->data); |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1067 | |
| 1068 | fail_open: |
| 1069 | snd_utils_put_dev_node(pcm->snd_node); |
David Li | 16d3636 | 2021-03-08 11:39:52 +0000 | [diff] [blame] | 1070 | free(pcm); |
| 1071 | return &bad_pcm; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1074 | int pcm_is_ready(struct pcm *pcm) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1075 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1076 | return pcm->fd >= 0; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1077 | } |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1078 | |
Omair Mohammed Abdullah | 7a0b995 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 1079 | int pcm_prepare(struct pcm *pcm) |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1080 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1081 | if (pcm->prepared) |
| 1082 | return 0; |
| 1083 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1084 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0) |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1085 | return oops(pcm, errno, "cannot prepare channel"); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1086 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1087 | pcm->prepared = 1; |
Omair Mohammed Abdullah | 7a0b995 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | int pcm_start(struct pcm *pcm) |
| 1092 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1093 | int prepare_error = pcm_prepare(pcm); |
| 1094 | if (prepare_error) |
| 1095 | return prepare_error; |
Omair Mohammed Abdullah | 7a0b995 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 1096 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1097 | if (pcm->flags & PCM_MMAP) |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1098 | pcm_sync_ptr(pcm, 0); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1099 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1100 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0) |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1101 | return oops(pcm, errno, "cannot start channel"); |
| 1102 | |
| 1103 | pcm->running = 1; |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | int pcm_stop(struct pcm *pcm) |
| 1108 | { |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1109 | if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0) |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1110 | return oops(pcm, errno, "cannot stop channel"); |
| 1111 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1112 | pcm->prepared = 0; |
| 1113 | pcm->running = 0; |
Simon Wilson | 70d7708 | 2011-06-24 11:08:10 -0700 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
| 1116 | |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1117 | static 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1125 | else if (avail > (int)pcm->boundary) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1126 | avail -= pcm->boundary; |
| 1127 | |
| 1128 | return avail; |
| 1129 | } |
| 1130 | |
| 1131 | static 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 Reid | 9074cfc | 2015-09-01 11:01:57 -0700 | [diff] [blame] | 1139 | int pcm_mmap_avail(struct pcm *pcm) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1140 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1141 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1142 | if (pcm->flags & PCM_IN) |
| 1143 | return pcm_mmap_capture_avail(pcm); |
| 1144 | else |
| 1145 | return pcm_mmap_playback_avail(pcm); |
| 1146 | } |
| 1147 | |
| 1148 | static 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 | |
| 1159 | int 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1186 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset __attribute__((unused)), unsigned int frames) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1187 | { |
| 1188 | /* update the application pointer in userspace and kernel */ |
| 1189 | pcm_mmap_appl_forward(pcm, frames); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1190 | pcm_sync_ptr(pcm, 0); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1191 | |
| 1192 | return frames; |
| 1193 | } |
| 1194 | |
| 1195 | int pcm_avail_update(struct pcm *pcm) |
| 1196 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1197 | pcm_sync_ptr(pcm, 0); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1198 | return pcm_mmap_avail(pcm); |
| 1199 | } |
| 1200 | |
| 1201 | int 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1210 | int 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 Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1219 | int pcm_wait(struct pcm *pcm, int timeout) |
| 1220 | { |
| 1221 | struct pollfd pfd; |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1222 | int err; |
| 1223 | |
| 1224 | pfd.fd = pcm->fd; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1225 | pfd.events = POLLOUT | POLLERR | POLLNVAL; |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1226 | |
| 1227 | do { |
| 1228 | /* let's wait for avail or timeout */ |
Phani Kumar Uppalapati | 3c70c56 | 2019-10-23 16:08:08 +0530 | [diff] [blame] | 1229 | err = pcm->ops->poll(pcm->data, &pfd, 1, timeout); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1230 | 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1260 | int pcm_get_poll_fd(struct pcm *pcm) |
Dylan Reid | cefba1e | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1261 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1262 | return pcm->fd; |
| 1263 | } |
Dylan Reid | cefba1e | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1264 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1265 | int 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 Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1269 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1270 | if (bytes == 0) |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1271 | return 0; |
| 1272 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1273 | count = pcm_bytes_to_frames(pcm, bytes); |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1274 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1275 | while (count > 0) { |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1276 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1277 | /* 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 Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1284 | /* start the audio if we reach the threshold */ |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1285 | if (!pcm->running && |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1286 | (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 Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1336 | /* 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 Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 1344 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1345 | |
| 1346 | offset += frames; |
| 1347 | count -= frames; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1350 | return 0; |
Simon Wilson | e9942c8 | 2011-10-13 13:57:25 -0700 | [diff] [blame] | 1351 | } |
Eric Laurent | c98da79 | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1352 | |
| 1353 | int 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1358 | return pcm_mmap_transfer(pcm, (void *)data, count); |
Eric Laurent | c98da79 | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | int 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 Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1366 | return pcm_mmap_transfer(pcm, data, count); |
Eric Laurent | c98da79 | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1367 | } |
Shiv Maliyappanahalli | 892d6a5 | 2014-05-23 15:20:13 -0700 | [diff] [blame] | 1368 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1369 | int pcm_ioctl(struct pcm *pcm, int request, ...) |
Shiv Maliyappanahalli | 892d6a5 | 2014-05-23 15:20:13 -0700 | [diff] [blame] | 1370 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1371 | va_list ap; |
| 1372 | void * arg; |
Shiv Maliyappanahalli | 892d6a5 | 2014-05-23 15:20:13 -0700 | [diff] [blame] | 1373 | |
| 1374 | if (!pcm_is_ready(pcm)) |
| 1375 | return -1; |
| 1376 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 1377 | va_start(ap, request); |
| 1378 | arg = va_arg(ap, void *); |
| 1379 | va_end(ap); |
| 1380 | |
Bhalchandra Gajare | eb387d7 | 2019-08-21 15:07:28 -0700 | [diff] [blame] | 1381 | return pcm->ops->ioctl(pcm->data, request, arg); |
Shiv Maliyappanahalli | 892d6a5 | 2014-05-23 15:20:13 -0700 | [diff] [blame] | 1382 | } |