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