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