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) { |
| 507 | case PCM_PARAM_SAMPLE_BITS: |
| 508 | return SNDRV_PCM_HW_PARAM_SAMPLE_BITS; |
| 509 | break; |
| 510 | case PCM_PARAM_FRAME_BITS: |
| 511 | return SNDRV_PCM_HW_PARAM_FRAME_BITS; |
| 512 | break; |
| 513 | case PCM_PARAM_CHANNELS: |
| 514 | return SNDRV_PCM_HW_PARAM_CHANNELS; |
| 515 | break; |
| 516 | case PCM_PARAM_RATE: |
| 517 | return SNDRV_PCM_HW_PARAM_RATE; |
| 518 | break; |
| 519 | case PCM_PARAM_PERIOD_TIME: |
| 520 | return SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 521 | break; |
| 522 | case PCM_PARAM_PERIOD_SIZE: |
| 523 | return SNDRV_PCM_HW_PARAM_PERIOD_SIZE; |
| 524 | break; |
| 525 | case PCM_PARAM_PERIOD_BYTES: |
| 526 | return SNDRV_PCM_HW_PARAM_PERIOD_BYTES; |
| 527 | break; |
| 528 | case PCM_PARAM_PERIODS: |
| 529 | return SNDRV_PCM_HW_PARAM_PERIODS; |
| 530 | break; |
| 531 | case PCM_PARAM_BUFFER_TIME: |
| 532 | return SNDRV_PCM_HW_PARAM_BUFFER_TIME; |
| 533 | break; |
| 534 | case PCM_PARAM_BUFFER_SIZE: |
| 535 | return SNDRV_PCM_HW_PARAM_BUFFER_SIZE; |
| 536 | break; |
| 537 | case PCM_PARAM_BUFFER_BYTES: |
| 538 | return SNDRV_PCM_HW_PARAM_BUFFER_BYTES; |
| 539 | break; |
| 540 | case PCM_PARAM_TICK_TIME: |
| 541 | return SNDRV_PCM_HW_PARAM_TICK_TIME; |
| 542 | break; |
| 543 | |
| 544 | default: |
| 545 | return -1; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | unsigned int pcm_params_get_min(struct pcm_params *pcm_params, |
| 550 | enum pcm_param param) |
| 551 | { |
| 552 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 553 | int p; |
| 554 | |
| 555 | if (!params) |
| 556 | return 0; |
| 557 | |
| 558 | p = pcm_param_to_alsa(param); |
| 559 | if (p < 0) |
| 560 | return 0; |
| 561 | |
| 562 | return param_get_min(params, p); |
| 563 | } |
| 564 | |
| 565 | unsigned int pcm_params_get_max(struct pcm_params *pcm_params, |
| 566 | enum pcm_param param) |
| 567 | { |
| 568 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 569 | int p; |
| 570 | |
| 571 | if (!params) |
| 572 | return 0; |
| 573 | |
| 574 | p = pcm_param_to_alsa(param); |
| 575 | if (p < 0) |
| 576 | return 0; |
| 577 | |
| 578 | return param_get_max(params, p); |
| 579 | } |
| 580 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 581 | int pcm_close(struct pcm *pcm) |
| 582 | { |
| 583 | if (pcm == &bad_pcm) |
| 584 | return 0; |
| 585 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 586 | pcm_hw_munmap_status(pcm); |
| 587 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 588 | if (pcm->flags & PCM_MMAP) { |
| 589 | pcm_stop(pcm); |
| 590 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 591 | } |
| 592 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 593 | if (pcm->fd >= 0) |
| 594 | close(pcm->fd); |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 595 | pcm->prepared = 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 596 | pcm->running = 0; |
| 597 | pcm->buffer_size = 0; |
| 598 | pcm->fd = -1; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 599 | free(pcm); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 603 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
| 604 | unsigned int flags, struct pcm_config *config) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 605 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 606 | struct pcm *pcm; |
| 607 | struct snd_pcm_info info; |
| 608 | struct snd_pcm_hw_params params; |
| 609 | struct snd_pcm_sw_params sparams; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 610 | char fn[256]; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 611 | int rc; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 612 | |
| 613 | pcm = calloc(1, sizeof(struct pcm)); |
| 614 | if (!pcm || !config) |
| 615 | return &bad_pcm; /* TODO: could support default config here */ |
| 616 | |
| 617 | pcm->config = *config; |
| 618 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 619 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 620 | flags & PCM_IN ? 'c' : 'p'); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 621 | |
| 622 | pcm->flags = flags; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 623 | pcm->fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 624 | if (pcm->fd < 0) { |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 625 | oops(pcm, errno, "cannot open device '%s'", fn); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 626 | return pcm; |
| 627 | } |
| 628 | |
| 629 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 630 | oops(pcm, errno, "cannot get info"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 631 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | param_init(¶ms); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 635 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 636 | pcm_format_to_alsa(config->format)); |
| 637 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 638 | SNDRV_PCM_SUBFORMAT_STD); |
| 639 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 640 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 641 | pcm_format_to_bits(config->format)); |
| 642 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 643 | pcm_format_to_bits(config->format) * config->channels); |
| 644 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 645 | config->channels); |
| 646 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 647 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 648 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 649 | if (flags & PCM_NOIRQ) { |
| 650 | |
| 651 | if (!(flags & PCM_MMAP)) { |
| 652 | oops(pcm, -EINVAL, "noirq only currently supported with mmap()."); |
| 653 | goto fail; |
| 654 | } |
| 655 | |
| 656 | params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; |
| 657 | pcm->noirq_frames_per_msec = config->rate / 1000; |
| 658 | } |
| 659 | |
| 660 | if (flags & PCM_MMAP) |
| 661 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 662 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 663 | else |
| 664 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 665 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 666 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 667 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 668 | oops(pcm, errno, "cannot set hw params"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 669 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 672 | /* get our refined hw_params */ |
| 673 | config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 674 | config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 675 | pcm->buffer_size = config->period_count * config->period_size; |
| 676 | |
| 677 | if (flags & PCM_MMAP) { |
| 678 | pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size), |
| 679 | PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0); |
| 680 | if (pcm->mmap_buffer == MAP_FAILED) { |
| 681 | oops(pcm, -errno, "failed to mmap buffer %d bytes\n", |
| 682 | pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 683 | goto fail_close; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 688 | memset(&sparams, 0, sizeof(sparams)); |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 689 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 690 | sparams.period_step = 1; |
| 691 | sparams.avail_min = 1; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 692 | |
Eric Laurent | 93e7b67 | 2012-08-22 16:18:14 -0700 | [diff] [blame] | 693 | if (!config->start_threshold) { |
| 694 | if (pcm->flags & PCM_IN) |
| 695 | pcm->config.start_threshold = sparams.start_threshold = 1; |
| 696 | else |
| 697 | pcm->config.start_threshold = sparams.start_threshold = |
| 698 | config->period_count * config->period_size / 2; |
| 699 | } else |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 700 | sparams.start_threshold = config->start_threshold; |
| 701 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 702 | /* pick a high stop threshold - todo: does this need further tuning */ |
Eric Laurent | 3502113 | 2012-01-30 11:31:56 -0800 | [diff] [blame] | 703 | if (!config->stop_threshold) { |
| 704 | if (pcm->flags & PCM_IN) |
| 705 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 706 | config->period_count * config->period_size * 10; |
| 707 | else |
| 708 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 709 | config->period_count * config->period_size; |
| 710 | } |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 711 | else |
| 712 | sparams.stop_threshold = config->stop_threshold; |
| 713 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 714 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 715 | sparams.silence_size = 0; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 716 | sparams.silence_threshold = config->silence_threshold; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 717 | pcm->boundary = sparams.boundary = pcm->buffer_size; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 718 | |
Gabriel M. Beddingfield | 80085d4 | 2012-02-08 16:53:32 -0600 | [diff] [blame] | 719 | while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 720 | pcm->boundary *= 2; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 721 | |
| 722 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 723 | oops(pcm, errno, "cannot set sw params"); |
| 724 | goto fail; |
| 725 | } |
| 726 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 727 | rc = pcm_hw_mmap_status(pcm); |
| 728 | if (rc < 0) { |
| 729 | oops(pcm, rc, "mmap status failed"); |
| 730 | goto fail; |
| 731 | } |
| 732 | |
Glenn Kasten | 8101240 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 733 | #ifdef SNDRV_PCM_IOCTL_TTSTAMP |
| 734 | if (pcm->flags & PCM_MONOTONIC) { |
| 735 | int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
| 736 | rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg); |
| 737 | if (rc < 0) { |
| 738 | oops(pcm, rc, "cannot set timestamp type"); |
| 739 | goto fail; |
| 740 | } |
| 741 | } |
| 742 | #endif |
| 743 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 744 | pcm->underruns = 0; |
| 745 | return pcm; |
| 746 | |
| 747 | fail: |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 748 | if (flags & PCM_MMAP) |
| 749 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 750 | fail_close: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 751 | close(pcm->fd); |
| 752 | pcm->fd = -1; |
| 753 | return pcm; |
| 754 | } |
| 755 | |
| 756 | int pcm_is_ready(struct pcm *pcm) |
| 757 | { |
| 758 | return pcm->fd >= 0; |
| 759 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 760 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 761 | int pcm_prepare(struct pcm *pcm) |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 762 | { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 763 | if (pcm->prepared) |
| 764 | return 0; |
| 765 | |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 766 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0) |
| 767 | return oops(pcm, errno, "cannot prepare channel"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 768 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 769 | pcm->prepared = 1; |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | int pcm_start(struct pcm *pcm) |
| 774 | { |
| 775 | int prepare_error = pcm_prepare(pcm); |
| 776 | if (prepare_error) |
| 777 | return prepare_error; |
| 778 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 779 | if (pcm->flags & PCM_MMAP) |
| 780 | pcm_sync_ptr(pcm, 0); |
| 781 | |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 782 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0) |
| 783 | return oops(pcm, errno, "cannot start channel"); |
| 784 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 785 | pcm->running = 1; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | int pcm_stop(struct pcm *pcm) |
| 790 | { |
| 791 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0) |
| 792 | return oops(pcm, errno, "cannot stop channel"); |
| 793 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 794 | pcm->prepared = 0; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 795 | pcm->running = 0; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 799 | static inline int pcm_mmap_playback_avail(struct pcm *pcm) |
| 800 | { |
| 801 | int avail; |
| 802 | |
| 803 | avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 804 | |
| 805 | if (avail < 0) |
| 806 | avail += pcm->boundary; |
| 807 | else if (avail > (int)pcm->boundary) |
| 808 | avail -= pcm->boundary; |
| 809 | |
| 810 | return avail; |
| 811 | } |
| 812 | |
| 813 | static inline int pcm_mmap_capture_avail(struct pcm *pcm) |
| 814 | { |
| 815 | int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr; |
| 816 | if (avail < 0) |
| 817 | avail += pcm->boundary; |
| 818 | return avail; |
| 819 | } |
| 820 | |
| 821 | static inline int pcm_mmap_avail(struct pcm *pcm) |
| 822 | { |
| 823 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC); |
| 824 | if (pcm->flags & PCM_IN) |
| 825 | return pcm_mmap_capture_avail(pcm); |
| 826 | else |
| 827 | return pcm_mmap_playback_avail(pcm); |
| 828 | } |
| 829 | |
| 830 | static void pcm_mmap_appl_forward(struct pcm *pcm, int frames) |
| 831 | { |
| 832 | unsigned int appl_ptr = pcm->mmap_control->appl_ptr; |
| 833 | appl_ptr += frames; |
| 834 | |
| 835 | /* check for boundary wrap */ |
| 836 | if (appl_ptr > pcm->boundary) |
| 837 | appl_ptr -= pcm->boundary; |
| 838 | pcm->mmap_control->appl_ptr = appl_ptr; |
| 839 | } |
| 840 | |
| 841 | int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, |
| 842 | unsigned int *frames) |
| 843 | { |
| 844 | unsigned int continuous, copy_frames, avail; |
| 845 | |
| 846 | /* return the mmap buffer */ |
| 847 | *areas = pcm->mmap_buffer; |
| 848 | |
| 849 | /* and the application offset in frames */ |
| 850 | *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size; |
| 851 | |
| 852 | avail = pcm_mmap_avail(pcm); |
| 853 | if (avail > pcm->buffer_size) |
| 854 | avail = pcm->buffer_size; |
| 855 | continuous = pcm->buffer_size - *offset; |
| 856 | |
| 857 | /* we can only copy frames if the are availabale and continuos */ |
| 858 | copy_frames = *frames; |
| 859 | if (copy_frames > avail) |
| 860 | copy_frames = avail; |
| 861 | if (copy_frames > continuous) |
| 862 | copy_frames = continuous; |
| 863 | *frames = copy_frames; |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames) |
| 869 | { |
| 870 | /* update the application pointer in userspace and kernel */ |
| 871 | pcm_mmap_appl_forward(pcm, frames); |
| 872 | pcm_sync_ptr(pcm, 0); |
| 873 | |
| 874 | return frames; |
| 875 | } |
| 876 | |
| 877 | int pcm_avail_update(struct pcm *pcm) |
| 878 | { |
| 879 | pcm_sync_ptr(pcm, 0); |
| 880 | return pcm_mmap_avail(pcm); |
| 881 | } |
| 882 | |
| 883 | int pcm_state(struct pcm *pcm) |
| 884 | { |
| 885 | int err = pcm_sync_ptr(pcm, 0); |
| 886 | if (err < 0) |
| 887 | return err; |
| 888 | |
| 889 | return pcm->mmap_status->state; |
| 890 | } |
| 891 | |
| 892 | int pcm_wait(struct pcm *pcm, int timeout) |
| 893 | { |
| 894 | struct pollfd pfd; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 895 | int err; |
| 896 | |
| 897 | pfd.fd = pcm->fd; |
| 898 | pfd.events = POLLOUT | POLLERR | POLLNVAL; |
| 899 | |
| 900 | do { |
| 901 | /* let's wait for avail or timeout */ |
| 902 | err = poll(&pfd, 1, timeout); |
| 903 | if (err < 0) |
| 904 | return -errno; |
| 905 | |
| 906 | /* timeout ? */ |
| 907 | if (err == 0) |
| 908 | return 0; |
| 909 | |
| 910 | /* have we been interrupted ? */ |
| 911 | if (errno == -EINTR) |
| 912 | continue; |
| 913 | |
| 914 | /* check for any errors */ |
| 915 | if (pfd.revents & (POLLERR | POLLNVAL)) { |
| 916 | switch (pcm_state(pcm)) { |
| 917 | case PCM_STATE_XRUN: |
| 918 | return -EPIPE; |
| 919 | case PCM_STATE_SUSPENDED: |
| 920 | return -ESTRPIPE; |
| 921 | case PCM_STATE_DISCONNECTED: |
| 922 | return -ENODEV; |
| 923 | default: |
| 924 | return -EIO; |
| 925 | } |
| 926 | } |
| 927 | /* poll again if fd not ready for IO */ |
| 928 | } while (!(pfd.revents & (POLLIN | POLLOUT))); |
| 929 | |
| 930 | return 1; |
| 931 | } |
| 932 | |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 933 | 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] | 934 | { |
| 935 | int err = 0, frames, avail; |
| 936 | unsigned int offset = 0, count; |
| 937 | |
| 938 | if (bytes == 0) |
| 939 | return 0; |
| 940 | |
| 941 | count = pcm_bytes_to_frames(pcm, bytes); |
| 942 | |
| 943 | while (count > 0) { |
| 944 | |
| 945 | /* get the available space for writing new frames */ |
| 946 | avail = pcm_avail_update(pcm); |
| 947 | if (avail < 0) { |
| 948 | fprintf(stderr, "cannot determine available mmap frames"); |
| 949 | return err; |
| 950 | } |
| 951 | |
| 952 | /* start the audio if we reach the threshold */ |
| 953 | if (!pcm->running && |
| 954 | (pcm->buffer_size - avail) >= pcm->config.start_threshold) { |
| 955 | if (pcm_start(pcm) < 0) { |
| 956 | fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n", |
| 957 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 958 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 959 | avail); |
| 960 | return -errno; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | /* sleep until we have space to write new frames */ |
| 965 | if (pcm->running && |
| 966 | (unsigned int)avail < pcm->mmap_control->avail_min) { |
| 967 | int time = -1; |
| 968 | |
| 969 | if (pcm->flags & PCM_NOIRQ) |
| 970 | time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min) |
| 971 | / pcm->noirq_frames_per_msec; |
| 972 | |
| 973 | err = pcm_wait(pcm, time); |
| 974 | if (err < 0) { |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 975 | pcm->prepared = 0; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 976 | pcm->running = 0; |
| 977 | fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n", |
| 978 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 979 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 980 | avail); |
| 981 | pcm->mmap_control->appl_ptr = 0; |
| 982 | return err; |
| 983 | } |
| 984 | continue; |
| 985 | } |
| 986 | |
| 987 | frames = count; |
| 988 | if (frames > avail) |
| 989 | frames = avail; |
| 990 | |
| 991 | if (!frames) |
| 992 | break; |
| 993 | |
| 994 | /* copy frames from buffer */ |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 995 | frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 996 | if (frames < 0) { |
| 997 | fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n", |
| 998 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 999 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 1000 | avail); |
| 1001 | return frames; |
| 1002 | } |
| 1003 | |
| 1004 | offset += frames; |
| 1005 | count -= frames; |
| 1006 | } |
| 1007 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1010 | |
| 1011 | int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1012 | { |
| 1013 | if ((~pcm->flags) & (PCM_OUT | PCM_MMAP)) |
| 1014 | return -ENOSYS; |
| 1015 | |
| 1016 | return pcm_mmap_transfer(pcm, (void *)data, count); |
| 1017 | } |
| 1018 | |
| 1019 | int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) |
| 1020 | { |
| 1021 | if ((~pcm->flags) & (PCM_IN | PCM_MMAP)) |
| 1022 | return -ENOSYS; |
| 1023 | |
| 1024 | return pcm_mmap_transfer(pcm, data, count); |
| 1025 | } |