Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -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> |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 36 | #include <poll.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 37 | |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/time.h> |
Dima Krasner | 696c448 | 2016-03-05 19:50:02 +0200 | [diff] [blame] | 41 | #include <time.h> |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 42 | #include <limits.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 43 | |
| 44 | #include <linux/ioctl.h> |
| 45 | #define __force |
| 46 | #define __bitwise |
| 47 | #define __user |
| 48 | #include <sound/asound.h> |
| 49 | |
| 50 | #include <tinyalsa/asoundlib.h> |
| 51 | |
| 52 | #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 53 | #define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 54 | |
| 55 | static inline int param_is_mask(int p) |
| 56 | { |
| 57 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) && |
| 58 | (p <= SNDRV_PCM_HW_PARAM_LAST_MASK); |
| 59 | } |
| 60 | |
| 61 | static inline int param_is_interval(int p) |
| 62 | { |
| 63 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) && |
| 64 | (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL); |
| 65 | } |
| 66 | |
| 67 | static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n) |
| 68 | { |
| 69 | return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); |
| 70 | } |
| 71 | |
| 72 | static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n) |
| 73 | { |
| 74 | return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]); |
| 75 | } |
| 76 | |
| 77 | static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit) |
| 78 | { |
| 79 | if (bit >= SNDRV_MASK_MAX) |
| 80 | return; |
| 81 | if (param_is_mask(n)) { |
| 82 | struct snd_mask *m = param_to_mask(p, n); |
| 83 | m->bits[0] = 0; |
| 84 | m->bits[1] = 0; |
| 85 | m->bits[bit >> 5] |= (1 << (bit & 31)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 90 | { |
| 91 | if (param_is_interval(n)) { |
| 92 | struct snd_interval *i = param_to_interval(p, n); |
| 93 | i->min = val; |
| 94 | } |
| 95 | } |
| 96 | |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 97 | static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n) |
| 98 | { |
| 99 | if (param_is_interval(n)) { |
| 100 | struct snd_interval *i = param_to_interval(p, n); |
| 101 | return i->min; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n) |
| 107 | { |
| 108 | if (param_is_interval(n)) { |
| 109 | struct snd_interval *i = param_to_interval(p, n); |
| 110 | return i->max; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 115 | static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 116 | { |
| 117 | if (param_is_interval(n)) { |
| 118 | struct snd_interval *i = param_to_interval(p, n); |
| 119 | i->min = val; |
| 120 | i->max = val; |
| 121 | i->integer = 1; |
| 122 | } |
| 123 | } |
| 124 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 125 | static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n) |
| 126 | { |
| 127 | if (param_is_interval(n)) { |
| 128 | struct snd_interval *i = param_to_interval(p, n); |
| 129 | if (i->integer) |
| 130 | return i->max; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 135 | static void param_init(struct snd_pcm_hw_params *p) |
| 136 | { |
| 137 | int n; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 138 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 139 | memset(p, 0, sizeof(*p)); |
| 140 | for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK; |
| 141 | n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) { |
| 142 | struct snd_mask *m = param_to_mask(p, n); |
| 143 | m->bits[0] = ~0; |
| 144 | m->bits[1] = ~0; |
| 145 | } |
| 146 | for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; |
| 147 | n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) { |
| 148 | struct snd_interval *i = param_to_interval(p, n); |
| 149 | i->min = 0; |
| 150 | i->max = ~0; |
| 151 | } |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 152 | p->rmask = ~0U; |
| 153 | p->cmask = 0; |
| 154 | p->info = ~0U; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | #define PCM_ERROR_MAX 128 |
| 158 | |
| 159 | struct pcm { |
| 160 | int fd; |
| 161 | unsigned int flags; |
| 162 | int running:1; |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 163 | int prepared:1; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 164 | int underruns; |
| 165 | unsigned int buffer_size; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 166 | unsigned int boundary; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 167 | char error[PCM_ERROR_MAX]; |
| 168 | struct pcm_config config; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 169 | struct snd_pcm_mmap_status *mmap_status; |
| 170 | struct snd_pcm_mmap_control *mmap_control; |
| 171 | struct snd_pcm_sync_ptr *sync_ptr; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 172 | void *mmap_buffer; |
| 173 | unsigned int noirq_frames_per_msec; |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 174 | long pcm_delay; |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 175 | unsigned int subdevice; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 178 | unsigned int pcm_get_buffer_size(struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 179 | { |
| 180 | return pcm->buffer_size; |
| 181 | } |
| 182 | |
Taylor Holberton | bb40260 | 2016-08-03 10:15:46 -0400 | [diff] [blame] | 183 | int pcm_get_file_descriptor(struct pcm *pcm) |
| 184 | { |
| 185 | return pcm->fd; |
| 186 | } |
| 187 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 188 | const char* pcm_get_error(struct pcm *pcm) |
| 189 | { |
| 190 | return pcm->error; |
| 191 | } |
| 192 | |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 193 | unsigned int pcm_get_subdevice(struct pcm *pcm) |
| 194 | { |
| 195 | return pcm->subdevice; |
| 196 | } |
| 197 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 198 | static int oops(struct pcm *pcm, int e, const char *fmt, ...) |
| 199 | { |
| 200 | va_list ap; |
| 201 | int sz; |
| 202 | |
| 203 | va_start(ap, fmt); |
| 204 | vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap); |
| 205 | va_end(ap); |
| 206 | sz = strlen(pcm->error); |
| 207 | |
| 208 | if (errno) |
| 209 | snprintf(pcm->error + sz, PCM_ERROR_MAX - sz, |
| 210 | ": %s", strerror(e)); |
| 211 | return -1; |
| 212 | } |
| 213 | |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 214 | static unsigned int pcm_format_to_alsa(enum pcm_format format) |
| 215 | { |
| 216 | switch (format) { |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 217 | |
Gabriel M. Beddingfield | 2a274a1 | 2012-05-02 11:51:20 -0500 | [diff] [blame] | 218 | case PCM_FORMAT_S8: |
| 219 | return SNDRV_PCM_FORMAT_S8; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 220 | |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 221 | default: |
| 222 | case PCM_FORMAT_S16_LE: |
| 223 | return SNDRV_PCM_FORMAT_S16_LE; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 224 | case PCM_FORMAT_S16_BE: |
| 225 | return SNDRV_PCM_FORMAT_S16_BE; |
| 226 | |
| 227 | case PCM_FORMAT_S24_LE: |
| 228 | return SNDRV_PCM_FORMAT_S24_LE; |
| 229 | case PCM_FORMAT_S24_BE: |
| 230 | return SNDRV_PCM_FORMAT_S24_BE; |
| 231 | |
| 232 | case PCM_FORMAT_S24_3LE: |
| 233 | return SNDRV_PCM_FORMAT_S24_3LE; |
| 234 | case PCM_FORMAT_S24_3BE: |
| 235 | return SNDRV_PCM_FORMAT_S24_3BE; |
| 236 | |
| 237 | case PCM_FORMAT_S32_LE: |
| 238 | return SNDRV_PCM_FORMAT_S32_LE; |
| 239 | case PCM_FORMAT_S32_BE: |
| 240 | return SNDRV_PCM_FORMAT_S32_BE; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 241 | }; |
| 242 | } |
| 243 | |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 244 | unsigned int pcm_format_to_bits(enum pcm_format format) |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 245 | { |
| 246 | switch (format) { |
| 247 | case PCM_FORMAT_S32_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 248 | case PCM_FORMAT_S32_BE: |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 249 | case PCM_FORMAT_S24_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 250 | case PCM_FORMAT_S24_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 251 | return 32; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 252 | case PCM_FORMAT_S24_3LE: |
| 253 | case PCM_FORMAT_S24_3BE: |
| 254 | return 24; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 255 | default: |
| 256 | case PCM_FORMAT_S16_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 257 | case PCM_FORMAT_S16_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 258 | return 16; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 259 | case PCM_FORMAT_S8: |
| 260 | return 8; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 261 | }; |
| 262 | } |
| 263 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 264 | unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes) |
| 265 | { |
| 266 | return bytes / (pcm->config.channels * |
| 267 | (pcm_format_to_bits(pcm->config.format) >> 3)); |
| 268 | } |
| 269 | |
| 270 | unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames) |
| 271 | { |
| 272 | return frames * pcm->config.channels * |
| 273 | (pcm_format_to_bits(pcm->config.format) >> 3); |
| 274 | } |
| 275 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 276 | static int pcm_sync_ptr(struct pcm *pcm, int flags) |
| 277 | { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 278 | if (pcm->sync_ptr) { |
| 279 | pcm->sync_ptr->flags = flags; |
| 280 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) |
| 281 | return -1; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 286 | static int pcm_hw_mmap_status(struct pcm *pcm) |
| 287 | { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 288 | if (pcm->sync_ptr) |
| 289 | return 0; |
| 290 | |
| 291 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 292 | pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED, |
| 293 | pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS); |
| 294 | if (pcm->mmap_status == MAP_FAILED) |
| 295 | pcm->mmap_status = NULL; |
| 296 | if (!pcm->mmap_status) |
| 297 | goto mmap_error; |
| 298 | |
| 299 | pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE, |
| 300 | MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL); |
| 301 | if (pcm->mmap_control == MAP_FAILED) |
| 302 | pcm->mmap_control = NULL; |
| 303 | if (!pcm->mmap_control) { |
| 304 | munmap(pcm->mmap_status, page_size); |
| 305 | pcm->mmap_status = NULL; |
| 306 | goto mmap_error; |
| 307 | } |
| 308 | pcm->mmap_control->avail_min = 1; |
| 309 | |
| 310 | return 0; |
| 311 | |
| 312 | mmap_error: |
| 313 | |
| 314 | pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr)); |
| 315 | if (!pcm->sync_ptr) |
| 316 | return -ENOMEM; |
| 317 | pcm->mmap_status = &pcm->sync_ptr->s.status; |
| 318 | pcm->mmap_control = &pcm->sync_ptr->c.control; |
| 319 | pcm->mmap_control->avail_min = 1; |
| 320 | pcm_sync_ptr(pcm, 0); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static void pcm_hw_munmap_status(struct pcm *pcm) { |
| 326 | if (pcm->sync_ptr) { |
| 327 | free(pcm->sync_ptr); |
| 328 | pcm->sync_ptr = NULL; |
| 329 | } else { |
| 330 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 331 | if (pcm->mmap_status) |
| 332 | munmap(pcm->mmap_status, page_size); |
| 333 | if (pcm->mmap_control) |
| 334 | munmap(pcm->mmap_control, page_size); |
| 335 | } |
| 336 | pcm->mmap_status = NULL; |
| 337 | pcm->mmap_control = NULL; |
| 338 | } |
| 339 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 340 | static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset, |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 341 | char *buf, unsigned int src_offset, |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 342 | unsigned int frames) |
| 343 | { |
| 344 | int size_bytes = pcm_frames_to_bytes(pcm, frames); |
| 345 | int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset); |
| 346 | int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset); |
| 347 | |
| 348 | /* interleaved only atm */ |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 349 | if (pcm->flags & PCM_IN) |
| 350 | memcpy(buf + src_offset_bytes, |
| 351 | (char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 352 | size_bytes); |
| 353 | else |
| 354 | memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 355 | buf + src_offset_bytes, |
| 356 | size_bytes); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 360 | static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf, |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 361 | unsigned int offset, unsigned int size) |
| 362 | { |
| 363 | void *pcm_areas; |
| 364 | int commit; |
| 365 | unsigned int pcm_offset, frames, count = 0; |
| 366 | |
| 367 | while (size > 0) { |
| 368 | frames = size; |
| 369 | pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames); |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 370 | pcm_areas_copy(pcm, pcm_offset, buf, offset, frames); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 371 | commit = pcm_mmap_commit(pcm, pcm_offset, frames); |
| 372 | if (commit < 0) { |
| 373 | oops(pcm, commit, "failed to commit %d frames\n", frames); |
| 374 | return commit; |
| 375 | } |
| 376 | |
| 377 | offset += commit; |
| 378 | count += commit; |
| 379 | size -= commit; |
| 380 | } |
| 381 | return count; |
| 382 | } |
| 383 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 384 | int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail, |
| 385 | struct timespec *tstamp) |
| 386 | { |
| 387 | int frames; |
| 388 | int rc; |
| 389 | snd_pcm_uframes_t hw_ptr; |
| 390 | |
| 391 | if (!pcm_is_ready(pcm)) |
| 392 | return -1; |
| 393 | |
| 394 | rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC); |
| 395 | if (rc < 0) |
| 396 | return -1; |
| 397 | |
Eric Laurent | 7db4858 | 2011-11-17 11:47:59 -0800 | [diff] [blame] | 398 | if ((pcm->mmap_status->state != PCM_STATE_RUNNING) && |
| 399 | (pcm->mmap_status->state != PCM_STATE_DRAINING)) |
Eric Laurent | ee9ba87 | 2011-11-15 19:04:03 -0800 | [diff] [blame] | 400 | return -1; |
| 401 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 402 | *tstamp = pcm->mmap_status->tstamp; |
| 403 | if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0) |
| 404 | return -1; |
| 405 | |
| 406 | hw_ptr = pcm->mmap_status->hw_ptr; |
| 407 | if (pcm->flags & PCM_IN) |
| 408 | frames = hw_ptr - pcm->mmap_control->appl_ptr; |
| 409 | else |
| 410 | frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 411 | |
| 412 | if (frames < 0) |
| 413 | return -1; |
| 414 | |
| 415 | *avail = (unsigned int)frames; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Mark Brown | 6bbe77a | 2012-02-10 22:07:09 +0000 | [diff] [blame] | 420 | int pcm_write(struct pcm *pcm, const void *data, unsigned int count) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 421 | { |
| 422 | struct snd_xferi x; |
| 423 | |
| 424 | if (pcm->flags & PCM_IN) |
| 425 | return -EINVAL; |
| 426 | |
Mark Brown | 6bbe77a | 2012-02-10 22:07:09 +0000 | [diff] [blame] | 427 | x.buf = (void*)data; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 428 | x.frames = count / (pcm->config.channels * |
| 429 | pcm_format_to_bits(pcm->config.format) / 8); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 430 | |
| 431 | for (;;) { |
| 432 | if (!pcm->running) { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 433 | int prepare_error = pcm_prepare(pcm); |
| 434 | if (prepare_error) |
| 435 | return prepare_error; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 436 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) |
| 437 | return oops(pcm, errno, "cannot write initial data"); |
| 438 | pcm->running = 1; |
| 439 | return 0; |
| 440 | } |
| 441 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 442 | pcm->prepared = 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 443 | pcm->running = 0; |
| 444 | if (errno == EPIPE) { |
John Grossman | b6db70a | 2012-04-03 16:37:38 -0700 | [diff] [blame] | 445 | /* we failed to make our window -- try to restart if we are |
| 446 | * allowed to do so. Otherwise, simply allow the EPIPE error to |
| 447 | * propagate up to the app level */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 448 | pcm->underruns++; |
John Grossman | b6db70a | 2012-04-03 16:37:38 -0700 | [diff] [blame] | 449 | if (pcm->flags & PCM_NORESTART) |
| 450 | return -EPIPE; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | return oops(pcm, errno, "cannot write stream data"); |
| 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | int pcm_read(struct pcm *pcm, void *data, unsigned int count) |
| 460 | { |
| 461 | struct snd_xferi x; |
| 462 | |
| 463 | if (!(pcm->flags & PCM_IN)) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | x.buf = data; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 467 | x.frames = count / (pcm->config.channels * |
| 468 | pcm_format_to_bits(pcm->config.format) / 8); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 469 | |
| 470 | for (;;) { |
| 471 | if (!pcm->running) { |
Keunyoung | 2581a1e | 2012-05-10 10:50:00 -0700 | [diff] [blame] | 472 | if (pcm_start(pcm) < 0) { |
| 473 | fprintf(stderr, "start error"); |
| 474 | return -errno; |
| 475 | } |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 476 | } |
| 477 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 478 | pcm->prepared = 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 479 | pcm->running = 0; |
| 480 | if (errno == EPIPE) { |
| 481 | /* we failed to make our window -- try to restart */ |
| 482 | pcm->underruns++; |
| 483 | continue; |
| 484 | } |
| 485 | return oops(pcm, errno, "cannot read stream data"); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static struct pcm bad_pcm = { |
| 492 | .fd = -1, |
| 493 | }; |
| 494 | |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 495 | struct pcm_params *pcm_params_get(unsigned int card, unsigned int device, |
| 496 | unsigned int flags) |
| 497 | { |
| 498 | struct snd_pcm_hw_params *params; |
| 499 | char fn[256]; |
| 500 | int fd; |
| 501 | |
| 502 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 503 | flags & PCM_IN ? 'c' : 'p'); |
| 504 | |
| 505 | fd = open(fn, O_RDWR); |
| 506 | if (fd < 0) { |
| 507 | fprintf(stderr, "cannot open device '%s'\n", fn); |
| 508 | goto err_open; |
| 509 | } |
| 510 | |
| 511 | params = calloc(1, sizeof(struct snd_pcm_hw_params)); |
| 512 | if (!params) |
| 513 | goto err_calloc; |
| 514 | |
| 515 | param_init(params); |
| 516 | if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) { |
| 517 | fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno); |
| 518 | goto err_hw_refine; |
| 519 | } |
| 520 | |
| 521 | close(fd); |
| 522 | |
| 523 | return (struct pcm_params *)params; |
| 524 | |
| 525 | err_hw_refine: |
| 526 | free(params); |
| 527 | err_calloc: |
| 528 | close(fd); |
| 529 | err_open: |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | void pcm_params_free(struct pcm_params *pcm_params) |
| 534 | { |
| 535 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 536 | |
| 537 | if (params) |
| 538 | free(params); |
| 539 | } |
| 540 | |
| 541 | static int pcm_param_to_alsa(enum pcm_param param) |
| 542 | { |
| 543 | switch (param) { |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 544 | case PCM_PARAM_ACCESS: |
| 545 | return SNDRV_PCM_HW_PARAM_ACCESS; |
| 546 | case PCM_PARAM_FORMAT: |
| 547 | return SNDRV_PCM_HW_PARAM_FORMAT; |
| 548 | case PCM_PARAM_SUBFORMAT: |
| 549 | return SNDRV_PCM_HW_PARAM_SUBFORMAT; |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 550 | case PCM_PARAM_SAMPLE_BITS: |
| 551 | return SNDRV_PCM_HW_PARAM_SAMPLE_BITS; |
| 552 | break; |
| 553 | case PCM_PARAM_FRAME_BITS: |
| 554 | return SNDRV_PCM_HW_PARAM_FRAME_BITS; |
| 555 | break; |
| 556 | case PCM_PARAM_CHANNELS: |
| 557 | return SNDRV_PCM_HW_PARAM_CHANNELS; |
| 558 | break; |
| 559 | case PCM_PARAM_RATE: |
| 560 | return SNDRV_PCM_HW_PARAM_RATE; |
| 561 | break; |
| 562 | case PCM_PARAM_PERIOD_TIME: |
| 563 | return SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 564 | break; |
| 565 | case PCM_PARAM_PERIOD_SIZE: |
| 566 | return SNDRV_PCM_HW_PARAM_PERIOD_SIZE; |
| 567 | break; |
| 568 | case PCM_PARAM_PERIOD_BYTES: |
| 569 | return SNDRV_PCM_HW_PARAM_PERIOD_BYTES; |
| 570 | break; |
| 571 | case PCM_PARAM_PERIODS: |
| 572 | return SNDRV_PCM_HW_PARAM_PERIODS; |
| 573 | break; |
| 574 | case PCM_PARAM_BUFFER_TIME: |
| 575 | return SNDRV_PCM_HW_PARAM_BUFFER_TIME; |
| 576 | break; |
| 577 | case PCM_PARAM_BUFFER_SIZE: |
| 578 | return SNDRV_PCM_HW_PARAM_BUFFER_SIZE; |
| 579 | break; |
| 580 | case PCM_PARAM_BUFFER_BYTES: |
| 581 | return SNDRV_PCM_HW_PARAM_BUFFER_BYTES; |
| 582 | break; |
| 583 | case PCM_PARAM_TICK_TIME: |
| 584 | return SNDRV_PCM_HW_PARAM_TICK_TIME; |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | return -1; |
| 589 | } |
| 590 | } |
| 591 | |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 592 | struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params, |
| 593 | enum pcm_param param) |
| 594 | { |
| 595 | int p; |
| 596 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 597 | if (params == NULL) { |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | p = pcm_param_to_alsa(param); |
| 602 | if (p < 0 || !param_is_mask(p)) { |
| 603 | return NULL; |
| 604 | } |
| 605 | |
| 606 | return (struct pcm_mask *)param_to_mask(params, p); |
| 607 | } |
| 608 | |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 609 | unsigned int pcm_params_get_min(struct pcm_params *pcm_params, |
| 610 | enum pcm_param param) |
| 611 | { |
| 612 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 613 | int p; |
| 614 | |
| 615 | if (!params) |
| 616 | return 0; |
| 617 | |
| 618 | p = pcm_param_to_alsa(param); |
| 619 | if (p < 0) |
| 620 | return 0; |
| 621 | |
| 622 | return param_get_min(params, p); |
| 623 | } |
| 624 | |
| 625 | unsigned int pcm_params_get_max(struct pcm_params *pcm_params, |
| 626 | enum pcm_param param) |
| 627 | { |
| 628 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 629 | int p; |
| 630 | |
| 631 | if (!params) |
| 632 | return 0; |
| 633 | |
| 634 | p = pcm_param_to_alsa(param); |
| 635 | if (p < 0) |
| 636 | return 0; |
| 637 | |
| 638 | return param_get_max(params, p); |
| 639 | } |
| 640 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 641 | int pcm_close(struct pcm *pcm) |
| 642 | { |
| 643 | if (pcm == &bad_pcm) |
| 644 | return 0; |
| 645 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 646 | pcm_hw_munmap_status(pcm); |
| 647 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 648 | if (pcm->flags & PCM_MMAP) { |
| 649 | pcm_stop(pcm); |
| 650 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 651 | } |
| 652 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 653 | if (pcm->fd >= 0) |
| 654 | close(pcm->fd); |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 655 | pcm->prepared = 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 656 | pcm->running = 0; |
| 657 | pcm->buffer_size = 0; |
| 658 | pcm->fd = -1; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 659 | free(pcm); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 663 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
| 664 | unsigned int flags, struct pcm_config *config) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 665 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 666 | struct pcm *pcm; |
| 667 | struct snd_pcm_info info; |
| 668 | struct snd_pcm_hw_params params; |
| 669 | struct snd_pcm_sw_params sparams; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 670 | char fn[256]; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 671 | int rc; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 672 | |
| 673 | pcm = calloc(1, sizeof(struct pcm)); |
| 674 | if (!pcm || !config) |
| 675 | return &bad_pcm; /* TODO: could support default config here */ |
| 676 | |
| 677 | pcm->config = *config; |
| 678 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 679 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 680 | flags & PCM_IN ? 'c' : 'p'); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 681 | |
| 682 | pcm->flags = flags; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 683 | pcm->fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 684 | if (pcm->fd < 0) { |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 685 | oops(pcm, errno, "cannot open device '%s'", fn); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 686 | return pcm; |
| 687 | } |
| 688 | |
| 689 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 690 | oops(pcm, errno, "cannot get info"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 691 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 692 | } |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 693 | pcm->subdevice = info.subdevice; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 694 | |
| 695 | param_init(¶ms); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 696 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 697 | pcm_format_to_alsa(config->format)); |
| 698 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 699 | SNDRV_PCM_SUBFORMAT_STD); |
| 700 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 701 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 702 | pcm_format_to_bits(config->format)); |
| 703 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 704 | pcm_format_to_bits(config->format) * config->channels); |
| 705 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 706 | config->channels); |
| 707 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 708 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 709 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 710 | if (flags & PCM_NOIRQ) { |
| 711 | |
| 712 | if (!(flags & PCM_MMAP)) { |
| 713 | oops(pcm, -EINVAL, "noirq only currently supported with mmap()."); |
| 714 | goto fail; |
| 715 | } |
| 716 | |
| 717 | params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; |
| 718 | pcm->noirq_frames_per_msec = config->rate / 1000; |
| 719 | } |
| 720 | |
| 721 | if (flags & PCM_MMAP) |
| 722 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 723 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 724 | else |
| 725 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 726 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 727 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 728 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 729 | oops(pcm, errno, "cannot set hw params"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 730 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 733 | /* get our refined hw_params */ |
| 734 | config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 735 | config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 736 | pcm->buffer_size = config->period_count * config->period_size; |
| 737 | |
| 738 | if (flags & PCM_MMAP) { |
| 739 | pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size), |
| 740 | PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0); |
| 741 | if (pcm->mmap_buffer == MAP_FAILED) { |
| 742 | oops(pcm, -errno, "failed to mmap buffer %d bytes\n", |
| 743 | pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 744 | goto fail_close; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 749 | memset(&sparams, 0, sizeof(sparams)); |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 750 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 751 | sparams.period_step = 1; |
| 752 | sparams.avail_min = 1; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 753 | |
Eric Laurent | 93e7b67 | 2012-08-22 16:18:14 -0700 | [diff] [blame] | 754 | if (!config->start_threshold) { |
| 755 | if (pcm->flags & PCM_IN) |
| 756 | pcm->config.start_threshold = sparams.start_threshold = 1; |
| 757 | else |
| 758 | pcm->config.start_threshold = sparams.start_threshold = |
| 759 | config->period_count * config->period_size / 2; |
| 760 | } else |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 761 | sparams.start_threshold = config->start_threshold; |
| 762 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 763 | /* pick a high stop threshold - todo: does this need further tuning */ |
Eric Laurent | 3502113 | 2012-01-30 11:31:56 -0800 | [diff] [blame] | 764 | if (!config->stop_threshold) { |
| 765 | if (pcm->flags & PCM_IN) |
| 766 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 767 | config->period_count * config->period_size * 10; |
| 768 | else |
| 769 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 770 | config->period_count * config->period_size; |
| 771 | } |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 772 | else |
| 773 | sparams.stop_threshold = config->stop_threshold; |
| 774 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 775 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 776 | sparams.silence_size = 0; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 777 | sparams.silence_threshold = config->silence_threshold; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 778 | pcm->boundary = sparams.boundary = pcm->buffer_size; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 779 | |
Gabriel M. Beddingfield | 80085d4 | 2012-02-08 16:53:32 -0600 | [diff] [blame] | 780 | while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 781 | pcm->boundary *= 2; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 782 | |
| 783 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 784 | oops(pcm, errno, "cannot set sw params"); |
| 785 | goto fail; |
| 786 | } |
| 787 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 788 | rc = pcm_hw_mmap_status(pcm); |
| 789 | if (rc < 0) { |
| 790 | oops(pcm, rc, "mmap status failed"); |
| 791 | goto fail; |
| 792 | } |
| 793 | |
Glenn Kasten | 8101240 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 794 | #ifdef SNDRV_PCM_IOCTL_TTSTAMP |
| 795 | if (pcm->flags & PCM_MONOTONIC) { |
| 796 | int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
| 797 | rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg); |
| 798 | if (rc < 0) { |
| 799 | oops(pcm, rc, "cannot set timestamp type"); |
| 800 | goto fail; |
| 801 | } |
| 802 | } |
| 803 | #endif |
| 804 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 805 | pcm->underruns = 0; |
| 806 | return pcm; |
| 807 | |
| 808 | fail: |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 809 | if (flags & PCM_MMAP) |
| 810 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 811 | fail_close: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 812 | close(pcm->fd); |
| 813 | pcm->fd = -1; |
| 814 | return pcm; |
| 815 | } |
| 816 | |
| 817 | int pcm_is_ready(struct pcm *pcm) |
| 818 | { |
| 819 | return pcm->fd >= 0; |
| 820 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 821 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 822 | int pcm_prepare(struct pcm *pcm) |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 823 | { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 824 | if (pcm->prepared) |
| 825 | return 0; |
| 826 | |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 827 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0) |
| 828 | return oops(pcm, errno, "cannot prepare channel"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 829 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 830 | pcm->prepared = 1; |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | int pcm_start(struct pcm *pcm) |
| 835 | { |
| 836 | int prepare_error = pcm_prepare(pcm); |
| 837 | if (prepare_error) |
| 838 | return prepare_error; |
| 839 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 840 | if (pcm->flags & PCM_MMAP) |
| 841 | pcm_sync_ptr(pcm, 0); |
| 842 | |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 843 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0) |
| 844 | return oops(pcm, errno, "cannot start channel"); |
| 845 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 846 | pcm->running = 1; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | int pcm_stop(struct pcm *pcm) |
| 851 | { |
| 852 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0) |
| 853 | return oops(pcm, errno, "cannot stop channel"); |
| 854 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 855 | pcm->prepared = 0; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 856 | pcm->running = 0; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 860 | static inline int pcm_mmap_playback_avail(struct pcm *pcm) |
| 861 | { |
| 862 | int avail; |
| 863 | |
| 864 | avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 865 | |
| 866 | if (avail < 0) |
| 867 | avail += pcm->boundary; |
StevenNAN | b0fc3e9 | 2014-03-17 11:14:49 +0800 | [diff] [blame] | 868 | else if (avail >= (int)pcm->boundary) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 869 | avail -= pcm->boundary; |
| 870 | |
| 871 | return avail; |
| 872 | } |
| 873 | |
| 874 | static inline int pcm_mmap_capture_avail(struct pcm *pcm) |
| 875 | { |
| 876 | int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr; |
| 877 | if (avail < 0) |
| 878 | avail += pcm->boundary; |
| 879 | return avail; |
| 880 | } |
| 881 | |
| 882 | static inline int pcm_mmap_avail(struct pcm *pcm) |
| 883 | { |
| 884 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC); |
| 885 | if (pcm->flags & PCM_IN) |
| 886 | return pcm_mmap_capture_avail(pcm); |
| 887 | else |
| 888 | return pcm_mmap_playback_avail(pcm); |
| 889 | } |
| 890 | |
| 891 | static void pcm_mmap_appl_forward(struct pcm *pcm, int frames) |
| 892 | { |
| 893 | unsigned int appl_ptr = pcm->mmap_control->appl_ptr; |
| 894 | appl_ptr += frames; |
| 895 | |
| 896 | /* check for boundary wrap */ |
| 897 | if (appl_ptr > pcm->boundary) |
| 898 | appl_ptr -= pcm->boundary; |
| 899 | pcm->mmap_control->appl_ptr = appl_ptr; |
| 900 | } |
| 901 | |
| 902 | int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, |
| 903 | unsigned int *frames) |
| 904 | { |
| 905 | unsigned int continuous, copy_frames, avail; |
| 906 | |
| 907 | /* return the mmap buffer */ |
| 908 | *areas = pcm->mmap_buffer; |
| 909 | |
| 910 | /* and the application offset in frames */ |
| 911 | *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size; |
| 912 | |
| 913 | avail = pcm_mmap_avail(pcm); |
| 914 | if (avail > pcm->buffer_size) |
| 915 | avail = pcm->buffer_size; |
| 916 | continuous = pcm->buffer_size - *offset; |
| 917 | |
| 918 | /* we can only copy frames if the are availabale and continuos */ |
| 919 | copy_frames = *frames; |
| 920 | if (copy_frames > avail) |
| 921 | copy_frames = avail; |
| 922 | if (copy_frames > continuous) |
| 923 | copy_frames = continuous; |
| 924 | *frames = copy_frames; |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames) |
| 930 | { |
| 931 | /* update the application pointer in userspace and kernel */ |
| 932 | pcm_mmap_appl_forward(pcm, frames); |
| 933 | pcm_sync_ptr(pcm, 0); |
| 934 | |
| 935 | return frames; |
| 936 | } |
| 937 | |
| 938 | int pcm_avail_update(struct pcm *pcm) |
| 939 | { |
| 940 | pcm_sync_ptr(pcm, 0); |
| 941 | return pcm_mmap_avail(pcm); |
| 942 | } |
| 943 | |
| 944 | int pcm_state(struct pcm *pcm) |
| 945 | { |
| 946 | int err = pcm_sync_ptr(pcm, 0); |
| 947 | if (err < 0) |
| 948 | return err; |
| 949 | |
| 950 | return pcm->mmap_status->state; |
| 951 | } |
| 952 | |
| 953 | int pcm_wait(struct pcm *pcm, int timeout) |
| 954 | { |
| 955 | struct pollfd pfd; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 956 | int err; |
| 957 | |
| 958 | pfd.fd = pcm->fd; |
Apelete Seketeli | 84889d0 | 2014-02-14 14:34:32 +0100 | [diff] [blame] | 959 | pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 960 | |
| 961 | do { |
| 962 | /* let's wait for avail or timeout */ |
| 963 | err = poll(&pfd, 1, timeout); |
| 964 | if (err < 0) |
| 965 | return -errno; |
| 966 | |
| 967 | /* timeout ? */ |
| 968 | if (err == 0) |
| 969 | return 0; |
| 970 | |
| 971 | /* have we been interrupted ? */ |
| 972 | if (errno == -EINTR) |
| 973 | continue; |
| 974 | |
| 975 | /* check for any errors */ |
| 976 | if (pfd.revents & (POLLERR | POLLNVAL)) { |
| 977 | switch (pcm_state(pcm)) { |
| 978 | case PCM_STATE_XRUN: |
| 979 | return -EPIPE; |
| 980 | case PCM_STATE_SUSPENDED: |
| 981 | return -ESTRPIPE; |
| 982 | case PCM_STATE_DISCONNECTED: |
| 983 | return -ENODEV; |
| 984 | default: |
| 985 | return -EIO; |
| 986 | } |
| 987 | } |
| 988 | /* poll again if fd not ready for IO */ |
| 989 | } while (!(pfd.revents & (POLLIN | POLLOUT))); |
| 990 | |
| 991 | return 1; |
| 992 | } |
| 993 | |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 994 | int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 995 | { |
| 996 | int err = 0, frames, avail; |
| 997 | unsigned int offset = 0, count; |
| 998 | |
| 999 | if (bytes == 0) |
| 1000 | return 0; |
| 1001 | |
| 1002 | count = pcm_bytes_to_frames(pcm, bytes); |
| 1003 | |
| 1004 | while (count > 0) { |
| 1005 | |
| 1006 | /* get the available space for writing new frames */ |
| 1007 | avail = pcm_avail_update(pcm); |
| 1008 | if (avail < 0) { |
| 1009 | fprintf(stderr, "cannot determine available mmap frames"); |
| 1010 | return err; |
| 1011 | } |
| 1012 | |
| 1013 | /* start the audio if we reach the threshold */ |
| 1014 | if (!pcm->running && |
| 1015 | (pcm->buffer_size - avail) >= pcm->config.start_threshold) { |
| 1016 | if (pcm_start(pcm) < 0) { |
| 1017 | fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n", |
| 1018 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 1019 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 1020 | avail); |
| 1021 | return -errno; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /* sleep until we have space to write new frames */ |
| 1026 | if (pcm->running && |
| 1027 | (unsigned int)avail < pcm->mmap_control->avail_min) { |
| 1028 | int time = -1; |
| 1029 | |
| 1030 | if (pcm->flags & PCM_NOIRQ) |
| 1031 | time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min) |
| 1032 | / pcm->noirq_frames_per_msec; |
| 1033 | |
| 1034 | err = pcm_wait(pcm, time); |
| 1035 | if (err < 0) { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 1036 | pcm->prepared = 0; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1037 | pcm->running = 0; |
| 1038 | fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n", |
| 1039 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 1040 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 1041 | avail); |
| 1042 | pcm->mmap_control->appl_ptr = 0; |
| 1043 | return err; |
| 1044 | } |
| 1045 | continue; |
| 1046 | } |
| 1047 | |
| 1048 | frames = count; |
| 1049 | if (frames > avail) |
| 1050 | frames = avail; |
| 1051 | |
| 1052 | if (!frames) |
| 1053 | break; |
| 1054 | |
| 1055 | /* copy frames from buffer */ |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1056 | frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1057 | if (frames < 0) { |
| 1058 | fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n", |
| 1059 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 1060 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 1061 | avail); |
| 1062 | return frames; |
| 1063 | } |
| 1064 | |
| 1065 | offset += frames; |
| 1066 | count -= frames; |
| 1067 | } |
| 1068 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1071 | |
| 1072 | int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1073 | { |
| 1074 | if ((~pcm->flags) & (PCM_OUT | PCM_MMAP)) |
| 1075 | return -ENOSYS; |
| 1076 | |
| 1077 | return pcm_mmap_transfer(pcm, (void *)data, count); |
| 1078 | } |
| 1079 | |
| 1080 | int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) |
| 1081 | { |
| 1082 | if ((~pcm->flags) & (PCM_IN | PCM_MMAP)) |
| 1083 | return -ENOSYS; |
| 1084 | |
| 1085 | return pcm_mmap_transfer(pcm, data, count); |
| 1086 | } |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 1087 | |
| 1088 | long pcm_get_delay(struct pcm *pcm) |
| 1089 | { |
| 1090 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0) |
| 1091 | return -1; |
| 1092 | |
| 1093 | return pcm->pcm_delay; |
| 1094 | } |