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 | |
| 96 | static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 97 | { |
| 98 | if (param_is_interval(n)) { |
| 99 | struct snd_interval *i = param_to_interval(p, n); |
| 100 | i->max = val; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 105 | { |
| 106 | if (param_is_interval(n)) { |
| 107 | struct snd_interval *i = param_to_interval(p, n); |
| 108 | i->min = val; |
| 109 | i->max = val; |
| 110 | i->integer = 1; |
| 111 | } |
| 112 | } |
| 113 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 114 | static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n) |
| 115 | { |
| 116 | if (param_is_interval(n)) { |
| 117 | struct snd_interval *i = param_to_interval(p, n); |
| 118 | if (i->integer) |
| 119 | return i->max; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 124 | static void param_init(struct snd_pcm_hw_params *p) |
| 125 | { |
| 126 | int n; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 127 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 128 | memset(p, 0, sizeof(*p)); |
| 129 | for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK; |
| 130 | n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) { |
| 131 | struct snd_mask *m = param_to_mask(p, n); |
| 132 | m->bits[0] = ~0; |
| 133 | m->bits[1] = ~0; |
| 134 | } |
| 135 | for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; |
| 136 | n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) { |
| 137 | struct snd_interval *i = param_to_interval(p, n); |
| 138 | i->min = 0; |
| 139 | i->max = ~0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #define PCM_ERROR_MAX 128 |
| 144 | |
| 145 | struct pcm { |
| 146 | int fd; |
| 147 | unsigned int flags; |
| 148 | int running:1; |
| 149 | int underruns; |
| 150 | unsigned int buffer_size; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 151 | unsigned int boundary; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 152 | char error[PCM_ERROR_MAX]; |
| 153 | struct pcm_config config; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 154 | struct snd_pcm_mmap_status *mmap_status; |
| 155 | struct snd_pcm_mmap_control *mmap_control; |
| 156 | struct snd_pcm_sync_ptr *sync_ptr; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 157 | void *mmap_buffer; |
| 158 | unsigned int noirq_frames_per_msec; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 161 | unsigned int pcm_get_buffer_size(struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 162 | { |
| 163 | return pcm->buffer_size; |
| 164 | } |
| 165 | |
| 166 | const char* pcm_get_error(struct pcm *pcm) |
| 167 | { |
| 168 | return pcm->error; |
| 169 | } |
| 170 | |
| 171 | static int oops(struct pcm *pcm, int e, const char *fmt, ...) |
| 172 | { |
| 173 | va_list ap; |
| 174 | int sz; |
| 175 | |
| 176 | va_start(ap, fmt); |
| 177 | vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap); |
| 178 | va_end(ap); |
| 179 | sz = strlen(pcm->error); |
| 180 | |
| 181 | if (errno) |
| 182 | snprintf(pcm->error + sz, PCM_ERROR_MAX - sz, |
| 183 | ": %s", strerror(e)); |
| 184 | return -1; |
| 185 | } |
| 186 | |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 187 | static unsigned int pcm_format_to_alsa(enum pcm_format format) |
| 188 | { |
| 189 | switch (format) { |
| 190 | case PCM_FORMAT_S32_LE: |
| 191 | return SNDRV_PCM_FORMAT_S32_LE; |
| 192 | default: |
| 193 | case PCM_FORMAT_S16_LE: |
| 194 | return SNDRV_PCM_FORMAT_S16_LE; |
| 195 | }; |
| 196 | } |
| 197 | |
| 198 | static unsigned int pcm_format_to_bits(enum pcm_format format) |
| 199 | { |
| 200 | switch (format) { |
| 201 | case PCM_FORMAT_S32_LE: |
| 202 | return 32; |
| 203 | default: |
| 204 | case PCM_FORMAT_S16_LE: |
| 205 | return 16; |
| 206 | }; |
| 207 | } |
| 208 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 209 | unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes) |
| 210 | { |
| 211 | return bytes / (pcm->config.channels * |
| 212 | (pcm_format_to_bits(pcm->config.format) >> 3)); |
| 213 | } |
| 214 | |
| 215 | unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames) |
| 216 | { |
| 217 | return frames * pcm->config.channels * |
| 218 | (pcm_format_to_bits(pcm->config.format) >> 3); |
| 219 | } |
| 220 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 221 | static int pcm_sync_ptr(struct pcm *pcm, int flags) { |
| 222 | if (pcm->sync_ptr) { |
| 223 | pcm->sync_ptr->flags = flags; |
| 224 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) |
| 225 | return -1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int pcm_hw_mmap_status(struct pcm *pcm) { |
| 231 | |
| 232 | if (pcm->sync_ptr) |
| 233 | return 0; |
| 234 | |
| 235 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 236 | pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED, |
| 237 | pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS); |
| 238 | if (pcm->mmap_status == MAP_FAILED) |
| 239 | pcm->mmap_status = NULL; |
| 240 | if (!pcm->mmap_status) |
| 241 | goto mmap_error; |
| 242 | |
| 243 | pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE, |
| 244 | MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL); |
| 245 | if (pcm->mmap_control == MAP_FAILED) |
| 246 | pcm->mmap_control = NULL; |
| 247 | if (!pcm->mmap_control) { |
| 248 | munmap(pcm->mmap_status, page_size); |
| 249 | pcm->mmap_status = NULL; |
| 250 | goto mmap_error; |
| 251 | } |
| 252 | pcm->mmap_control->avail_min = 1; |
| 253 | |
| 254 | return 0; |
| 255 | |
| 256 | mmap_error: |
| 257 | |
| 258 | pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr)); |
| 259 | if (!pcm->sync_ptr) |
| 260 | return -ENOMEM; |
| 261 | pcm->mmap_status = &pcm->sync_ptr->s.status; |
| 262 | pcm->mmap_control = &pcm->sync_ptr->c.control; |
| 263 | pcm->mmap_control->avail_min = 1; |
| 264 | pcm_sync_ptr(pcm, 0); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void pcm_hw_munmap_status(struct pcm *pcm) { |
| 270 | if (pcm->sync_ptr) { |
| 271 | free(pcm->sync_ptr); |
| 272 | pcm->sync_ptr = NULL; |
| 273 | } else { |
| 274 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 275 | if (pcm->mmap_status) |
| 276 | munmap(pcm->mmap_status, page_size); |
| 277 | if (pcm->mmap_control) |
| 278 | munmap(pcm->mmap_control, page_size); |
| 279 | } |
| 280 | pcm->mmap_status = NULL; |
| 281 | pcm->mmap_control = NULL; |
| 282 | } |
| 283 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 284 | static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset, |
| 285 | const char *src, unsigned int src_offset, |
| 286 | unsigned int frames) |
| 287 | { |
| 288 | int size_bytes = pcm_frames_to_bytes(pcm, frames); |
| 289 | int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset); |
| 290 | int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset); |
| 291 | |
| 292 | /* interleaved only atm */ |
| 293 | memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 294 | src + src_offset_bytes, size_bytes); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int pcm_mmap_write_areas(struct pcm *pcm, char *src, |
| 299 | unsigned int offset, unsigned int size) |
| 300 | { |
| 301 | void *pcm_areas; |
| 302 | int commit; |
| 303 | unsigned int pcm_offset, frames, count = 0; |
| 304 | |
| 305 | while (size > 0) { |
| 306 | frames = size; |
| 307 | pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames); |
| 308 | pcm_areas_copy(pcm, pcm_offset, src, offset, frames); |
| 309 | commit = pcm_mmap_commit(pcm, pcm_offset, frames); |
| 310 | if (commit < 0) { |
| 311 | oops(pcm, commit, "failed to commit %d frames\n", frames); |
| 312 | return commit; |
| 313 | } |
| 314 | |
| 315 | offset += commit; |
| 316 | count += commit; |
| 317 | size -= commit; |
| 318 | } |
| 319 | return count; |
| 320 | } |
| 321 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 322 | int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail, |
| 323 | struct timespec *tstamp) |
| 324 | { |
| 325 | int frames; |
| 326 | int rc; |
| 327 | snd_pcm_uframes_t hw_ptr; |
| 328 | |
| 329 | if (!pcm_is_ready(pcm)) |
| 330 | return -1; |
| 331 | |
| 332 | rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC); |
| 333 | if (rc < 0) |
| 334 | return -1; |
| 335 | |
| 336 | *tstamp = pcm->mmap_status->tstamp; |
| 337 | if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0) |
| 338 | return -1; |
| 339 | |
| 340 | hw_ptr = pcm->mmap_status->hw_ptr; |
| 341 | if (pcm->flags & PCM_IN) |
| 342 | frames = hw_ptr - pcm->mmap_control->appl_ptr; |
| 343 | else |
| 344 | frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 345 | |
| 346 | if (frames < 0) |
| 347 | return -1; |
| 348 | |
| 349 | *avail = (unsigned int)frames; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 354 | int pcm_write(struct pcm *pcm, void *data, unsigned int count) |
| 355 | { |
| 356 | struct snd_xferi x; |
| 357 | |
| 358 | if (pcm->flags & PCM_IN) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | x.buf = data; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 362 | x.frames = count / (pcm->config.channels * |
| 363 | pcm_format_to_bits(pcm->config.format) / 8); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 364 | |
| 365 | for (;;) { |
| 366 | if (!pcm->running) { |
| 367 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE)) |
| 368 | return oops(pcm, errno, "cannot prepare channel"); |
| 369 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) |
| 370 | return oops(pcm, errno, "cannot write initial data"); |
| 371 | pcm->running = 1; |
| 372 | return 0; |
| 373 | } |
| 374 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) { |
| 375 | pcm->running = 0; |
| 376 | if (errno == EPIPE) { |
| 377 | /* we failed to make our window -- try to restart */ |
| 378 | pcm->underruns++; |
| 379 | continue; |
| 380 | } |
| 381 | return oops(pcm, errno, "cannot write stream data"); |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | int pcm_read(struct pcm *pcm, void *data, unsigned int count) |
| 388 | { |
| 389 | struct snd_xferi x; |
| 390 | |
| 391 | if (!(pcm->flags & PCM_IN)) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | x.buf = data; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 395 | x.frames = count / (pcm->config.channels * |
| 396 | pcm_format_to_bits(pcm->config.format) / 8); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 397 | |
| 398 | for (;;) { |
| 399 | if (!pcm->running) { |
| 400 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE)) |
| 401 | return oops(pcm, errno, "cannot prepare channel"); |
| 402 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START)) |
| 403 | return oops(pcm, errno, "cannot start channel"); |
| 404 | pcm->running = 1; |
| 405 | } |
| 406 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) { |
| 407 | pcm->running = 0; |
| 408 | if (errno == EPIPE) { |
| 409 | /* we failed to make our window -- try to restart */ |
| 410 | pcm->underruns++; |
| 411 | continue; |
| 412 | } |
| 413 | return oops(pcm, errno, "cannot read stream data"); |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static struct pcm bad_pcm = { |
| 420 | .fd = -1, |
| 421 | }; |
| 422 | |
| 423 | int pcm_close(struct pcm *pcm) |
| 424 | { |
| 425 | if (pcm == &bad_pcm) |
| 426 | return 0; |
| 427 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 428 | pcm_hw_munmap_status(pcm); |
| 429 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 430 | if (pcm->flags & PCM_MMAP) { |
| 431 | pcm_stop(pcm); |
| 432 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 433 | } |
| 434 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 435 | if (pcm->fd >= 0) |
| 436 | close(pcm->fd); |
| 437 | pcm->running = 0; |
| 438 | pcm->buffer_size = 0; |
| 439 | pcm->fd = -1; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 440 | free(pcm); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 444 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
| 445 | unsigned int flags, struct pcm_config *config) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 446 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 447 | struct pcm *pcm; |
| 448 | struct snd_pcm_info info; |
| 449 | struct snd_pcm_hw_params params; |
| 450 | struct snd_pcm_sw_params sparams; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 451 | char fn[256]; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 452 | int rc; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 453 | |
| 454 | pcm = calloc(1, sizeof(struct pcm)); |
| 455 | if (!pcm || !config) |
| 456 | return &bad_pcm; /* TODO: could support default config here */ |
| 457 | |
| 458 | pcm->config = *config; |
| 459 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 460 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 461 | flags & PCM_IN ? 'c' : 'p'); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 462 | |
| 463 | pcm->flags = flags; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 464 | pcm->fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 465 | if (pcm->fd < 0) { |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 466 | oops(pcm, errno, "cannot open device '%s'", fn); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 467 | return pcm; |
| 468 | } |
| 469 | |
| 470 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 471 | oops(pcm, errno, "cannot get info"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 472 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | param_init(¶ms); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 476 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 477 | pcm_format_to_alsa(config->format)); |
| 478 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 479 | SNDRV_PCM_SUBFORMAT_STD); |
| 480 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 481 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 482 | pcm_format_to_bits(config->format)); |
| 483 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 484 | pcm_format_to_bits(config->format) * config->channels); |
| 485 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 486 | config->channels); |
| 487 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 488 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 489 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 490 | if (flags & PCM_NOIRQ) { |
| 491 | |
| 492 | if (!(flags & PCM_MMAP)) { |
| 493 | oops(pcm, -EINVAL, "noirq only currently supported with mmap()."); |
| 494 | goto fail; |
| 495 | } |
| 496 | |
| 497 | params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; |
| 498 | pcm->noirq_frames_per_msec = config->rate / 1000; |
| 499 | } |
| 500 | |
| 501 | if (flags & PCM_MMAP) |
| 502 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 503 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 504 | else |
| 505 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 506 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 507 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 508 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 509 | oops(pcm, errno, "cannot set hw params"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 510 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 513 | /* get our refined hw_params */ |
| 514 | config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 515 | config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 516 | pcm->buffer_size = config->period_count * config->period_size; |
| 517 | |
| 518 | if (flags & PCM_MMAP) { |
| 519 | pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size), |
| 520 | PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0); |
| 521 | if (pcm->mmap_buffer == MAP_FAILED) { |
| 522 | oops(pcm, -errno, "failed to mmap buffer %d bytes\n", |
| 523 | pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 524 | goto fail_close; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 529 | memset(&sparams, 0, sizeof(sparams)); |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 530 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 531 | sparams.period_step = 1; |
| 532 | sparams.avail_min = 1; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 533 | |
| 534 | if (!config->start_threshold) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 535 | pcm->config.start_threshold = sparams.start_threshold = |
| 536 | config->period_count * config->period_size / 2; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 537 | else |
| 538 | sparams.start_threshold = config->start_threshold; |
| 539 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 540 | /* pick a high stop threshold - todo: does this need further tuning */ |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 541 | if (!config->stop_threshold) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 542 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 543 | config->period_count * config->period_size * 10; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 544 | else |
| 545 | sparams.stop_threshold = config->stop_threshold; |
| 546 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 547 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 548 | sparams.silence_size = 0; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 549 | sparams.silence_threshold = config->silence_threshold; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 550 | pcm->boundary = sparams.boundary = pcm->buffer_size; |
John Grossman | 3bb114a | 2011-07-21 10:59:55 -0700 | [diff] [blame] | 551 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 552 | while (pcm->boundary * 2 <= LONG_MAX - pcm->buffer_size) |
| 553 | pcm->boundary *= 2; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 554 | |
| 555 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 556 | oops(pcm, errno, "cannot set sw params"); |
| 557 | goto fail; |
| 558 | } |
| 559 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 560 | rc = pcm_hw_mmap_status(pcm); |
| 561 | if (rc < 0) { |
| 562 | oops(pcm, rc, "mmap status failed"); |
| 563 | goto fail; |
| 564 | } |
| 565 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 566 | pcm->underruns = 0; |
| 567 | return pcm; |
| 568 | |
| 569 | fail: |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 570 | if (flags & PCM_MMAP) |
| 571 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 572 | fail_close: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 573 | close(pcm->fd); |
| 574 | pcm->fd = -1; |
| 575 | return pcm; |
| 576 | } |
| 577 | |
| 578 | int pcm_is_ready(struct pcm *pcm) |
| 579 | { |
| 580 | return pcm->fd >= 0; |
| 581 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 582 | |
| 583 | int pcm_start(struct pcm *pcm) |
| 584 | { |
| 585 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0) |
| 586 | return oops(pcm, errno, "cannot prepare channel"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 587 | |
| 588 | if (pcm->flags & PCM_MMAP) |
| 589 | pcm_sync_ptr(pcm, 0); |
| 590 | |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 591 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0) |
| 592 | return oops(pcm, errno, "cannot start channel"); |
| 593 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 594 | pcm->running = 1; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int pcm_stop(struct pcm *pcm) |
| 599 | { |
| 600 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0) |
| 601 | return oops(pcm, errno, "cannot stop channel"); |
| 602 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 603 | pcm->running = 0; |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame^] | 607 | static inline int pcm_mmap_playback_avail(struct pcm *pcm) |
| 608 | { |
| 609 | int avail; |
| 610 | |
| 611 | avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 612 | |
| 613 | if (avail < 0) |
| 614 | avail += pcm->boundary; |
| 615 | else if (avail > (int)pcm->boundary) |
| 616 | avail -= pcm->boundary; |
| 617 | |
| 618 | return avail; |
| 619 | } |
| 620 | |
| 621 | static inline int pcm_mmap_capture_avail(struct pcm *pcm) |
| 622 | { |
| 623 | int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr; |
| 624 | if (avail < 0) |
| 625 | avail += pcm->boundary; |
| 626 | return avail; |
| 627 | } |
| 628 | |
| 629 | static inline int pcm_mmap_avail(struct pcm *pcm) |
| 630 | { |
| 631 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC); |
| 632 | if (pcm->flags & PCM_IN) |
| 633 | return pcm_mmap_capture_avail(pcm); |
| 634 | else |
| 635 | return pcm_mmap_playback_avail(pcm); |
| 636 | } |
| 637 | |
| 638 | static void pcm_mmap_appl_forward(struct pcm *pcm, int frames) |
| 639 | { |
| 640 | unsigned int appl_ptr = pcm->mmap_control->appl_ptr; |
| 641 | appl_ptr += frames; |
| 642 | |
| 643 | /* check for boundary wrap */ |
| 644 | if (appl_ptr > pcm->boundary) |
| 645 | appl_ptr -= pcm->boundary; |
| 646 | pcm->mmap_control->appl_ptr = appl_ptr; |
| 647 | } |
| 648 | |
| 649 | int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, |
| 650 | unsigned int *frames) |
| 651 | { |
| 652 | unsigned int continuous, copy_frames, avail; |
| 653 | |
| 654 | /* return the mmap buffer */ |
| 655 | *areas = pcm->mmap_buffer; |
| 656 | |
| 657 | /* and the application offset in frames */ |
| 658 | *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size; |
| 659 | |
| 660 | avail = pcm_mmap_avail(pcm); |
| 661 | if (avail > pcm->buffer_size) |
| 662 | avail = pcm->buffer_size; |
| 663 | continuous = pcm->buffer_size - *offset; |
| 664 | |
| 665 | /* we can only copy frames if the are availabale and continuos */ |
| 666 | copy_frames = *frames; |
| 667 | if (copy_frames > avail) |
| 668 | copy_frames = avail; |
| 669 | if (copy_frames > continuous) |
| 670 | copy_frames = continuous; |
| 671 | *frames = copy_frames; |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames) |
| 677 | { |
| 678 | /* update the application pointer in userspace and kernel */ |
| 679 | pcm_mmap_appl_forward(pcm, frames); |
| 680 | pcm_sync_ptr(pcm, 0); |
| 681 | |
| 682 | return frames; |
| 683 | } |
| 684 | |
| 685 | int pcm_avail_update(struct pcm *pcm) |
| 686 | { |
| 687 | pcm_sync_ptr(pcm, 0); |
| 688 | return pcm_mmap_avail(pcm); |
| 689 | } |
| 690 | |
| 691 | int pcm_state(struct pcm *pcm) |
| 692 | { |
| 693 | int err = pcm_sync_ptr(pcm, 0); |
| 694 | if (err < 0) |
| 695 | return err; |
| 696 | |
| 697 | return pcm->mmap_status->state; |
| 698 | } |
| 699 | |
| 700 | int pcm_wait(struct pcm *pcm, int timeout) |
| 701 | { |
| 702 | struct pollfd pfd; |
| 703 | unsigned short revents = 0; |
| 704 | int err; |
| 705 | |
| 706 | pfd.fd = pcm->fd; |
| 707 | pfd.events = POLLOUT | POLLERR | POLLNVAL; |
| 708 | |
| 709 | do { |
| 710 | /* let's wait for avail or timeout */ |
| 711 | err = poll(&pfd, 1, timeout); |
| 712 | if (err < 0) |
| 713 | return -errno; |
| 714 | |
| 715 | /* timeout ? */ |
| 716 | if (err == 0) |
| 717 | return 0; |
| 718 | |
| 719 | /* have we been interrupted ? */ |
| 720 | if (errno == -EINTR) |
| 721 | continue; |
| 722 | |
| 723 | /* check for any errors */ |
| 724 | if (pfd.revents & (POLLERR | POLLNVAL)) { |
| 725 | switch (pcm_state(pcm)) { |
| 726 | case PCM_STATE_XRUN: |
| 727 | return -EPIPE; |
| 728 | case PCM_STATE_SUSPENDED: |
| 729 | return -ESTRPIPE; |
| 730 | case PCM_STATE_DISCONNECTED: |
| 731 | return -ENODEV; |
| 732 | default: |
| 733 | return -EIO; |
| 734 | } |
| 735 | } |
| 736 | /* poll again if fd not ready for IO */ |
| 737 | } while (!(pfd.revents & (POLLIN | POLLOUT))); |
| 738 | |
| 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | int pcm_mmap_write(struct pcm *pcm, void *buffer, unsigned int bytes) |
| 743 | { |
| 744 | int err = 0, frames, avail; |
| 745 | unsigned int offset = 0, count; |
| 746 | |
| 747 | if (bytes == 0) |
| 748 | return 0; |
| 749 | |
| 750 | count = pcm_bytes_to_frames(pcm, bytes); |
| 751 | |
| 752 | while (count > 0) { |
| 753 | |
| 754 | /* get the available space for writing new frames */ |
| 755 | avail = pcm_avail_update(pcm); |
| 756 | if (avail < 0) { |
| 757 | fprintf(stderr, "cannot determine available mmap frames"); |
| 758 | return err; |
| 759 | } |
| 760 | |
| 761 | /* start the audio if we reach the threshold */ |
| 762 | if (!pcm->running && |
| 763 | (pcm->buffer_size - avail) >= pcm->config.start_threshold) { |
| 764 | if (pcm_start(pcm) < 0) { |
| 765 | fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n", |
| 766 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 767 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 768 | avail); |
| 769 | return -errno; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* sleep until we have space to write new frames */ |
| 774 | if (pcm->running && |
| 775 | (unsigned int)avail < pcm->mmap_control->avail_min) { |
| 776 | int time = -1; |
| 777 | |
| 778 | if (pcm->flags & PCM_NOIRQ) |
| 779 | time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min) |
| 780 | / pcm->noirq_frames_per_msec; |
| 781 | |
| 782 | err = pcm_wait(pcm, time); |
| 783 | if (err < 0) { |
| 784 | pcm->running = 0; |
| 785 | fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n", |
| 786 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 787 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 788 | avail); |
| 789 | pcm->mmap_control->appl_ptr = 0; |
| 790 | return err; |
| 791 | } |
| 792 | continue; |
| 793 | } |
| 794 | |
| 795 | frames = count; |
| 796 | if (frames > avail) |
| 797 | frames = avail; |
| 798 | |
| 799 | if (!frames) |
| 800 | break; |
| 801 | |
| 802 | /* copy frames from buffer */ |
| 803 | frames = pcm_mmap_write_areas(pcm, buffer, offset, frames); |
| 804 | if (frames < 0) { |
| 805 | fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n", |
| 806 | (unsigned int)pcm->mmap_status->hw_ptr, |
| 807 | (unsigned int)pcm->mmap_control->appl_ptr, |
| 808 | avail); |
| 809 | return frames; |
| 810 | } |
| 811 | |
| 812 | offset += frames; |
| 813 | count -= frames; |
| 814 | } |
| 815 | |
| 816 | _end: |
| 817 | return 0; |
| 818 | } |