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