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