Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Digital Audio (PCM) abstract layer |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 4 | * Abramo Bagnara <abramo@alsa-project.org> |
| 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sound/driver.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/time.h> |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/control.h> |
| 28 | #include <sound/info.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/timer.h> |
| 32 | |
| 33 | /* |
| 34 | * fill ring buffer with silence |
| 35 | * runtime->silence_start: starting pointer to silence area |
| 36 | * runtime->silence_filled: size filled with silence |
| 37 | * runtime->silence_threshold: threshold from application |
| 38 | * runtime->silence_size: maximal size from application |
| 39 | * |
| 40 | * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately |
| 41 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 42 | void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 44 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | snd_pcm_uframes_t frames, ofs, transfer; |
| 46 | |
| 47 | if (runtime->silence_size < runtime->boundary) { |
| 48 | snd_pcm_sframes_t noise_dist, n; |
| 49 | if (runtime->silence_start != runtime->control->appl_ptr) { |
| 50 | n = runtime->control->appl_ptr - runtime->silence_start; |
| 51 | if (n < 0) |
| 52 | n += runtime->boundary; |
| 53 | if ((snd_pcm_uframes_t)n < runtime->silence_filled) |
| 54 | runtime->silence_filled -= n; |
| 55 | else |
| 56 | runtime->silence_filled = 0; |
| 57 | runtime->silence_start = runtime->control->appl_ptr; |
| 58 | } |
Takashi Iwai | 235475c | 2005-12-07 15:28:07 +0100 | [diff] [blame] | 59 | if (runtime->silence_filled >= runtime->buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
| 62 | if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) |
| 63 | return; |
| 64 | frames = runtime->silence_threshold - noise_dist; |
| 65 | if (frames > runtime->silence_size) |
| 66 | frames = runtime->silence_size; |
| 67 | } else { |
| 68 | if (new_hw_ptr == ULONG_MAX) { /* initialization */ |
| 69 | snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); |
| 70 | runtime->silence_filled = avail > 0 ? avail : 0; |
| 71 | runtime->silence_start = (runtime->status->hw_ptr + |
| 72 | runtime->silence_filled) % |
| 73 | runtime->boundary; |
| 74 | } else { |
| 75 | ofs = runtime->status->hw_ptr; |
| 76 | frames = new_hw_ptr - ofs; |
| 77 | if ((snd_pcm_sframes_t)frames < 0) |
| 78 | frames += runtime->boundary; |
| 79 | runtime->silence_filled -= frames; |
| 80 | if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { |
| 81 | runtime->silence_filled = 0; |
| 82 | runtime->silence_start = (ofs + frames) - runtime->buffer_size; |
| 83 | } else { |
| 84 | runtime->silence_start = ofs - runtime->silence_filled; |
| 85 | } |
| 86 | if ((snd_pcm_sframes_t)runtime->silence_start < 0) |
| 87 | runtime->silence_start += runtime->boundary; |
| 88 | } |
| 89 | frames = runtime->buffer_size - runtime->silence_filled; |
| 90 | } |
| 91 | snd_assert(frames <= runtime->buffer_size, return); |
| 92 | if (frames == 0) |
| 93 | return; |
| 94 | ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; |
| 95 | while (frames > 0) { |
| 96 | transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; |
| 97 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || |
| 98 | runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) { |
| 99 | if (substream->ops->silence) { |
| 100 | int err; |
| 101 | err = substream->ops->silence(substream, -1, ofs, transfer); |
| 102 | snd_assert(err >= 0, ); |
| 103 | } else { |
| 104 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs); |
| 105 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels); |
| 106 | } |
| 107 | } else { |
| 108 | unsigned int c; |
| 109 | unsigned int channels = runtime->channels; |
| 110 | if (substream->ops->silence) { |
| 111 | for (c = 0; c < channels; ++c) { |
| 112 | int err; |
| 113 | err = substream->ops->silence(substream, c, ofs, transfer); |
| 114 | snd_assert(err >= 0, ); |
| 115 | } |
| 116 | } else { |
| 117 | size_t dma_csize = runtime->dma_bytes / channels; |
| 118 | for (c = 0; c < channels; ++c) { |
| 119 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs); |
| 120 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | runtime->silence_filled += transfer; |
| 125 | frames -= transfer; |
| 126 | ofs = 0; |
| 127 | } |
| 128 | } |
| 129 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 130 | static void xrun(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
Jaroslav Kysela | 61fb63c | 2006-04-24 21:57:16 +0200 | [diff] [blame] | 133 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (substream->pstr->xrun_debug) { |
| 135 | snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n", |
| 136 | substream->pcm->card->number, |
| 137 | substream->pcm->device, |
| 138 | substream->stream ? 'c' : 'p'); |
| 139 | if (substream->pstr->xrun_debug > 1) |
| 140 | dump_stack(); |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 145 | static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream, |
| 146 | struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | snd_pcm_uframes_t pos; |
| 149 | |
| 150 | pos = substream->ops->pointer(substream); |
| 151 | if (pos == SNDRV_PCM_POS_XRUN) |
| 152 | return pos; /* XRUN */ |
| 153 | if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP) |
Takashi Iwai | 07799e7 | 2005-10-10 11:49:49 +0200 | [diff] [blame] | 154 | getnstimeofday((struct timespec *)&runtime->status->tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | #ifdef CONFIG_SND_DEBUG |
| 156 | if (pos >= runtime->buffer_size) { |
| 157 | snd_printk(KERN_ERR "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | pos -= pos % runtime->min_align; |
| 161 | return pos; |
| 162 | } |
| 163 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 164 | static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream, |
| 165 | struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | snd_pcm_uframes_t avail; |
| 168 | |
| 169 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 170 | avail = snd_pcm_playback_avail(runtime); |
| 171 | else |
| 172 | avail = snd_pcm_capture_avail(runtime); |
| 173 | if (avail > runtime->avail_max) |
| 174 | runtime->avail_max = avail; |
| 175 | if (avail >= runtime->stop_threshold) { |
| 176 | if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING) |
| 177 | snd_pcm_drain_done(substream); |
| 178 | else |
| 179 | xrun(substream); |
| 180 | return -EPIPE; |
| 181 | } |
| 182 | if (avail >= runtime->control->avail_min) |
| 183 | wake_up(&runtime->sleep); |
| 184 | return 0; |
| 185 | } |
| 186 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 187 | static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 189 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | snd_pcm_uframes_t pos; |
| 191 | snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt; |
| 192 | snd_pcm_sframes_t delta; |
| 193 | |
| 194 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); |
| 195 | if (pos == SNDRV_PCM_POS_XRUN) { |
| 196 | xrun(substream); |
| 197 | return -EPIPE; |
| 198 | } |
| 199 | if (runtime->period_size == runtime->buffer_size) |
| 200 | goto __next_buf; |
| 201 | new_hw_ptr = runtime->hw_ptr_base + pos; |
| 202 | hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size; |
| 203 | |
| 204 | delta = hw_ptr_interrupt - new_hw_ptr; |
| 205 | if (delta > 0) { |
| 206 | if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) { |
Jaroslav Kysela | 61fb63c | 2006-04-24 21:57:16 +0200 | [diff] [blame] | 207 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (runtime->periods > 1 && substream->pstr->xrun_debug) { |
| 209 | snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2); |
| 210 | if (substream->pstr->xrun_debug > 1) |
| 211 | dump_stack(); |
| 212 | } |
| 213 | #endif |
| 214 | return 0; |
| 215 | } |
| 216 | __next_buf: |
| 217 | runtime->hw_ptr_base += runtime->buffer_size; |
| 218 | if (runtime->hw_ptr_base == runtime->boundary) |
| 219 | runtime->hw_ptr_base = 0; |
| 220 | new_hw_ptr = runtime->hw_ptr_base + pos; |
| 221 | } |
| 222 | |
| 223 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 224 | runtime->silence_size > 0) |
| 225 | snd_pcm_playback_silence(substream, new_hw_ptr); |
| 226 | |
| 227 | runtime->status->hw_ptr = new_hw_ptr; |
| 228 | runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size; |
| 229 | |
| 230 | return snd_pcm_update_hw_ptr_post(substream, runtime); |
| 231 | } |
| 232 | |
| 233 | /* CAUTION: call it with irq disabled */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 234 | int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 236 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | snd_pcm_uframes_t pos; |
| 238 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr; |
| 239 | snd_pcm_sframes_t delta; |
| 240 | |
| 241 | old_hw_ptr = runtime->status->hw_ptr; |
| 242 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); |
| 243 | if (pos == SNDRV_PCM_POS_XRUN) { |
| 244 | xrun(substream); |
| 245 | return -EPIPE; |
| 246 | } |
| 247 | new_hw_ptr = runtime->hw_ptr_base + pos; |
| 248 | |
| 249 | delta = old_hw_ptr - new_hw_ptr; |
| 250 | if (delta > 0) { |
| 251 | if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) { |
Jaroslav Kysela | 61fb63c | 2006-04-24 21:57:16 +0200 | [diff] [blame] | 252 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if (runtime->periods > 2 && substream->pstr->xrun_debug) { |
| 254 | snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2); |
| 255 | if (substream->pstr->xrun_debug > 1) |
| 256 | dump_stack(); |
| 257 | } |
| 258 | #endif |
| 259 | return 0; |
| 260 | } |
| 261 | runtime->hw_ptr_base += runtime->buffer_size; |
| 262 | if (runtime->hw_ptr_base == runtime->boundary) |
| 263 | runtime->hw_ptr_base = 0; |
| 264 | new_hw_ptr = runtime->hw_ptr_base + pos; |
| 265 | } |
| 266 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 267 | runtime->silence_size > 0) |
| 268 | snd_pcm_playback_silence(substream, new_hw_ptr); |
| 269 | |
| 270 | runtime->status->hw_ptr = new_hw_ptr; |
| 271 | |
| 272 | return snd_pcm_update_hw_ptr_post(substream, runtime); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * snd_pcm_set_ops - set the PCM operators |
| 277 | * @pcm: the pcm instance |
| 278 | * @direction: stream direction, SNDRV_PCM_STREAM_XXX |
| 279 | * @ops: the operator table |
| 280 | * |
| 281 | * Sets the given PCM operators to the pcm instance. |
| 282 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 283 | void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 285 | struct snd_pcm_str *stream = &pcm->streams[direction]; |
| 286 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | for (substream = stream->substream; substream != NULL; substream = substream->next) |
| 289 | substream->ops = ops; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * snd_pcm_sync - set the PCM sync id |
| 295 | * @substream: the pcm substream |
| 296 | * |
| 297 | * Sets the PCM sync identifier for the card. |
| 298 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 299 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 301 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | runtime->sync.id32[0] = substream->pcm->card->number; |
| 304 | runtime->sync.id32[1] = -1; |
| 305 | runtime->sync.id32[2] = -1; |
| 306 | runtime->sync.id32[3] = -1; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Standard ioctl routine |
| 311 | */ |
| 312 | |
| 313 | /* Code taken from alsa-lib */ |
| 314 | #define assert(a) snd_assert((a), return -EINVAL) |
| 315 | |
| 316 | static inline unsigned int div32(unsigned int a, unsigned int b, |
| 317 | unsigned int *r) |
| 318 | { |
| 319 | if (b == 0) { |
| 320 | *r = 0; |
| 321 | return UINT_MAX; |
| 322 | } |
| 323 | *r = a % b; |
| 324 | return a / b; |
| 325 | } |
| 326 | |
| 327 | static inline unsigned int div_down(unsigned int a, unsigned int b) |
| 328 | { |
| 329 | if (b == 0) |
| 330 | return UINT_MAX; |
| 331 | return a / b; |
| 332 | } |
| 333 | |
| 334 | static inline unsigned int div_up(unsigned int a, unsigned int b) |
| 335 | { |
| 336 | unsigned int r; |
| 337 | unsigned int q; |
| 338 | if (b == 0) |
| 339 | return UINT_MAX; |
| 340 | q = div32(a, b, &r); |
| 341 | if (r) |
| 342 | ++q; |
| 343 | return q; |
| 344 | } |
| 345 | |
| 346 | static inline unsigned int mul(unsigned int a, unsigned int b) |
| 347 | { |
| 348 | if (a == 0) |
| 349 | return 0; |
| 350 | if (div_down(UINT_MAX, a) < b) |
| 351 | return UINT_MAX; |
| 352 | return a * b; |
| 353 | } |
| 354 | |
| 355 | static inline unsigned int muldiv32(unsigned int a, unsigned int b, |
| 356 | unsigned int c, unsigned int *r) |
| 357 | { |
| 358 | u_int64_t n = (u_int64_t) a * b; |
| 359 | if (c == 0) { |
| 360 | snd_assert(n > 0, ); |
| 361 | *r = 0; |
| 362 | return UINT_MAX; |
| 363 | } |
| 364 | div64_32(&n, c, r); |
| 365 | if (n >= UINT_MAX) { |
| 366 | *r = 0; |
| 367 | return UINT_MAX; |
| 368 | } |
| 369 | return n; |
| 370 | } |
| 371 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 372 | static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | int changed = 0; |
| 375 | assert(!snd_interval_empty(i)); |
| 376 | if (i->min < min) { |
| 377 | i->min = min; |
| 378 | i->openmin = openmin; |
| 379 | changed = 1; |
| 380 | } else if (i->min == min && !i->openmin && openmin) { |
| 381 | i->openmin = 1; |
| 382 | changed = 1; |
| 383 | } |
| 384 | if (i->integer) { |
| 385 | if (i->openmin) { |
| 386 | i->min++; |
| 387 | i->openmin = 0; |
| 388 | } |
| 389 | } |
| 390 | if (snd_interval_checkempty(i)) { |
| 391 | snd_interval_none(i); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | return changed; |
| 395 | } |
| 396 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 397 | static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | int changed = 0; |
| 400 | assert(!snd_interval_empty(i)); |
| 401 | if (i->max > max) { |
| 402 | i->max = max; |
| 403 | i->openmax = openmax; |
| 404 | changed = 1; |
| 405 | } else if (i->max == max && !i->openmax && openmax) { |
| 406 | i->openmax = 1; |
| 407 | changed = 1; |
| 408 | } |
| 409 | if (i->integer) { |
| 410 | if (i->openmax) { |
| 411 | i->max--; |
| 412 | i->openmax = 0; |
| 413 | } |
| 414 | } |
| 415 | if (snd_interval_checkempty(i)) { |
| 416 | snd_interval_none(i); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | return changed; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * snd_interval_refine - refine the interval value of configurator |
| 424 | * @i: the interval value to refine |
| 425 | * @v: the interval value to refer to |
| 426 | * |
| 427 | * Refines the interval value with the reference value. |
| 428 | * The interval is changed to the range satisfying both intervals. |
| 429 | * The interval status (min, max, integer, etc.) are evaluated. |
| 430 | * |
| 431 | * Returns non-zero if the value is changed, zero if not changed. |
| 432 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 433 | int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | int changed = 0; |
| 436 | assert(!snd_interval_empty(i)); |
| 437 | if (i->min < v->min) { |
| 438 | i->min = v->min; |
| 439 | i->openmin = v->openmin; |
| 440 | changed = 1; |
| 441 | } else if (i->min == v->min && !i->openmin && v->openmin) { |
| 442 | i->openmin = 1; |
| 443 | changed = 1; |
| 444 | } |
| 445 | if (i->max > v->max) { |
| 446 | i->max = v->max; |
| 447 | i->openmax = v->openmax; |
| 448 | changed = 1; |
| 449 | } else if (i->max == v->max && !i->openmax && v->openmax) { |
| 450 | i->openmax = 1; |
| 451 | changed = 1; |
| 452 | } |
| 453 | if (!i->integer && v->integer) { |
| 454 | i->integer = 1; |
| 455 | changed = 1; |
| 456 | } |
| 457 | if (i->integer) { |
| 458 | if (i->openmin) { |
| 459 | i->min++; |
| 460 | i->openmin = 0; |
| 461 | } |
| 462 | if (i->openmax) { |
| 463 | i->max--; |
| 464 | i->openmax = 0; |
| 465 | } |
| 466 | } else if (!i->openmin && !i->openmax && i->min == i->max) |
| 467 | i->integer = 1; |
| 468 | if (snd_interval_checkempty(i)) { |
| 469 | snd_interval_none(i); |
| 470 | return -EINVAL; |
| 471 | } |
| 472 | return changed; |
| 473 | } |
| 474 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 475 | static int snd_interval_refine_first(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | assert(!snd_interval_empty(i)); |
| 478 | if (snd_interval_single(i)) |
| 479 | return 0; |
| 480 | i->max = i->min; |
| 481 | i->openmax = i->openmin; |
| 482 | if (i->openmax) |
| 483 | i->max++; |
| 484 | return 1; |
| 485 | } |
| 486 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 487 | static int snd_interval_refine_last(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | assert(!snd_interval_empty(i)); |
| 490 | if (snd_interval_single(i)) |
| 491 | return 0; |
| 492 | i->min = i->max; |
| 493 | i->openmin = i->openmax; |
| 494 | if (i->openmin) |
| 495 | i->min--; |
| 496 | return 1; |
| 497 | } |
| 498 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 499 | static int snd_interval_refine_set(struct snd_interval *i, unsigned int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 501 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | t.empty = 0; |
| 503 | t.min = t.max = val; |
| 504 | t.openmin = t.openmax = 0; |
| 505 | t.integer = 1; |
| 506 | return snd_interval_refine(i, &t); |
| 507 | } |
| 508 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 509 | void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | if (a->empty || b->empty) { |
| 512 | snd_interval_none(c); |
| 513 | return; |
| 514 | } |
| 515 | c->empty = 0; |
| 516 | c->min = mul(a->min, b->min); |
| 517 | c->openmin = (a->openmin || b->openmin); |
| 518 | c->max = mul(a->max, b->max); |
| 519 | c->openmax = (a->openmax || b->openmax); |
| 520 | c->integer = (a->integer && b->integer); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * snd_interval_div - refine the interval value with division |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 525 | * @a: dividend |
| 526 | * @b: divisor |
| 527 | * @c: quotient |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * |
| 529 | * c = a / b |
| 530 | * |
| 531 | * Returns non-zero if the value is changed, zero if not changed. |
| 532 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 533 | void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
| 535 | unsigned int r; |
| 536 | if (a->empty || b->empty) { |
| 537 | snd_interval_none(c); |
| 538 | return; |
| 539 | } |
| 540 | c->empty = 0; |
| 541 | c->min = div32(a->min, b->max, &r); |
| 542 | c->openmin = (r || a->openmin || b->openmax); |
| 543 | if (b->min > 0) { |
| 544 | c->max = div32(a->max, b->min, &r); |
| 545 | if (r) { |
| 546 | c->max++; |
| 547 | c->openmax = 1; |
| 548 | } else |
| 549 | c->openmax = (a->openmax || b->openmin); |
| 550 | } else { |
| 551 | c->max = UINT_MAX; |
| 552 | c->openmax = 0; |
| 553 | } |
| 554 | c->integer = 0; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * snd_interval_muldivk - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 559 | * @a: dividend 1 |
| 560 | * @b: dividend 2 |
| 561 | * @k: divisor (as integer) |
| 562 | * @c: result |
| 563 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | * c = a * b / k |
| 565 | * |
| 566 | * Returns non-zero if the value is changed, zero if not changed. |
| 567 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 568 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
| 569 | unsigned int k, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
| 571 | unsigned int r; |
| 572 | if (a->empty || b->empty) { |
| 573 | snd_interval_none(c); |
| 574 | return; |
| 575 | } |
| 576 | c->empty = 0; |
| 577 | c->min = muldiv32(a->min, b->min, k, &r); |
| 578 | c->openmin = (r || a->openmin || b->openmin); |
| 579 | c->max = muldiv32(a->max, b->max, k, &r); |
| 580 | if (r) { |
| 581 | c->max++; |
| 582 | c->openmax = 1; |
| 583 | } else |
| 584 | c->openmax = (a->openmax || b->openmax); |
| 585 | c->integer = 0; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * snd_interval_mulkdiv - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 590 | * @a: dividend 1 |
| 591 | * @k: dividend 2 (as integer) |
| 592 | * @b: divisor |
| 593 | * @c: result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | * |
| 595 | * c = a * k / b |
| 596 | * |
| 597 | * Returns non-zero if the value is changed, zero if not changed. |
| 598 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 599 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
| 600 | const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
| 602 | unsigned int r; |
| 603 | if (a->empty || b->empty) { |
| 604 | snd_interval_none(c); |
| 605 | return; |
| 606 | } |
| 607 | c->empty = 0; |
| 608 | c->min = muldiv32(a->min, k, b->max, &r); |
| 609 | c->openmin = (r || a->openmin || b->openmax); |
| 610 | if (b->min > 0) { |
| 611 | c->max = muldiv32(a->max, k, b->min, &r); |
| 612 | if (r) { |
| 613 | c->max++; |
| 614 | c->openmax = 1; |
| 615 | } else |
| 616 | c->openmax = (a->openmax || b->openmin); |
| 617 | } else { |
| 618 | c->max = UINT_MAX; |
| 619 | c->openmax = 0; |
| 620 | } |
| 621 | c->integer = 0; |
| 622 | } |
| 623 | |
| 624 | #undef assert |
| 625 | /* ---- */ |
| 626 | |
| 627 | |
| 628 | /** |
| 629 | * snd_interval_ratnum - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 630 | * @i: interval to refine |
| 631 | * @rats_count: number of ratnum_t |
| 632 | * @rats: ratnum_t array |
| 633 | * @nump: pointer to store the resultant numerator |
| 634 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | * |
| 636 | * Returns non-zero if the value is changed, zero if not changed. |
| 637 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 638 | int snd_interval_ratnum(struct snd_interval *i, |
| 639 | unsigned int rats_count, struct snd_ratnum *rats, |
| 640 | unsigned int *nump, unsigned int *denp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
| 642 | unsigned int best_num, best_diff, best_den; |
| 643 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 644 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | int err; |
| 646 | |
| 647 | best_num = best_den = best_diff = 0; |
| 648 | for (k = 0; k < rats_count; ++k) { |
| 649 | unsigned int num = rats[k].num; |
| 650 | unsigned int den; |
| 651 | unsigned int q = i->min; |
| 652 | int diff; |
| 653 | if (q == 0) |
| 654 | q = 1; |
| 655 | den = div_down(num, q); |
| 656 | if (den < rats[k].den_min) |
| 657 | continue; |
| 658 | if (den > rats[k].den_max) |
| 659 | den = rats[k].den_max; |
| 660 | else { |
| 661 | unsigned int r; |
| 662 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 663 | if (r != 0) |
| 664 | den -= r; |
| 665 | } |
| 666 | diff = num - q * den; |
| 667 | if (best_num == 0 || |
| 668 | diff * best_den < best_diff * den) { |
| 669 | best_diff = diff; |
| 670 | best_den = den; |
| 671 | best_num = num; |
| 672 | } |
| 673 | } |
| 674 | if (best_den == 0) { |
| 675 | i->empty = 1; |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | t.min = div_down(best_num, best_den); |
| 679 | t.openmin = !!(best_num % best_den); |
| 680 | |
| 681 | best_num = best_den = best_diff = 0; |
| 682 | for (k = 0; k < rats_count; ++k) { |
| 683 | unsigned int num = rats[k].num; |
| 684 | unsigned int den; |
| 685 | unsigned int q = i->max; |
| 686 | int diff; |
| 687 | if (q == 0) { |
| 688 | i->empty = 1; |
| 689 | return -EINVAL; |
| 690 | } |
| 691 | den = div_up(num, q); |
| 692 | if (den > rats[k].den_max) |
| 693 | continue; |
| 694 | if (den < rats[k].den_min) |
| 695 | den = rats[k].den_min; |
| 696 | else { |
| 697 | unsigned int r; |
| 698 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 699 | if (r != 0) |
| 700 | den += rats[k].den_step - r; |
| 701 | } |
| 702 | diff = q * den - num; |
| 703 | if (best_num == 0 || |
| 704 | diff * best_den < best_diff * den) { |
| 705 | best_diff = diff; |
| 706 | best_den = den; |
| 707 | best_num = num; |
| 708 | } |
| 709 | } |
| 710 | if (best_den == 0) { |
| 711 | i->empty = 1; |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | t.max = div_up(best_num, best_den); |
| 715 | t.openmax = !!(best_num % best_den); |
| 716 | t.integer = 0; |
| 717 | err = snd_interval_refine(i, &t); |
| 718 | if (err < 0) |
| 719 | return err; |
| 720 | |
| 721 | if (snd_interval_single(i)) { |
| 722 | if (nump) |
| 723 | *nump = best_num; |
| 724 | if (denp) |
| 725 | *denp = best_den; |
| 726 | } |
| 727 | return err; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * snd_interval_ratden - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 732 | * @i: interval to refine |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 733 | * @rats_count: number of struct ratden |
| 734 | * @rats: struct ratden array |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 735 | * @nump: pointer to store the resultant numerator |
| 736 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | * |
| 738 | * Returns non-zero if the value is changed, zero if not changed. |
| 739 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 740 | static int snd_interval_ratden(struct snd_interval *i, |
| 741 | unsigned int rats_count, struct snd_ratden *rats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | unsigned int *nump, unsigned int *denp) |
| 743 | { |
| 744 | unsigned int best_num, best_diff, best_den; |
| 745 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 746 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | int err; |
| 748 | |
| 749 | best_num = best_den = best_diff = 0; |
| 750 | for (k = 0; k < rats_count; ++k) { |
| 751 | unsigned int num; |
| 752 | unsigned int den = rats[k].den; |
| 753 | unsigned int q = i->min; |
| 754 | int diff; |
| 755 | num = mul(q, den); |
| 756 | if (num > rats[k].num_max) |
| 757 | continue; |
| 758 | if (num < rats[k].num_min) |
| 759 | num = rats[k].num_max; |
| 760 | else { |
| 761 | unsigned int r; |
| 762 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 763 | if (r != 0) |
| 764 | num += rats[k].num_step - r; |
| 765 | } |
| 766 | diff = num - q * den; |
| 767 | if (best_num == 0 || |
| 768 | diff * best_den < best_diff * den) { |
| 769 | best_diff = diff; |
| 770 | best_den = den; |
| 771 | best_num = num; |
| 772 | } |
| 773 | } |
| 774 | if (best_den == 0) { |
| 775 | i->empty = 1; |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | t.min = div_down(best_num, best_den); |
| 779 | t.openmin = !!(best_num % best_den); |
| 780 | |
| 781 | best_num = best_den = best_diff = 0; |
| 782 | for (k = 0; k < rats_count; ++k) { |
| 783 | unsigned int num; |
| 784 | unsigned int den = rats[k].den; |
| 785 | unsigned int q = i->max; |
| 786 | int diff; |
| 787 | num = mul(q, den); |
| 788 | if (num < rats[k].num_min) |
| 789 | continue; |
| 790 | if (num > rats[k].num_max) |
| 791 | num = rats[k].num_max; |
| 792 | else { |
| 793 | unsigned int r; |
| 794 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 795 | if (r != 0) |
| 796 | num -= r; |
| 797 | } |
| 798 | diff = q * den - num; |
| 799 | if (best_num == 0 || |
| 800 | diff * best_den < best_diff * den) { |
| 801 | best_diff = diff; |
| 802 | best_den = den; |
| 803 | best_num = num; |
| 804 | } |
| 805 | } |
| 806 | if (best_den == 0) { |
| 807 | i->empty = 1; |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | t.max = div_up(best_num, best_den); |
| 811 | t.openmax = !!(best_num % best_den); |
| 812 | t.integer = 0; |
| 813 | err = snd_interval_refine(i, &t); |
| 814 | if (err < 0) |
| 815 | return err; |
| 816 | |
| 817 | if (snd_interval_single(i)) { |
| 818 | if (nump) |
| 819 | *nump = best_num; |
| 820 | if (denp) |
| 821 | *denp = best_den; |
| 822 | } |
| 823 | return err; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * snd_interval_list - refine the interval value from the list |
| 828 | * @i: the interval value to refine |
| 829 | * @count: the number of elements in the list |
| 830 | * @list: the value list |
| 831 | * @mask: the bit-mask to evaluate |
| 832 | * |
| 833 | * Refines the interval value from the list. |
| 834 | * When mask is non-zero, only the elements corresponding to bit 1 are |
| 835 | * evaluated. |
| 836 | * |
| 837 | * Returns non-zero if the value is changed, zero if not changed. |
| 838 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 839 | int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
| 841 | unsigned int k; |
| 842 | int changed = 0; |
| 843 | for (k = 0; k < count; k++) { |
| 844 | if (mask && !(mask & (1 << k))) |
| 845 | continue; |
| 846 | if (i->min == list[k] && !i->openmin) |
| 847 | goto _l1; |
| 848 | if (i->min < list[k]) { |
| 849 | i->min = list[k]; |
| 850 | i->openmin = 0; |
| 851 | changed = 1; |
| 852 | goto _l1; |
| 853 | } |
| 854 | } |
| 855 | i->empty = 1; |
| 856 | return -EINVAL; |
| 857 | _l1: |
| 858 | for (k = count; k-- > 0;) { |
| 859 | if (mask && !(mask & (1 << k))) |
| 860 | continue; |
| 861 | if (i->max == list[k] && !i->openmax) |
| 862 | goto _l2; |
| 863 | if (i->max > list[k]) { |
| 864 | i->max = list[k]; |
| 865 | i->openmax = 0; |
| 866 | changed = 1; |
| 867 | goto _l2; |
| 868 | } |
| 869 | } |
| 870 | i->empty = 1; |
| 871 | return -EINVAL; |
| 872 | _l2: |
| 873 | if (snd_interval_checkempty(i)) { |
| 874 | i->empty = 1; |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | return changed; |
| 878 | } |
| 879 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 880 | static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
| 882 | unsigned int n; |
| 883 | int changed = 0; |
| 884 | n = (i->min - min) % step; |
| 885 | if (n != 0 || i->openmin) { |
| 886 | i->min += step - n; |
| 887 | changed = 1; |
| 888 | } |
| 889 | n = (i->max - min) % step; |
| 890 | if (n != 0 || i->openmax) { |
| 891 | i->max -= n; |
| 892 | changed = 1; |
| 893 | } |
| 894 | if (snd_interval_checkempty(i)) { |
| 895 | i->empty = 1; |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | return changed; |
| 899 | } |
| 900 | |
| 901 | /* Info constraints helpers */ |
| 902 | |
| 903 | /** |
| 904 | * snd_pcm_hw_rule_add - add the hw-constraint rule |
| 905 | * @runtime: the pcm runtime instance |
| 906 | * @cond: condition bits |
| 907 | * @var: the variable to evaluate |
| 908 | * @func: the evaluation function |
| 909 | * @private: the private data pointer passed to function |
| 910 | * @dep: the dependent variables |
| 911 | * |
| 912 | * Returns zero if successful, or a negative error code on failure. |
| 913 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 914 | int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | int var, |
| 916 | snd_pcm_hw_rule_func_t func, void *private, |
| 917 | int dep, ...) |
| 918 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 919 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 920 | struct snd_pcm_hw_rule *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | unsigned int k; |
| 922 | va_list args; |
| 923 | va_start(args, dep); |
| 924 | if (constrs->rules_num >= constrs->rules_all) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 925 | struct snd_pcm_hw_rule *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | unsigned int new_rules = constrs->rules_all + 16; |
| 927 | new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL); |
| 928 | if (!new) |
| 929 | return -ENOMEM; |
| 930 | if (constrs->rules) { |
| 931 | memcpy(new, constrs->rules, |
| 932 | constrs->rules_num * sizeof(*c)); |
| 933 | kfree(constrs->rules); |
| 934 | } |
| 935 | constrs->rules = new; |
| 936 | constrs->rules_all = new_rules; |
| 937 | } |
| 938 | c = &constrs->rules[constrs->rules_num]; |
| 939 | c->cond = cond; |
| 940 | c->func = func; |
| 941 | c->var = var; |
| 942 | c->private = private; |
| 943 | k = 0; |
| 944 | while (1) { |
| 945 | snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL); |
| 946 | c->deps[k++] = dep; |
| 947 | if (dep < 0) |
| 948 | break; |
| 949 | dep = va_arg(args, int); |
| 950 | } |
| 951 | constrs->rules_num++; |
| 952 | va_end(args); |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * snd_pcm_hw_constraint_mask |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 958 | * @runtime: PCM runtime instance |
| 959 | * @var: hw_params variable to apply the mask |
| 960 | * @mask: the bitmap mask |
| 961 | * |
| 962 | * Apply the constraint of the given bitmap mask to a mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 964 | int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | u_int32_t mask) |
| 966 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 967 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 968 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | *maskp->bits &= mask; |
| 970 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ |
| 971 | if (*maskp->bits == 0) |
| 972 | return -EINVAL; |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * snd_pcm_hw_constraint_mask64 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 978 | * @runtime: PCM runtime instance |
| 979 | * @var: hw_params variable to apply the mask |
| 980 | * @mask: the 64bit bitmap mask |
| 981 | * |
| 982 | * Apply the constraint of the given bitmap mask to a mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 984 | int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | u_int64_t mask) |
| 986 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 987 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 988 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | maskp->bits[0] &= (u_int32_t)mask; |
| 990 | maskp->bits[1] &= (u_int32_t)(mask >> 32); |
| 991 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ |
| 992 | if (! maskp->bits[0] && ! maskp->bits[1]) |
| 993 | return -EINVAL; |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * snd_pcm_hw_constraint_integer |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 999 | * @runtime: PCM runtime instance |
| 1000 | * @var: hw_params variable to apply the integer constraint |
| 1001 | * |
| 1002 | * Apply the constraint of integer to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1004 | int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1006 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * snd_pcm_hw_constraint_minmax |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1012 | * @runtime: PCM runtime instance |
| 1013 | * @var: hw_params variable to apply the range |
| 1014 | * @min: the minimal value |
| 1015 | * @max: the maximal value |
| 1016 | * |
| 1017 | * Apply the min/max range constraint to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1019 | int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | unsigned int min, unsigned int max) |
| 1021 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1022 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1023 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | t.min = min; |
| 1025 | t.max = max; |
| 1026 | t.openmin = t.openmax = 0; |
| 1027 | t.integer = 0; |
| 1028 | return snd_interval_refine(constrs_interval(constrs, var), &t); |
| 1029 | } |
| 1030 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1031 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
| 1032 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1034 | struct snd_pcm_hw_constraint_list *list = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | /** |
| 1040 | * snd_pcm_hw_constraint_list |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1041 | * @runtime: PCM runtime instance |
| 1042 | * @cond: condition bits |
| 1043 | * @var: hw_params variable to apply the list constraint |
| 1044 | * @l: list |
| 1045 | * |
| 1046 | * Apply the list of constraints to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1048 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | unsigned int cond, |
| 1050 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1051 | struct snd_pcm_hw_constraint_list *l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | { |
| 1053 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1054 | snd_pcm_hw_rule_list, l, |
| 1055 | var, -1); |
| 1056 | } |
| 1057 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1058 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
| 1059 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1061 | struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | unsigned int num = 0, den = 0; |
| 1063 | int err; |
| 1064 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), |
| 1065 | r->nrats, r->rats, &num, &den); |
| 1066 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1067 | params->rate_num = num; |
| 1068 | params->rate_den = den; |
| 1069 | } |
| 1070 | return err; |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * snd_pcm_hw_constraint_ratnums |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1075 | * @runtime: PCM runtime instance |
| 1076 | * @cond: condition bits |
| 1077 | * @var: hw_params variable to apply the ratnums constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1078 | * @r: struct snd_ratnums constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1080 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | unsigned int cond, |
| 1082 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1083 | struct snd_pcm_hw_constraint_ratnums *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | { |
| 1085 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1086 | snd_pcm_hw_rule_ratnums, r, |
| 1087 | var, -1); |
| 1088 | } |
| 1089 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1090 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
| 1091 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1093 | struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | unsigned int num = 0, den = 0; |
| 1095 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), |
| 1096 | r->nrats, r->rats, &num, &den); |
| 1097 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1098 | params->rate_num = num; |
| 1099 | params->rate_den = den; |
| 1100 | } |
| 1101 | return err; |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * snd_pcm_hw_constraint_ratdens |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1106 | * @runtime: PCM runtime instance |
| 1107 | * @cond: condition bits |
| 1108 | * @var: hw_params variable to apply the ratdens constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1109 | * @r: struct snd_ratdens constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1111 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | unsigned int cond, |
| 1113 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1114 | struct snd_pcm_hw_constraint_ratdens *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1117 | snd_pcm_hw_rule_ratdens, r, |
| 1118 | var, -1); |
| 1119 | } |
| 1120 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1121 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
| 1122 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
| 1124 | unsigned int l = (unsigned long) rule->private; |
| 1125 | int width = l & 0xffff; |
| 1126 | unsigned int msbits = l >> 16; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1127 | struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | if (snd_interval_single(i) && snd_interval_value(i) == width) |
| 1129 | params->msbits = msbits; |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * snd_pcm_hw_constraint_msbits |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1135 | * @runtime: PCM runtime instance |
| 1136 | * @cond: condition bits |
| 1137 | * @width: sample bits width |
| 1138 | * @msbits: msbits width |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1140 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | unsigned int cond, |
| 1142 | unsigned int width, |
| 1143 | unsigned int msbits) |
| 1144 | { |
| 1145 | unsigned long l = (msbits << 16) | width; |
| 1146 | return snd_pcm_hw_rule_add(runtime, cond, -1, |
| 1147 | snd_pcm_hw_rule_msbits, |
| 1148 | (void*) l, |
| 1149 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1150 | } |
| 1151 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1152 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
| 1153 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | { |
| 1155 | unsigned long step = (unsigned long) rule->private; |
| 1156 | return snd_interval_step(hw_param_interval(params, rule->var), 0, step); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * snd_pcm_hw_constraint_step |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1161 | * @runtime: PCM runtime instance |
| 1162 | * @cond: condition bits |
| 1163 | * @var: hw_params variable to apply the step constraint |
| 1164 | * @step: step size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1166 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | unsigned int cond, |
| 1168 | snd_pcm_hw_param_t var, |
| 1169 | unsigned long step) |
| 1170 | { |
| 1171 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1172 | snd_pcm_hw_rule_step, (void *) step, |
| 1173 | var, -1); |
| 1174 | } |
| 1175 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1176 | static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | { |
| 1178 | static int pow2_sizes[] = { |
| 1179 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
| 1180 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, |
| 1181 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, |
| 1182 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 |
| 1183 | }; |
| 1184 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 1185 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * snd_pcm_hw_constraint_pow2 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1190 | * @runtime: PCM runtime instance |
| 1191 | * @cond: condition bits |
| 1192 | * @var: hw_params variable to apply the power-of-2 constraint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1194 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | unsigned int cond, |
| 1196 | snd_pcm_hw_param_t var) |
| 1197 | { |
| 1198 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1199 | snd_pcm_hw_rule_pow2, NULL, |
| 1200 | var, -1); |
| 1201 | } |
| 1202 | |
| 1203 | /* To use the same code we have in alsa-lib */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | #define assert(i) snd_assert((i), return -EINVAL) |
| 1205 | #ifndef INT_MIN |
| 1206 | #define INT_MIN ((int)((unsigned int)INT_MAX+1)) |
| 1207 | #endif |
| 1208 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1209 | static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1210 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
| 1212 | if (hw_is_mask(var)) { |
| 1213 | snd_mask_any(hw_param_mask(params, var)); |
| 1214 | params->cmask |= 1 << var; |
| 1215 | params->rmask |= 1 << var; |
| 1216 | return; |
| 1217 | } |
| 1218 | if (hw_is_interval(var)) { |
| 1219 | snd_interval_any(hw_param_interval(params, var)); |
| 1220 | params->cmask |= 1 << var; |
| 1221 | params->rmask |= 1 << var; |
| 1222 | return; |
| 1223 | } |
| 1224 | snd_BUG(); |
| 1225 | } |
| 1226 | |
Takashi Iwai | 6214410 | 2005-05-24 17:24:59 +0200 | [diff] [blame] | 1227 | #if 0 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1228 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | * snd_pcm_hw_param_any |
| 1230 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1231 | int snd_pcm_hw_param_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | snd_pcm_hw_param_t var) |
| 1233 | { |
| 1234 | _snd_pcm_hw_param_any(params, var); |
| 1235 | return snd_pcm_hw_refine(pcm, params); |
| 1236 | } |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1237 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1239 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { |
| 1241 | unsigned int k; |
| 1242 | memset(params, 0, sizeof(*params)); |
| 1243 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) |
| 1244 | _snd_pcm_hw_param_any(params, k); |
| 1245 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
| 1246 | _snd_pcm_hw_param_any(params, k); |
| 1247 | params->info = ~0U; |
| 1248 | } |
| 1249 | |
Takashi Iwai | 6214410 | 2005-05-24 17:24:59 +0200 | [diff] [blame] | 1250 | #if 0 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1251 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | * snd_pcm_hw_params_any |
| 1253 | * |
| 1254 | * Fill PARAMS with full configuration space boundaries |
| 1255 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1256 | int snd_pcm_hw_params_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | { |
| 1258 | _snd_pcm_hw_params_any(params); |
| 1259 | return snd_pcm_hw_refine(pcm, params); |
| 1260 | } |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1261 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | /** |
| 1264 | * snd_pcm_hw_param_value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1265 | * @params: the hw_params instance |
| 1266 | * @var: parameter to retrieve |
| 1267 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | * |
| 1269 | * Return the value for field PAR if it's fixed in configuration space |
| 1270 | * defined by PARAMS. Return -EINVAL otherwise |
| 1271 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1272 | static int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1273 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { |
| 1275 | if (hw_is_mask(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1276 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | if (!snd_mask_single(mask)) |
| 1278 | return -EINVAL; |
| 1279 | if (dir) |
| 1280 | *dir = 0; |
| 1281 | return snd_mask_value(mask); |
| 1282 | } |
| 1283 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1284 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | if (!snd_interval_single(i)) |
| 1286 | return -EINVAL; |
| 1287 | if (dir) |
| 1288 | *dir = i->openmin; |
| 1289 | return snd_interval_value(i); |
| 1290 | } |
| 1291 | assert(0); |
| 1292 | return -EINVAL; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * snd_pcm_hw_param_value_min |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1297 | * @params: the hw_params instance |
| 1298 | * @var: parameter to retrieve |
| 1299 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | * |
| 1301 | * Return the minimum value for field PAR. |
| 1302 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1303 | unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | snd_pcm_hw_param_t var, int *dir) |
| 1305 | { |
| 1306 | if (hw_is_mask(var)) { |
| 1307 | if (dir) |
| 1308 | *dir = 0; |
| 1309 | return snd_mask_min(hw_param_mask_c(params, var)); |
| 1310 | } |
| 1311 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1312 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (dir) |
| 1314 | *dir = i->openmin; |
| 1315 | return snd_interval_min(i); |
| 1316 | } |
| 1317 | assert(0); |
| 1318 | return -EINVAL; |
| 1319 | } |
| 1320 | |
| 1321 | /** |
| 1322 | * snd_pcm_hw_param_value_max |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1323 | * @params: the hw_params instance |
| 1324 | * @var: parameter to retrieve |
| 1325 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | * |
| 1327 | * Return the maximum value for field PAR. |
| 1328 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1329 | unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | snd_pcm_hw_param_t var, int *dir) |
| 1331 | { |
| 1332 | if (hw_is_mask(var)) { |
| 1333 | if (dir) |
| 1334 | *dir = 0; |
| 1335 | return snd_mask_max(hw_param_mask_c(params, var)); |
| 1336 | } |
| 1337 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1338 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | if (dir) |
| 1340 | *dir = - (int) i->openmax; |
| 1341 | return snd_interval_max(i); |
| 1342 | } |
| 1343 | assert(0); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1347 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | snd_pcm_hw_param_t var) |
| 1349 | { |
| 1350 | if (hw_is_mask(var)) { |
| 1351 | snd_mask_none(hw_param_mask(params, var)); |
| 1352 | params->cmask |= 1 << var; |
| 1353 | params->rmask |= 1 << var; |
| 1354 | } else if (hw_is_interval(var)) { |
| 1355 | snd_interval_none(hw_param_interval(params, var)); |
| 1356 | params->cmask |= 1 << var; |
| 1357 | params->rmask |= 1 << var; |
| 1358 | } else { |
| 1359 | snd_BUG(); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1363 | int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | snd_pcm_hw_param_t var) |
| 1365 | { |
| 1366 | int changed; |
| 1367 | assert(hw_is_interval(var)); |
| 1368 | changed = snd_interval_setinteger(hw_param_interval(params, var)); |
| 1369 | if (changed) { |
| 1370 | params->cmask |= 1 << var; |
| 1371 | params->rmask |= 1 << var; |
| 1372 | } |
| 1373 | return changed; |
| 1374 | } |
| 1375 | |
Takashi Iwai | 6214410 | 2005-05-24 17:24:59 +0200 | [diff] [blame] | 1376 | #if 0 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1377 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | * snd_pcm_hw_param_setinteger |
| 1379 | * |
| 1380 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1381 | * non integer values. Reduce configuration space accordingly. |
| 1382 | * Return -EINVAL if the configuration space is empty |
| 1383 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1384 | int snd_pcm_hw_param_setinteger(struct snd_pcm_substream *pcm, |
| 1385 | struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | snd_pcm_hw_param_t var) |
| 1387 | { |
| 1388 | int changed = _snd_pcm_hw_param_setinteger(params, var); |
| 1389 | if (changed < 0) |
| 1390 | return changed; |
| 1391 | if (params->rmask) { |
| 1392 | int err = snd_pcm_hw_refine(pcm, params); |
| 1393 | if (err < 0) |
| 1394 | return err; |
| 1395 | } |
| 1396 | return 0; |
| 1397 | } |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1398 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1400 | static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1401 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | { |
| 1403 | int changed; |
| 1404 | if (hw_is_mask(var)) |
| 1405 | changed = snd_mask_refine_first(hw_param_mask(params, var)); |
| 1406 | else if (hw_is_interval(var)) |
| 1407 | changed = snd_interval_refine_first(hw_param_interval(params, var)); |
| 1408 | else { |
| 1409 | assert(0); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | if (changed) { |
| 1413 | params->cmask |= 1 << var; |
| 1414 | params->rmask |= 1 << var; |
| 1415 | } |
| 1416 | return changed; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /** |
| 1421 | * snd_pcm_hw_param_first |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1422 | * @pcm: PCM instance |
| 1423 | * @params: the hw_params instance |
| 1424 | * @var: parameter to retrieve |
| 1425 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | * |
| 1427 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1428 | * values > minimum. Reduce configuration space accordingly. |
| 1429 | * Return the minimum. |
| 1430 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1431 | static int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
| 1432 | struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1433 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | { |
| 1435 | int changed = _snd_pcm_hw_param_first(params, var); |
| 1436 | if (changed < 0) |
| 1437 | return changed; |
| 1438 | if (params->rmask) { |
| 1439 | int err = snd_pcm_hw_refine(pcm, params); |
| 1440 | assert(err >= 0); |
| 1441 | } |
| 1442 | return snd_pcm_hw_param_value(params, var, dir); |
| 1443 | } |
| 1444 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1445 | static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1446 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | { |
| 1448 | int changed; |
| 1449 | if (hw_is_mask(var)) |
| 1450 | changed = snd_mask_refine_last(hw_param_mask(params, var)); |
| 1451 | else if (hw_is_interval(var)) |
| 1452 | changed = snd_interval_refine_last(hw_param_interval(params, var)); |
| 1453 | else { |
| 1454 | assert(0); |
| 1455 | return -EINVAL; |
| 1456 | } |
| 1457 | if (changed) { |
| 1458 | params->cmask |= 1 << var; |
| 1459 | params->rmask |= 1 << var; |
| 1460 | } |
| 1461 | return changed; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | /** |
| 1466 | * snd_pcm_hw_param_last |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1467 | * @pcm: PCM instance |
| 1468 | * @params: the hw_params instance |
| 1469 | * @var: parameter to retrieve |
| 1470 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | * |
| 1472 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1473 | * values < maximum. Reduce configuration space accordingly. |
| 1474 | * Return the maximum. |
| 1475 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1476 | static int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
| 1477 | struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1478 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | { |
| 1480 | int changed = _snd_pcm_hw_param_last(params, var); |
| 1481 | if (changed < 0) |
| 1482 | return changed; |
| 1483 | if (params->rmask) { |
| 1484 | int err = snd_pcm_hw_refine(pcm, params); |
| 1485 | assert(err >= 0); |
| 1486 | } |
| 1487 | return snd_pcm_hw_param_value(params, var, dir); |
| 1488 | } |
| 1489 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1490 | int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | snd_pcm_hw_param_t var, unsigned int val, int dir) |
| 1492 | { |
| 1493 | int changed; |
| 1494 | int open = 0; |
| 1495 | if (dir) { |
| 1496 | if (dir > 0) { |
| 1497 | open = 1; |
| 1498 | } else if (dir < 0) { |
| 1499 | if (val > 0) { |
| 1500 | open = 1; |
| 1501 | val--; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | if (hw_is_mask(var)) |
| 1506 | changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open); |
| 1507 | else if (hw_is_interval(var)) |
| 1508 | changed = snd_interval_refine_min(hw_param_interval(params, var), val, open); |
| 1509 | else { |
| 1510 | assert(0); |
| 1511 | return -EINVAL; |
| 1512 | } |
| 1513 | if (changed) { |
| 1514 | params->cmask |= 1 << var; |
| 1515 | params->rmask |= 1 << var; |
| 1516 | } |
| 1517 | return changed; |
| 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * snd_pcm_hw_param_min |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1522 | * @pcm: PCM instance |
| 1523 | * @params: the hw_params instance |
| 1524 | * @var: parameter to retrieve |
| 1525 | * @val: minimal value |
| 1526 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | * |
| 1528 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1529 | * values < VAL. Reduce configuration space accordingly. |
| 1530 | * Return new minimum or -EINVAL if the configuration space is empty |
| 1531 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1532 | static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1533 | snd_pcm_hw_param_t var, unsigned int val, |
| 1534 | int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | { |
| 1536 | int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0); |
| 1537 | if (changed < 0) |
| 1538 | return changed; |
| 1539 | if (params->rmask) { |
| 1540 | int err = snd_pcm_hw_refine(pcm, params); |
| 1541 | if (err < 0) |
| 1542 | return err; |
| 1543 | } |
| 1544 | return snd_pcm_hw_param_value_min(params, var, dir); |
| 1545 | } |
| 1546 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1547 | static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1548 | snd_pcm_hw_param_t var, unsigned int val, |
| 1549 | int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
| 1551 | int changed; |
| 1552 | int open = 0; |
| 1553 | if (dir) { |
| 1554 | if (dir < 0) { |
| 1555 | open = 1; |
| 1556 | } else if (dir > 0) { |
| 1557 | open = 1; |
| 1558 | val++; |
| 1559 | } |
| 1560 | } |
| 1561 | if (hw_is_mask(var)) { |
| 1562 | if (val == 0 && open) { |
| 1563 | snd_mask_none(hw_param_mask(params, var)); |
| 1564 | changed = -EINVAL; |
| 1565 | } else |
| 1566 | changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open); |
| 1567 | } else if (hw_is_interval(var)) |
| 1568 | changed = snd_interval_refine_max(hw_param_interval(params, var), val, open); |
| 1569 | else { |
| 1570 | assert(0); |
| 1571 | return -EINVAL; |
| 1572 | } |
| 1573 | if (changed) { |
| 1574 | params->cmask |= 1 << var; |
| 1575 | params->rmask |= 1 << var; |
| 1576 | } |
| 1577 | return changed; |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * snd_pcm_hw_param_max |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1582 | * @pcm: PCM instance |
| 1583 | * @params: the hw_params instance |
| 1584 | * @var: parameter to retrieve |
| 1585 | * @val: maximal value |
| 1586 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | * |
| 1588 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1589 | * values >= VAL + 1. Reduce configuration space accordingly. |
| 1590 | * Return new maximum or -EINVAL if the configuration space is empty |
| 1591 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1592 | static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
Adrian Bunk | 123992f | 2005-05-18 18:02:04 +0200 | [diff] [blame] | 1593 | snd_pcm_hw_param_t var, unsigned int val, |
| 1594 | int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | { |
| 1596 | int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0); |
| 1597 | if (changed < 0) |
| 1598 | return changed; |
| 1599 | if (params->rmask) { |
| 1600 | int err = snd_pcm_hw_refine(pcm, params); |
| 1601 | if (err < 0) |
| 1602 | return err; |
| 1603 | } |
| 1604 | return snd_pcm_hw_param_value_max(params, var, dir); |
| 1605 | } |
| 1606 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1607 | int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | snd_pcm_hw_param_t var, unsigned int val, int dir) |
| 1609 | { |
| 1610 | int changed; |
| 1611 | if (hw_is_mask(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1612 | struct snd_mask *m = hw_param_mask(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | if (val == 0 && dir < 0) { |
| 1614 | changed = -EINVAL; |
| 1615 | snd_mask_none(m); |
| 1616 | } else { |
| 1617 | if (dir > 0) |
| 1618 | val++; |
| 1619 | else if (dir < 0) |
| 1620 | val--; |
| 1621 | changed = snd_mask_refine_set(hw_param_mask(params, var), val); |
| 1622 | } |
| 1623 | } else if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1624 | struct snd_interval *i = hw_param_interval(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | if (val == 0 && dir < 0) { |
| 1626 | changed = -EINVAL; |
| 1627 | snd_interval_none(i); |
| 1628 | } else if (dir == 0) |
| 1629 | changed = snd_interval_refine_set(i, val); |
| 1630 | else { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1631 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | t.openmin = 1; |
| 1633 | t.openmax = 1; |
| 1634 | t.empty = 0; |
| 1635 | t.integer = 0; |
| 1636 | if (dir < 0) { |
| 1637 | t.min = val - 1; |
| 1638 | t.max = val; |
| 1639 | } else { |
| 1640 | t.min = val; |
| 1641 | t.max = val+1; |
| 1642 | } |
| 1643 | changed = snd_interval_refine(i, &t); |
| 1644 | } |
| 1645 | } else { |
| 1646 | assert(0); |
| 1647 | return -EINVAL; |
| 1648 | } |
| 1649 | if (changed) { |
| 1650 | params->cmask |= 1 << var; |
| 1651 | params->rmask |= 1 << var; |
| 1652 | } |
| 1653 | return changed; |
| 1654 | } |
| 1655 | |
| 1656 | /** |
| 1657 | * snd_pcm_hw_param_set |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1658 | * @pcm: PCM instance |
| 1659 | * @params: the hw_params instance |
| 1660 | * @var: parameter to retrieve |
| 1661 | * @val: value to set |
| 1662 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | * |
| 1664 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1665 | * values != VAL. Reduce configuration space accordingly. |
| 1666 | * Return VAL or -EINVAL if the configuration space is empty |
| 1667 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1668 | int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | snd_pcm_hw_param_t var, unsigned int val, int dir) |
| 1670 | { |
| 1671 | int changed = _snd_pcm_hw_param_set(params, var, val, dir); |
| 1672 | if (changed < 0) |
| 1673 | return changed; |
| 1674 | if (params->rmask) { |
| 1675 | int err = snd_pcm_hw_refine(pcm, params); |
| 1676 | if (err < 0) |
| 1677 | return err; |
| 1678 | } |
| 1679 | return snd_pcm_hw_param_value(params, var, NULL); |
| 1680 | } |
| 1681 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1682 | static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params, |
| 1683 | snd_pcm_hw_param_t var, const struct snd_mask *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | { |
| 1685 | int changed; |
| 1686 | assert(hw_is_mask(var)); |
| 1687 | changed = snd_mask_refine(hw_param_mask(params, var), val); |
| 1688 | if (changed) { |
| 1689 | params->cmask |= 1 << var; |
| 1690 | params->rmask |= 1 << var; |
| 1691 | } |
| 1692 | return changed; |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * snd_pcm_hw_param_mask |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1697 | * @pcm: PCM instance |
| 1698 | * @params: the hw_params instance |
| 1699 | * @var: parameter to retrieve |
| 1700 | * @val: mask to apply |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | * |
| 1702 | * Inside configuration space defined by PARAMS remove from PAR all values |
| 1703 | * not contained in MASK. Reduce configuration space accordingly. |
| 1704 | * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS, |
| 1705 | * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT. |
| 1706 | * Return 0 on success or -EINVAL |
| 1707 | * if the configuration space is empty |
| 1708 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1709 | int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
| 1710 | snd_pcm_hw_param_t var, const struct snd_mask *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | { |
| 1712 | int changed = _snd_pcm_hw_param_mask(params, var, val); |
| 1713 | if (changed < 0) |
| 1714 | return changed; |
| 1715 | if (params->rmask) { |
| 1716 | int err = snd_pcm_hw_refine(pcm, params); |
| 1717 | if (err < 0) |
| 1718 | return err; |
| 1719 | } |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | static int boundary_sub(int a, int adir, |
| 1724 | int b, int bdir, |
| 1725 | int *c, int *cdir) |
| 1726 | { |
| 1727 | adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0); |
| 1728 | bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0); |
| 1729 | *c = a - b; |
| 1730 | *cdir = adir - bdir; |
| 1731 | if (*cdir == -2) { |
| 1732 | assert(*c > INT_MIN); |
| 1733 | (*c)--; |
| 1734 | } else if (*cdir == 2) { |
| 1735 | assert(*c < INT_MAX); |
| 1736 | (*c)++; |
| 1737 | } |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | static int boundary_lt(unsigned int a, int adir, |
| 1742 | unsigned int b, int bdir) |
| 1743 | { |
| 1744 | assert(a > 0 || adir >= 0); |
| 1745 | assert(b > 0 || bdir >= 0); |
| 1746 | if (adir < 0) { |
| 1747 | a--; |
| 1748 | adir = 1; |
| 1749 | } else if (adir > 0) |
| 1750 | adir = 1; |
| 1751 | if (bdir < 0) { |
| 1752 | b--; |
| 1753 | bdir = 1; |
| 1754 | } else if (bdir > 0) |
| 1755 | bdir = 1; |
| 1756 | return a < b || (a == b && adir < bdir); |
| 1757 | } |
| 1758 | |
| 1759 | /* Return 1 if min is nearer to best than max */ |
| 1760 | static int boundary_nearer(int min, int mindir, |
| 1761 | int best, int bestdir, |
| 1762 | int max, int maxdir) |
| 1763 | { |
| 1764 | int dmin, dmindir; |
| 1765 | int dmax, dmaxdir; |
| 1766 | boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir); |
| 1767 | boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir); |
| 1768 | return boundary_lt(dmin, dmindir, dmax, dmaxdir); |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * snd_pcm_hw_param_near |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1773 | * @pcm: PCM instance |
| 1774 | * @params: the hw_params instance |
| 1775 | * @var: parameter to retrieve |
| 1776 | * @best: value to set |
| 1777 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | * |
| 1779 | * Inside configuration space defined by PARAMS set PAR to the available value |
| 1780 | * nearest to VAL. Reduce configuration space accordingly. |
| 1781 | * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS, |
| 1782 | * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT. |
| 1783 | * Return the value found. |
| 1784 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1785 | int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | snd_pcm_hw_param_t var, unsigned int best, int *dir) |
| 1787 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1788 | struct snd_pcm_hw_params *save = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | int v; |
| 1790 | unsigned int saved_min; |
| 1791 | int last = 0; |
| 1792 | int min, max; |
| 1793 | int mindir, maxdir; |
| 1794 | int valdir = dir ? *dir : 0; |
| 1795 | /* FIXME */ |
| 1796 | if (best > INT_MAX) |
| 1797 | best = INT_MAX; |
| 1798 | min = max = best; |
| 1799 | mindir = maxdir = valdir; |
| 1800 | if (maxdir > 0) |
| 1801 | maxdir = 0; |
| 1802 | else if (maxdir == 0) |
| 1803 | maxdir = -1; |
| 1804 | else { |
| 1805 | maxdir = 1; |
| 1806 | max--; |
| 1807 | } |
| 1808 | save = kmalloc(sizeof(*save), GFP_KERNEL); |
| 1809 | if (save == NULL) |
| 1810 | return -ENOMEM; |
| 1811 | *save = *params; |
| 1812 | saved_min = min; |
| 1813 | min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir); |
| 1814 | if (min >= 0) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1815 | struct snd_pcm_hw_params *params1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | if (max < 0) |
| 1817 | goto _end; |
| 1818 | if ((unsigned int)min == saved_min && mindir == valdir) |
| 1819 | goto _end; |
| 1820 | params1 = kmalloc(sizeof(*params1), GFP_KERNEL); |
| 1821 | if (params1 == NULL) { |
| 1822 | kfree(save); |
| 1823 | return -ENOMEM; |
| 1824 | } |
| 1825 | *params1 = *save; |
| 1826 | max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir); |
| 1827 | if (max < 0) { |
| 1828 | kfree(params1); |
| 1829 | goto _end; |
| 1830 | } |
| 1831 | if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) { |
| 1832 | *params = *params1; |
| 1833 | last = 1; |
| 1834 | } |
| 1835 | kfree(params1); |
| 1836 | } else { |
| 1837 | *params = *save; |
| 1838 | max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir); |
| 1839 | assert(max >= 0); |
| 1840 | last = 1; |
| 1841 | } |
| 1842 | _end: |
| 1843 | kfree(save); |
| 1844 | if (last) |
| 1845 | v = snd_pcm_hw_param_last(pcm, params, var, dir); |
| 1846 | else |
| 1847 | v = snd_pcm_hw_param_first(pcm, params, var, dir); |
| 1848 | assert(v >= 0); |
| 1849 | return v; |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * snd_pcm_hw_param_choose |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1854 | * @pcm: PCM instance |
| 1855 | * @params: the hw_params instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | * |
| 1857 | * Choose one configuration from configuration space defined by PARAMS |
| 1858 | * The configuration chosen is that obtained fixing in this order: |
| 1859 | * first access, first format, first subformat, min channels, |
| 1860 | * min rate, min period time, max buffer size, min tick time |
| 1861 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1862 | int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | { |
| 1864 | int err; |
| 1865 | |
| 1866 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_ACCESS, NULL); |
| 1867 | assert(err >= 0); |
| 1868 | |
| 1869 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_FORMAT, NULL); |
| 1870 | assert(err >= 0); |
| 1871 | |
| 1872 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_SUBFORMAT, NULL); |
| 1873 | assert(err >= 0); |
| 1874 | |
| 1875 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_CHANNELS, NULL); |
| 1876 | assert(err >= 0); |
| 1877 | |
| 1878 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_RATE, NULL); |
| 1879 | assert(err >= 0); |
| 1880 | |
| 1881 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, NULL); |
| 1882 | assert(err >= 0); |
| 1883 | |
| 1884 | err = snd_pcm_hw_param_last(pcm, params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL); |
| 1885 | assert(err >= 0); |
| 1886 | |
| 1887 | err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_TICK_TIME, NULL); |
| 1888 | assert(err >= 0); |
| 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | #undef assert |
| 1894 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1895 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | void *arg) |
| 1897 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1898 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | unsigned long flags; |
| 1900 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1901 | if (snd_pcm_running(substream) && |
| 1902 | snd_pcm_update_hw_ptr(substream) >= 0) |
| 1903 | runtime->status->hw_ptr %= runtime->buffer_size; |
| 1904 | else |
| 1905 | runtime->status->hw_ptr = 0; |
| 1906 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1910 | static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | void *arg) |
| 1912 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1913 | struct snd_pcm_channel_info *info = arg; |
| 1914 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | int width; |
| 1916 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { |
| 1917 | info->offset = -1; |
| 1918 | return 0; |
| 1919 | } |
| 1920 | width = snd_pcm_format_physical_width(runtime->format); |
| 1921 | if (width < 0) |
| 1922 | return width; |
| 1923 | info->offset = 0; |
| 1924 | switch (runtime->access) { |
| 1925 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: |
| 1926 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: |
| 1927 | info->first = info->channel * width; |
| 1928 | info->step = runtime->channels * width; |
| 1929 | break; |
| 1930 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: |
| 1931 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: |
| 1932 | { |
| 1933 | size_t size = runtime->dma_bytes / runtime->channels; |
| 1934 | info->first = info->channel * size * 8; |
| 1935 | info->step = width; |
| 1936 | break; |
| 1937 | } |
| 1938 | default: |
| 1939 | snd_BUG(); |
| 1940 | break; |
| 1941 | } |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback |
| 1947 | * @substream: the pcm substream instance |
| 1948 | * @cmd: ioctl command |
| 1949 | * @arg: ioctl argument |
| 1950 | * |
| 1951 | * Processes the generic ioctl commands for PCM. |
| 1952 | * Can be passed as the ioctl callback for PCM ops. |
| 1953 | * |
| 1954 | * Returns zero if successful, or a negative error code on failure. |
| 1955 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1956 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | unsigned int cmd, void *arg) |
| 1958 | { |
| 1959 | switch (cmd) { |
| 1960 | case SNDRV_PCM_IOCTL1_INFO: |
| 1961 | return 0; |
| 1962 | case SNDRV_PCM_IOCTL1_RESET: |
| 1963 | return snd_pcm_lib_ioctl_reset(substream, arg); |
| 1964 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: |
| 1965 | return snd_pcm_lib_ioctl_channel_info(substream, arg); |
| 1966 | } |
| 1967 | return -ENXIO; |
| 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * Conditions |
| 1972 | */ |
| 1973 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1974 | static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | unsigned long ticks) |
| 1976 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1977 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | if (ticks == 0) |
| 1979 | del_timer(&runtime->tick_timer); |
| 1980 | else { |
| 1981 | ticks += (1000000 / HZ) - 1; |
| 1982 | ticks /= (1000000 / HZ); |
| 1983 | mod_timer(&runtime->tick_timer, jiffies + ticks); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* Temporary alias */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1988 | void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | { |
| 1990 | snd_pcm_system_tick_set(substream, ticks); |
| 1991 | } |
| 1992 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1993 | void snd_pcm_tick_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1995 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | snd_pcm_uframes_t frames = ULONG_MAX; |
| 1997 | snd_pcm_uframes_t avail, dist; |
| 1998 | unsigned int ticks; |
| 1999 | u_int64_t n; |
| 2000 | u_int32_t r; |
| 2001 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 2002 | if (runtime->silence_size >= runtime->boundary) { |
| 2003 | frames = 1; |
| 2004 | } else if (runtime->silence_size > 0 && |
| 2005 | runtime->silence_filled < runtime->buffer_size) { |
| 2006 | snd_pcm_sframes_t noise_dist; |
| 2007 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
Takashi Iwai | 235475c | 2005-12-07 15:28:07 +0100 | [diff] [blame] | 2008 | if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold) |
| 2009 | frames = noise_dist - runtime->silence_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } |
| 2011 | avail = snd_pcm_playback_avail(runtime); |
| 2012 | } else { |
| 2013 | avail = snd_pcm_capture_avail(runtime); |
| 2014 | } |
| 2015 | if (avail < runtime->control->avail_min) { |
| 2016 | snd_pcm_sframes_t n = runtime->control->avail_min - avail; |
| 2017 | if (n > 0 && frames > (snd_pcm_uframes_t)n) |
| 2018 | frames = n; |
| 2019 | } |
| 2020 | if (avail < runtime->buffer_size) { |
| 2021 | snd_pcm_sframes_t n = runtime->buffer_size - avail; |
| 2022 | if (n > 0 && frames > (snd_pcm_uframes_t)n) |
| 2023 | frames = n; |
| 2024 | } |
| 2025 | if (frames == ULONG_MAX) { |
| 2026 | snd_pcm_tick_set(substream, 0); |
| 2027 | return; |
| 2028 | } |
| 2029 | dist = runtime->status->hw_ptr - runtime->hw_ptr_base; |
| 2030 | /* Distance to next interrupt */ |
| 2031 | dist = runtime->period_size - dist % runtime->period_size; |
| 2032 | if (dist <= frames) { |
| 2033 | snd_pcm_tick_set(substream, 0); |
| 2034 | return; |
| 2035 | } |
| 2036 | /* the base time is us */ |
| 2037 | n = frames; |
| 2038 | n *= 1000000; |
| 2039 | div64_32(&n, runtime->tick_time * runtime->rate, &r); |
| 2040 | ticks = n + (r > 0 ? 1 : 0); |
| 2041 | if (ticks < runtime->sleep_min) |
| 2042 | ticks = runtime->sleep_min; |
| 2043 | snd_pcm_tick_set(substream, (unsigned long) ticks); |
| 2044 | } |
| 2045 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2046 | void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2048 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | unsigned long flags; |
| 2050 | |
| 2051 | snd_assert(substream != NULL, return); |
| 2052 | runtime = substream->runtime; |
| 2053 | snd_assert(runtime != NULL, return); |
| 2054 | |
| 2055 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 2056 | if (!snd_pcm_running(substream) || |
| 2057 | snd_pcm_update_hw_ptr(substream) < 0) |
| 2058 | goto _end; |
| 2059 | if (runtime->sleep_min) |
| 2060 | snd_pcm_tick_prepare(substream); |
| 2061 | _end: |
| 2062 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 2063 | } |
| 2064 | |
| 2065 | /** |
| 2066 | * snd_pcm_period_elapsed - update the pcm status for the next period |
| 2067 | * @substream: the pcm substream instance |
| 2068 | * |
| 2069 | * This function is called from the interrupt handler when the |
| 2070 | * PCM has processed the period size. It will update the current |
| 2071 | * pointer, set up the tick, wake up sleepers, etc. |
| 2072 | * |
| 2073 | * Even if more than one periods have elapsed since the last call, you |
| 2074 | * have to call this only once. |
| 2075 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2076 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2078 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | unsigned long flags; |
| 2080 | |
| 2081 | snd_assert(substream != NULL, return); |
| 2082 | runtime = substream->runtime; |
| 2083 | snd_assert(runtime != NULL, return); |
| 2084 | |
| 2085 | if (runtime->transfer_ack_begin) |
| 2086 | runtime->transfer_ack_begin(substream); |
| 2087 | |
| 2088 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 2089 | if (!snd_pcm_running(substream) || |
| 2090 | snd_pcm_update_hw_ptr_interrupt(substream) < 0) |
| 2091 | goto _end; |
| 2092 | |
| 2093 | if (substream->timer_running) |
| 2094 | snd_timer_interrupt(substream->timer, 1); |
| 2095 | if (runtime->sleep_min) |
| 2096 | snd_pcm_tick_prepare(substream); |
| 2097 | _end: |
| 2098 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 2099 | if (runtime->transfer_ack_end) |
| 2100 | runtime->transfer_ack_end(substream); |
| 2101 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); |
| 2102 | } |
| 2103 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2104 | static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | unsigned int hwoff, |
| 2106 | unsigned long data, unsigned int off, |
| 2107 | snd_pcm_uframes_t frames) |
| 2108 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2109 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | int err; |
| 2111 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 2112 | if (substream->ops->copy) { |
| 2113 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 2114 | return err; |
| 2115 | } else { |
| 2116 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 2117 | snd_assert(runtime->dma_area, return -EFAULT); |
| 2118 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) |
| 2119 | return -EFAULT; |
| 2120 | } |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2124 | typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | unsigned long data, unsigned int off, |
| 2126 | snd_pcm_uframes_t size); |
| 2127 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2128 | static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | unsigned long data, |
| 2130 | snd_pcm_uframes_t size, |
| 2131 | int nonblock, |
| 2132 | transfer_f transfer) |
| 2133 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2134 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | snd_pcm_uframes_t xfer = 0; |
| 2136 | snd_pcm_uframes_t offset = 0; |
| 2137 | int err = 0; |
| 2138 | |
| 2139 | if (size == 0) |
| 2140 | return 0; |
| 2141 | if (size > runtime->xfer_align) |
| 2142 | size -= size % runtime->xfer_align; |
| 2143 | |
| 2144 | snd_pcm_stream_lock_irq(substream); |
| 2145 | switch (runtime->status->state) { |
| 2146 | case SNDRV_PCM_STATE_PREPARED: |
| 2147 | case SNDRV_PCM_STATE_RUNNING: |
| 2148 | case SNDRV_PCM_STATE_PAUSED: |
| 2149 | break; |
| 2150 | case SNDRV_PCM_STATE_XRUN: |
| 2151 | err = -EPIPE; |
| 2152 | goto _end_unlock; |
| 2153 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2154 | err = -ESTRPIPE; |
| 2155 | goto _end_unlock; |
| 2156 | default: |
| 2157 | err = -EBADFD; |
| 2158 | goto _end_unlock; |
| 2159 | } |
| 2160 | |
| 2161 | while (size > 0) { |
| 2162 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 2163 | snd_pcm_uframes_t avail; |
| 2164 | snd_pcm_uframes_t cont; |
| 2165 | if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2166 | snd_pcm_update_hw_ptr(substream); |
| 2167 | avail = snd_pcm_playback_avail(runtime); |
| 2168 | if (((avail < runtime->control->avail_min && size > avail) || |
| 2169 | (size >= runtime->xfer_align && avail < runtime->xfer_align))) { |
| 2170 | wait_queue_t wait; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2171 | enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | long tout; |
| 2173 | |
| 2174 | if (nonblock) { |
| 2175 | err = -EAGAIN; |
| 2176 | goto _end_unlock; |
| 2177 | } |
| 2178 | |
| 2179 | init_waitqueue_entry(&wait, current); |
| 2180 | add_wait_queue(&runtime->sleep, &wait); |
| 2181 | while (1) { |
| 2182 | if (signal_pending(current)) { |
| 2183 | state = SIGNALED; |
| 2184 | break; |
| 2185 | } |
| 2186 | set_current_state(TASK_INTERRUPTIBLE); |
| 2187 | snd_pcm_stream_unlock_irq(substream); |
| 2188 | tout = schedule_timeout(10 * HZ); |
| 2189 | snd_pcm_stream_lock_irq(substream); |
| 2190 | if (tout == 0) { |
| 2191 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED && |
| 2192 | runtime->status->state != SNDRV_PCM_STATE_PAUSED) { |
| 2193 | state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED; |
| 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | switch (runtime->status->state) { |
| 2198 | case SNDRV_PCM_STATE_XRUN: |
| 2199 | case SNDRV_PCM_STATE_DRAINING: |
| 2200 | state = ERROR; |
| 2201 | goto _end_loop; |
| 2202 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2203 | state = SUSPENDED; |
| 2204 | goto _end_loop; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2205 | case SNDRV_PCM_STATE_SETUP: |
| 2206 | state = DROPPED; |
| 2207 | goto _end_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | default: |
| 2209 | break; |
| 2210 | } |
| 2211 | avail = snd_pcm_playback_avail(runtime); |
| 2212 | if (avail >= runtime->control->avail_min) { |
| 2213 | state = READY; |
| 2214 | break; |
| 2215 | } |
| 2216 | } |
| 2217 | _end_loop: |
| 2218 | remove_wait_queue(&runtime->sleep, &wait); |
| 2219 | |
| 2220 | switch (state) { |
| 2221 | case ERROR: |
| 2222 | err = -EPIPE; |
| 2223 | goto _end_unlock; |
| 2224 | case SUSPENDED: |
| 2225 | err = -ESTRPIPE; |
| 2226 | goto _end_unlock; |
| 2227 | case SIGNALED: |
| 2228 | err = -ERESTARTSYS; |
| 2229 | goto _end_unlock; |
| 2230 | case EXPIRED: |
| 2231 | snd_printd("playback write error (DMA or IRQ trouble?)\n"); |
| 2232 | err = -EIO; |
| 2233 | goto _end_unlock; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2234 | case DROPPED: |
| 2235 | err = -EBADFD; |
| 2236 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | default: |
| 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | if (avail > runtime->xfer_align) |
| 2242 | avail -= avail % runtime->xfer_align; |
| 2243 | frames = size > avail ? avail : size; |
| 2244 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 2245 | if (frames > cont) |
| 2246 | frames = cont; |
| 2247 | snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL); |
| 2248 | appl_ptr = runtime->control->appl_ptr; |
| 2249 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 2250 | snd_pcm_stream_unlock_irq(substream); |
| 2251 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 2252 | goto _end; |
| 2253 | snd_pcm_stream_lock_irq(substream); |
| 2254 | switch (runtime->status->state) { |
| 2255 | case SNDRV_PCM_STATE_XRUN: |
| 2256 | err = -EPIPE; |
| 2257 | goto _end_unlock; |
| 2258 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2259 | err = -ESTRPIPE; |
| 2260 | goto _end_unlock; |
| 2261 | default: |
| 2262 | break; |
| 2263 | } |
| 2264 | appl_ptr += frames; |
| 2265 | if (appl_ptr >= runtime->boundary) |
| 2266 | appl_ptr -= runtime->boundary; |
| 2267 | runtime->control->appl_ptr = appl_ptr; |
| 2268 | if (substream->ops->ack) |
| 2269 | substream->ops->ack(substream); |
| 2270 | |
| 2271 | offset += frames; |
| 2272 | size -= frames; |
| 2273 | xfer += frames; |
| 2274 | if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && |
| 2275 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { |
| 2276 | err = snd_pcm_start(substream); |
| 2277 | if (err < 0) |
| 2278 | goto _end_unlock; |
| 2279 | } |
| 2280 | if (runtime->sleep_min && |
| 2281 | runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2282 | snd_pcm_tick_prepare(substream); |
| 2283 | } |
| 2284 | _end_unlock: |
| 2285 | snd_pcm_stream_unlock_irq(substream); |
| 2286 | _end: |
| 2287 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 2288 | } |
| 2289 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2290 | snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2292 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | int nonblock; |
| 2294 | |
| 2295 | snd_assert(substream != NULL, return -ENXIO); |
| 2296 | runtime = substream->runtime; |
| 2297 | snd_assert(runtime != NULL, return -ENXIO); |
| 2298 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2299 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2300 | return -EBADFD; |
| 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | |
| 2304 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && |
| 2305 | runtime->channels > 1) |
| 2306 | return -EINVAL; |
| 2307 | return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock, |
| 2308 | snd_pcm_lib_write_transfer); |
| 2309 | } |
| 2310 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2311 | static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | unsigned int hwoff, |
| 2313 | unsigned long data, unsigned int off, |
| 2314 | snd_pcm_uframes_t frames) |
| 2315 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2316 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | int err; |
| 2318 | void __user **bufs = (void __user **)data; |
| 2319 | int channels = runtime->channels; |
| 2320 | int c; |
| 2321 | if (substream->ops->copy) { |
| 2322 | snd_assert(substream->ops->silence != NULL, return -EINVAL); |
| 2323 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2324 | if (*bufs == NULL) { |
| 2325 | if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0) |
| 2326 | return err; |
| 2327 | } else { |
| 2328 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 2329 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2330 | return err; |
| 2331 | } |
| 2332 | } |
| 2333 | } else { |
| 2334 | /* default transfer behaviour */ |
| 2335 | size_t dma_csize = runtime->dma_bytes / channels; |
| 2336 | snd_assert(runtime->dma_area, return -EFAULT); |
| 2337 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2338 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2339 | if (*bufs == NULL) { |
| 2340 | snd_pcm_format_set_silence(runtime->format, hwbuf, frames); |
| 2341 | } else { |
| 2342 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 2343 | if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames))) |
| 2344 | return -EFAULT; |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | return 0; |
| 2349 | } |
| 2350 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2351 | snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | void __user **bufs, |
| 2353 | snd_pcm_uframes_t frames) |
| 2354 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2355 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | int nonblock; |
| 2357 | |
| 2358 | snd_assert(substream != NULL, return -ENXIO); |
| 2359 | runtime = substream->runtime; |
| 2360 | snd_assert(runtime != NULL, return -ENXIO); |
| 2361 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2362 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2363 | return -EBADFD; |
| 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | |
| 2367 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2368 | return -EINVAL; |
| 2369 | return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames, |
| 2370 | nonblock, snd_pcm_lib_writev_transfer); |
| 2371 | } |
| 2372 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2373 | static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | unsigned int hwoff, |
| 2375 | unsigned long data, unsigned int off, |
| 2376 | snd_pcm_uframes_t frames) |
| 2377 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2378 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | int err; |
| 2380 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 2381 | if (substream->ops->copy) { |
| 2382 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 2383 | return err; |
| 2384 | } else { |
| 2385 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 2386 | snd_assert(runtime->dma_area, return -EFAULT); |
| 2387 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) |
| 2388 | return -EFAULT; |
| 2389 | } |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2393 | static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | unsigned long data, |
| 2395 | snd_pcm_uframes_t size, |
| 2396 | int nonblock, |
| 2397 | transfer_f transfer) |
| 2398 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2399 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | snd_pcm_uframes_t xfer = 0; |
| 2401 | snd_pcm_uframes_t offset = 0; |
| 2402 | int err = 0; |
| 2403 | |
| 2404 | if (size == 0) |
| 2405 | return 0; |
| 2406 | if (size > runtime->xfer_align) |
| 2407 | size -= size % runtime->xfer_align; |
| 2408 | |
| 2409 | snd_pcm_stream_lock_irq(substream); |
| 2410 | switch (runtime->status->state) { |
| 2411 | case SNDRV_PCM_STATE_PREPARED: |
| 2412 | if (size >= runtime->start_threshold) { |
| 2413 | err = snd_pcm_start(substream); |
| 2414 | if (err < 0) |
| 2415 | goto _end_unlock; |
| 2416 | } |
| 2417 | break; |
| 2418 | case SNDRV_PCM_STATE_DRAINING: |
| 2419 | case SNDRV_PCM_STATE_RUNNING: |
| 2420 | case SNDRV_PCM_STATE_PAUSED: |
| 2421 | break; |
| 2422 | case SNDRV_PCM_STATE_XRUN: |
| 2423 | err = -EPIPE; |
| 2424 | goto _end_unlock; |
| 2425 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2426 | err = -ESTRPIPE; |
| 2427 | goto _end_unlock; |
| 2428 | default: |
| 2429 | err = -EBADFD; |
| 2430 | goto _end_unlock; |
| 2431 | } |
| 2432 | |
| 2433 | while (size > 0) { |
| 2434 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 2435 | snd_pcm_uframes_t avail; |
| 2436 | snd_pcm_uframes_t cont; |
| 2437 | if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2438 | snd_pcm_update_hw_ptr(substream); |
| 2439 | __draining: |
| 2440 | avail = snd_pcm_capture_avail(runtime); |
| 2441 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
| 2442 | if (avail < runtime->xfer_align) { |
| 2443 | err = -EPIPE; |
| 2444 | goto _end_unlock; |
| 2445 | } |
| 2446 | } else if ((avail < runtime->control->avail_min && size > avail) || |
| 2447 | (size >= runtime->xfer_align && avail < runtime->xfer_align)) { |
| 2448 | wait_queue_t wait; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2449 | enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | long tout; |
| 2451 | |
| 2452 | if (nonblock) { |
| 2453 | err = -EAGAIN; |
| 2454 | goto _end_unlock; |
| 2455 | } |
| 2456 | |
| 2457 | init_waitqueue_entry(&wait, current); |
| 2458 | add_wait_queue(&runtime->sleep, &wait); |
| 2459 | while (1) { |
| 2460 | if (signal_pending(current)) { |
| 2461 | state = SIGNALED; |
| 2462 | break; |
| 2463 | } |
| 2464 | set_current_state(TASK_INTERRUPTIBLE); |
| 2465 | snd_pcm_stream_unlock_irq(substream); |
| 2466 | tout = schedule_timeout(10 * HZ); |
| 2467 | snd_pcm_stream_lock_irq(substream); |
| 2468 | if (tout == 0) { |
| 2469 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED && |
| 2470 | runtime->status->state != SNDRV_PCM_STATE_PAUSED) { |
| 2471 | state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED; |
| 2472 | break; |
| 2473 | } |
| 2474 | } |
| 2475 | switch (runtime->status->state) { |
| 2476 | case SNDRV_PCM_STATE_XRUN: |
| 2477 | state = ERROR; |
| 2478 | goto _end_loop; |
| 2479 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2480 | state = SUSPENDED; |
| 2481 | goto _end_loop; |
| 2482 | case SNDRV_PCM_STATE_DRAINING: |
| 2483 | goto __draining; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2484 | case SNDRV_PCM_STATE_SETUP: |
| 2485 | state = DROPPED; |
| 2486 | goto _end_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | default: |
| 2488 | break; |
| 2489 | } |
| 2490 | avail = snd_pcm_capture_avail(runtime); |
| 2491 | if (avail >= runtime->control->avail_min) { |
| 2492 | state = READY; |
| 2493 | break; |
| 2494 | } |
| 2495 | } |
| 2496 | _end_loop: |
| 2497 | remove_wait_queue(&runtime->sleep, &wait); |
| 2498 | |
| 2499 | switch (state) { |
| 2500 | case ERROR: |
| 2501 | err = -EPIPE; |
| 2502 | goto _end_unlock; |
| 2503 | case SUSPENDED: |
| 2504 | err = -ESTRPIPE; |
| 2505 | goto _end_unlock; |
| 2506 | case SIGNALED: |
| 2507 | err = -ERESTARTSYS; |
| 2508 | goto _end_unlock; |
| 2509 | case EXPIRED: |
| 2510 | snd_printd("capture read error (DMA or IRQ trouble?)\n"); |
| 2511 | err = -EIO; |
| 2512 | goto _end_unlock; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2513 | case DROPPED: |
| 2514 | err = -EBADFD; |
| 2515 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | default: |
| 2517 | break; |
| 2518 | } |
| 2519 | } |
| 2520 | if (avail > runtime->xfer_align) |
| 2521 | avail -= avail % runtime->xfer_align; |
| 2522 | frames = size > avail ? avail : size; |
| 2523 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 2524 | if (frames > cont) |
| 2525 | frames = cont; |
| 2526 | snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL); |
| 2527 | appl_ptr = runtime->control->appl_ptr; |
| 2528 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 2529 | snd_pcm_stream_unlock_irq(substream); |
| 2530 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 2531 | goto _end; |
| 2532 | snd_pcm_stream_lock_irq(substream); |
| 2533 | switch (runtime->status->state) { |
| 2534 | case SNDRV_PCM_STATE_XRUN: |
| 2535 | err = -EPIPE; |
| 2536 | goto _end_unlock; |
| 2537 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2538 | err = -ESTRPIPE; |
| 2539 | goto _end_unlock; |
| 2540 | default: |
| 2541 | break; |
| 2542 | } |
| 2543 | appl_ptr += frames; |
| 2544 | if (appl_ptr >= runtime->boundary) |
| 2545 | appl_ptr -= runtime->boundary; |
| 2546 | runtime->control->appl_ptr = appl_ptr; |
| 2547 | if (substream->ops->ack) |
| 2548 | substream->ops->ack(substream); |
| 2549 | |
| 2550 | offset += frames; |
| 2551 | size -= frames; |
| 2552 | xfer += frames; |
| 2553 | if (runtime->sleep_min && |
| 2554 | runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2555 | snd_pcm_tick_prepare(substream); |
| 2556 | } |
| 2557 | _end_unlock: |
| 2558 | snd_pcm_stream_unlock_irq(substream); |
| 2559 | _end: |
| 2560 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 2561 | } |
| 2562 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2563 | snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2565 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | int nonblock; |
| 2567 | |
| 2568 | snd_assert(substream != NULL, return -ENXIO); |
| 2569 | runtime = substream->runtime; |
| 2570 | snd_assert(runtime != NULL, return -ENXIO); |
| 2571 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2572 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2573 | return -EBADFD; |
| 2574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
| 2577 | return -EINVAL; |
| 2578 | return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer); |
| 2579 | } |
| 2580 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2581 | static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | unsigned int hwoff, |
| 2583 | unsigned long data, unsigned int off, |
| 2584 | snd_pcm_uframes_t frames) |
| 2585 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2586 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | int err; |
| 2588 | void __user **bufs = (void __user **)data; |
| 2589 | int channels = runtime->channels; |
| 2590 | int c; |
| 2591 | if (substream->ops->copy) { |
| 2592 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2593 | char __user *buf; |
| 2594 | if (*bufs == NULL) |
| 2595 | continue; |
| 2596 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2597 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2598 | return err; |
| 2599 | } |
| 2600 | } else { |
| 2601 | snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels; |
| 2602 | snd_assert(runtime->dma_area, return -EFAULT); |
| 2603 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2604 | char *hwbuf; |
| 2605 | char __user *buf; |
| 2606 | if (*bufs == NULL) |
| 2607 | continue; |
| 2608 | |
| 2609 | hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2610 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2611 | if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames))) |
| 2612 | return -EFAULT; |
| 2613 | } |
| 2614 | } |
| 2615 | return 0; |
| 2616 | } |
| 2617 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2618 | snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | void __user **bufs, |
| 2620 | snd_pcm_uframes_t frames) |
| 2621 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2622 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | int nonblock; |
| 2624 | |
| 2625 | snd_assert(substream != NULL, return -ENXIO); |
| 2626 | runtime = substream->runtime; |
| 2627 | snd_assert(runtime != NULL, return -ENXIO); |
| 2628 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2629 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2630 | return -EBADFD; |
| 2631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2634 | return -EINVAL; |
| 2635 | return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer); |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * Exported symbols |
| 2640 | */ |
| 2641 | |
| 2642 | EXPORT_SYMBOL(snd_interval_refine); |
| 2643 | EXPORT_SYMBOL(snd_interval_list); |
| 2644 | EXPORT_SYMBOL(snd_interval_ratnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
| 2646 | EXPORT_SYMBOL(_snd_pcm_hw_param_min); |
| 2647 | EXPORT_SYMBOL(_snd_pcm_hw_param_set); |
| 2648 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
| 2649 | EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger); |
| 2650 | EXPORT_SYMBOL(snd_pcm_hw_param_value_min); |
| 2651 | EXPORT_SYMBOL(snd_pcm_hw_param_value_max); |
| 2652 | EXPORT_SYMBOL(snd_pcm_hw_param_mask); |
| 2653 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
| 2654 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
| 2655 | EXPORT_SYMBOL(snd_pcm_hw_param_near); |
| 2656 | EXPORT_SYMBOL(snd_pcm_hw_param_set); |
| 2657 | EXPORT_SYMBOL(snd_pcm_hw_refine); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | EXPORT_SYMBOL(snd_pcm_hw_constraints_init); |
| 2659 | EXPORT_SYMBOL(snd_pcm_hw_constraints_complete); |
| 2660 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
| 2661 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
| 2662 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
| 2663 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
| 2664 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
| 2665 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
| 2666 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
| 2667 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
| 2668 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
| 2669 | EXPORT_SYMBOL(snd_pcm_set_ops); |
| 2670 | EXPORT_SYMBOL(snd_pcm_set_sync); |
| 2671 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
| 2672 | EXPORT_SYMBOL(snd_pcm_stop); |
| 2673 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
| 2674 | EXPORT_SYMBOL(snd_pcm_lib_write); |
| 2675 | EXPORT_SYMBOL(snd_pcm_lib_read); |
| 2676 | EXPORT_SYMBOL(snd_pcm_lib_writev); |
| 2677 | EXPORT_SYMBOL(snd_pcm_lib_readv); |
| 2678 | EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes); |
| 2679 | EXPORT_SYMBOL(snd_pcm_lib_period_bytes); |
| 2680 | /* pcm_memory.c */ |
| 2681 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all); |
| 2682 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages); |
| 2683 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all); |
| 2684 | EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page); |
| 2685 | EXPORT_SYMBOL(snd_pcm_lib_malloc_pages); |
| 2686 | EXPORT_SYMBOL(snd_pcm_lib_free_pages); |