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 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 292 | EXPORT_SYMBOL(snd_pcm_set_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * snd_pcm_sync - set the PCM sync id |
| 296 | * @substream: the pcm substream |
| 297 | * |
| 298 | * Sets the PCM sync identifier for the card. |
| 299 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 300 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 302 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | runtime->sync.id32[0] = substream->pcm->card->number; |
| 305 | runtime->sync.id32[1] = -1; |
| 306 | runtime->sync.id32[2] = -1; |
| 307 | runtime->sync.id32[3] = -1; |
| 308 | } |
| 309 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 310 | EXPORT_SYMBOL(snd_pcm_set_sync); |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | /* |
| 313 | * Standard ioctl routine |
| 314 | */ |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /** |
| 373 | * snd_interval_refine - refine the interval value of configurator |
| 374 | * @i: the interval value to refine |
| 375 | * @v: the interval value to refer to |
| 376 | * |
| 377 | * Refines the interval value with the reference value. |
| 378 | * The interval is changed to the range satisfying both intervals. |
| 379 | * The interval status (min, max, integer, etc.) are evaluated. |
| 380 | * |
| 381 | * Returns non-zero if the value is changed, zero if not changed. |
| 382 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 383 | 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] | 384 | { |
| 385 | int changed = 0; |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 386 | snd_assert(!snd_interval_empty(i), return -EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (i->min < v->min) { |
| 388 | i->min = v->min; |
| 389 | i->openmin = v->openmin; |
| 390 | changed = 1; |
| 391 | } else if (i->min == v->min && !i->openmin && v->openmin) { |
| 392 | i->openmin = 1; |
| 393 | changed = 1; |
| 394 | } |
| 395 | if (i->max > v->max) { |
| 396 | i->max = v->max; |
| 397 | i->openmax = v->openmax; |
| 398 | changed = 1; |
| 399 | } else if (i->max == v->max && !i->openmax && v->openmax) { |
| 400 | i->openmax = 1; |
| 401 | changed = 1; |
| 402 | } |
| 403 | if (!i->integer && v->integer) { |
| 404 | i->integer = 1; |
| 405 | changed = 1; |
| 406 | } |
| 407 | if (i->integer) { |
| 408 | if (i->openmin) { |
| 409 | i->min++; |
| 410 | i->openmin = 0; |
| 411 | } |
| 412 | if (i->openmax) { |
| 413 | i->max--; |
| 414 | i->openmax = 0; |
| 415 | } |
| 416 | } else if (!i->openmin && !i->openmax && i->min == i->max) |
| 417 | i->integer = 1; |
| 418 | if (snd_interval_checkempty(i)) { |
| 419 | snd_interval_none(i); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | return changed; |
| 423 | } |
| 424 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 425 | EXPORT_SYMBOL(snd_interval_refine); |
| 426 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 427 | static int snd_interval_refine_first(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 429 | snd_assert(!snd_interval_empty(i), return -EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (snd_interval_single(i)) |
| 431 | return 0; |
| 432 | i->max = i->min; |
| 433 | i->openmax = i->openmin; |
| 434 | if (i->openmax) |
| 435 | i->max++; |
| 436 | return 1; |
| 437 | } |
| 438 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 439 | static int snd_interval_refine_last(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 441 | snd_assert(!snd_interval_empty(i), return -EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (snd_interval_single(i)) |
| 443 | return 0; |
| 444 | i->min = i->max; |
| 445 | i->openmin = i->openmax; |
| 446 | if (i->openmin) |
| 447 | i->min--; |
| 448 | return 1; |
| 449 | } |
| 450 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 451 | 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] | 452 | { |
| 453 | if (a->empty || b->empty) { |
| 454 | snd_interval_none(c); |
| 455 | return; |
| 456 | } |
| 457 | c->empty = 0; |
| 458 | c->min = mul(a->min, b->min); |
| 459 | c->openmin = (a->openmin || b->openmin); |
| 460 | c->max = mul(a->max, b->max); |
| 461 | c->openmax = (a->openmax || b->openmax); |
| 462 | c->integer = (a->integer && b->integer); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * snd_interval_div - refine the interval value with division |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 467 | * @a: dividend |
| 468 | * @b: divisor |
| 469 | * @c: quotient |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | * |
| 471 | * c = a / b |
| 472 | * |
| 473 | * Returns non-zero if the value is changed, zero if not changed. |
| 474 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 475 | 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] | 476 | { |
| 477 | unsigned int r; |
| 478 | if (a->empty || b->empty) { |
| 479 | snd_interval_none(c); |
| 480 | return; |
| 481 | } |
| 482 | c->empty = 0; |
| 483 | c->min = div32(a->min, b->max, &r); |
| 484 | c->openmin = (r || a->openmin || b->openmax); |
| 485 | if (b->min > 0) { |
| 486 | c->max = div32(a->max, b->min, &r); |
| 487 | if (r) { |
| 488 | c->max++; |
| 489 | c->openmax = 1; |
| 490 | } else |
| 491 | c->openmax = (a->openmax || b->openmin); |
| 492 | } else { |
| 493 | c->max = UINT_MAX; |
| 494 | c->openmax = 0; |
| 495 | } |
| 496 | c->integer = 0; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * snd_interval_muldivk - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 501 | * @a: dividend 1 |
| 502 | * @b: dividend 2 |
| 503 | * @k: divisor (as integer) |
| 504 | * @c: result |
| 505 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * c = a * b / k |
| 507 | * |
| 508 | * Returns non-zero if the value is changed, zero if not changed. |
| 509 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 510 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
| 511 | unsigned int k, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
| 513 | unsigned int r; |
| 514 | if (a->empty || b->empty) { |
| 515 | snd_interval_none(c); |
| 516 | return; |
| 517 | } |
| 518 | c->empty = 0; |
| 519 | c->min = muldiv32(a->min, b->min, k, &r); |
| 520 | c->openmin = (r || a->openmin || b->openmin); |
| 521 | c->max = muldiv32(a->max, b->max, k, &r); |
| 522 | if (r) { |
| 523 | c->max++; |
| 524 | c->openmax = 1; |
| 525 | } else |
| 526 | c->openmax = (a->openmax || b->openmax); |
| 527 | c->integer = 0; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * snd_interval_mulkdiv - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 532 | * @a: dividend 1 |
| 533 | * @k: dividend 2 (as integer) |
| 534 | * @b: divisor |
| 535 | * @c: result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | * |
| 537 | * c = a * k / b |
| 538 | * |
| 539 | * Returns non-zero if the value is changed, zero if not changed. |
| 540 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 541 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
| 542 | const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | unsigned int r; |
| 545 | if (a->empty || b->empty) { |
| 546 | snd_interval_none(c); |
| 547 | return; |
| 548 | } |
| 549 | c->empty = 0; |
| 550 | c->min = muldiv32(a->min, k, b->max, &r); |
| 551 | c->openmin = (r || a->openmin || b->openmax); |
| 552 | if (b->min > 0) { |
| 553 | c->max = muldiv32(a->max, k, b->min, &r); |
| 554 | if (r) { |
| 555 | c->max++; |
| 556 | c->openmax = 1; |
| 557 | } else |
| 558 | c->openmax = (a->openmax || b->openmin); |
| 559 | } else { |
| 560 | c->max = UINT_MAX; |
| 561 | c->openmax = 0; |
| 562 | } |
| 563 | c->integer = 0; |
| 564 | } |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* ---- */ |
| 567 | |
| 568 | |
| 569 | /** |
| 570 | * snd_interval_ratnum - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 571 | * @i: interval to refine |
| 572 | * @rats_count: number of ratnum_t |
| 573 | * @rats: ratnum_t array |
| 574 | * @nump: pointer to store the resultant numerator |
| 575 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | * |
| 577 | * Returns non-zero if the value is changed, zero if not changed. |
| 578 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 579 | int snd_interval_ratnum(struct snd_interval *i, |
| 580 | unsigned int rats_count, struct snd_ratnum *rats, |
| 581 | unsigned int *nump, unsigned int *denp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
| 583 | unsigned int best_num, best_diff, best_den; |
| 584 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 585 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | int err; |
| 587 | |
| 588 | best_num = best_den = best_diff = 0; |
| 589 | for (k = 0; k < rats_count; ++k) { |
| 590 | unsigned int num = rats[k].num; |
| 591 | unsigned int den; |
| 592 | unsigned int q = i->min; |
| 593 | int diff; |
| 594 | if (q == 0) |
| 595 | q = 1; |
| 596 | den = div_down(num, q); |
| 597 | if (den < rats[k].den_min) |
| 598 | continue; |
| 599 | if (den > rats[k].den_max) |
| 600 | den = rats[k].den_max; |
| 601 | else { |
| 602 | unsigned int r; |
| 603 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 604 | if (r != 0) |
| 605 | den -= r; |
| 606 | } |
| 607 | diff = num - q * den; |
| 608 | if (best_num == 0 || |
| 609 | diff * best_den < best_diff * den) { |
| 610 | best_diff = diff; |
| 611 | best_den = den; |
| 612 | best_num = num; |
| 613 | } |
| 614 | } |
| 615 | if (best_den == 0) { |
| 616 | i->empty = 1; |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | t.min = div_down(best_num, best_den); |
| 620 | t.openmin = !!(best_num % best_den); |
| 621 | |
| 622 | best_num = best_den = best_diff = 0; |
| 623 | for (k = 0; k < rats_count; ++k) { |
| 624 | unsigned int num = rats[k].num; |
| 625 | unsigned int den; |
| 626 | unsigned int q = i->max; |
| 627 | int diff; |
| 628 | if (q == 0) { |
| 629 | i->empty = 1; |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | den = div_up(num, q); |
| 633 | if (den > rats[k].den_max) |
| 634 | continue; |
| 635 | if (den < rats[k].den_min) |
| 636 | den = rats[k].den_min; |
| 637 | else { |
| 638 | unsigned int r; |
| 639 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 640 | if (r != 0) |
| 641 | den += rats[k].den_step - r; |
| 642 | } |
| 643 | diff = q * den - num; |
| 644 | if (best_num == 0 || |
| 645 | diff * best_den < best_diff * den) { |
| 646 | best_diff = diff; |
| 647 | best_den = den; |
| 648 | best_num = num; |
| 649 | } |
| 650 | } |
| 651 | if (best_den == 0) { |
| 652 | i->empty = 1; |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | t.max = div_up(best_num, best_den); |
| 656 | t.openmax = !!(best_num % best_den); |
| 657 | t.integer = 0; |
| 658 | err = snd_interval_refine(i, &t); |
| 659 | if (err < 0) |
| 660 | return err; |
| 661 | |
| 662 | if (snd_interval_single(i)) { |
| 663 | if (nump) |
| 664 | *nump = best_num; |
| 665 | if (denp) |
| 666 | *denp = best_den; |
| 667 | } |
| 668 | return err; |
| 669 | } |
| 670 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 671 | EXPORT_SYMBOL(snd_interval_ratnum); |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | /** |
| 674 | * snd_interval_ratden - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 675 | * @i: interval to refine |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 676 | * @rats_count: number of struct ratden |
| 677 | * @rats: struct ratden array |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 678 | * @nump: pointer to store the resultant numerator |
| 679 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | * |
| 681 | * Returns non-zero if the value is changed, zero if not changed. |
| 682 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 683 | static int snd_interval_ratden(struct snd_interval *i, |
| 684 | unsigned int rats_count, struct snd_ratden *rats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | unsigned int *nump, unsigned int *denp) |
| 686 | { |
| 687 | unsigned int best_num, best_diff, best_den; |
| 688 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 689 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | int err; |
| 691 | |
| 692 | best_num = best_den = best_diff = 0; |
| 693 | for (k = 0; k < rats_count; ++k) { |
| 694 | unsigned int num; |
| 695 | unsigned int den = rats[k].den; |
| 696 | unsigned int q = i->min; |
| 697 | int diff; |
| 698 | num = mul(q, den); |
| 699 | if (num > rats[k].num_max) |
| 700 | continue; |
| 701 | if (num < rats[k].num_min) |
| 702 | num = rats[k].num_max; |
| 703 | else { |
| 704 | unsigned int r; |
| 705 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 706 | if (r != 0) |
| 707 | num += rats[k].num_step - r; |
| 708 | } |
| 709 | diff = num - q * den; |
| 710 | if (best_num == 0 || |
| 711 | diff * best_den < best_diff * den) { |
| 712 | best_diff = diff; |
| 713 | best_den = den; |
| 714 | best_num = num; |
| 715 | } |
| 716 | } |
| 717 | if (best_den == 0) { |
| 718 | i->empty = 1; |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | t.min = div_down(best_num, best_den); |
| 722 | t.openmin = !!(best_num % best_den); |
| 723 | |
| 724 | best_num = best_den = best_diff = 0; |
| 725 | for (k = 0; k < rats_count; ++k) { |
| 726 | unsigned int num; |
| 727 | unsigned int den = rats[k].den; |
| 728 | unsigned int q = i->max; |
| 729 | int diff; |
| 730 | num = mul(q, den); |
| 731 | if (num < rats[k].num_min) |
| 732 | continue; |
| 733 | if (num > rats[k].num_max) |
| 734 | num = rats[k].num_max; |
| 735 | else { |
| 736 | unsigned int r; |
| 737 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 738 | if (r != 0) |
| 739 | num -= r; |
| 740 | } |
| 741 | diff = q * den - num; |
| 742 | if (best_num == 0 || |
| 743 | diff * best_den < best_diff * den) { |
| 744 | best_diff = diff; |
| 745 | best_den = den; |
| 746 | best_num = num; |
| 747 | } |
| 748 | } |
| 749 | if (best_den == 0) { |
| 750 | i->empty = 1; |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | t.max = div_up(best_num, best_den); |
| 754 | t.openmax = !!(best_num % best_den); |
| 755 | t.integer = 0; |
| 756 | err = snd_interval_refine(i, &t); |
| 757 | if (err < 0) |
| 758 | return err; |
| 759 | |
| 760 | if (snd_interval_single(i)) { |
| 761 | if (nump) |
| 762 | *nump = best_num; |
| 763 | if (denp) |
| 764 | *denp = best_den; |
| 765 | } |
| 766 | return err; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * snd_interval_list - refine the interval value from the list |
| 771 | * @i: the interval value to refine |
| 772 | * @count: the number of elements in the list |
| 773 | * @list: the value list |
| 774 | * @mask: the bit-mask to evaluate |
| 775 | * |
| 776 | * Refines the interval value from the list. |
| 777 | * When mask is non-zero, only the elements corresponding to bit 1 are |
| 778 | * evaluated. |
| 779 | * |
| 780 | * Returns non-zero if the value is changed, zero if not changed. |
| 781 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 782 | 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] | 783 | { |
| 784 | unsigned int k; |
| 785 | int changed = 0; |
| 786 | for (k = 0; k < count; k++) { |
| 787 | if (mask && !(mask & (1 << k))) |
| 788 | continue; |
| 789 | if (i->min == list[k] && !i->openmin) |
| 790 | goto _l1; |
| 791 | if (i->min < list[k]) { |
| 792 | i->min = list[k]; |
| 793 | i->openmin = 0; |
| 794 | changed = 1; |
| 795 | goto _l1; |
| 796 | } |
| 797 | } |
| 798 | i->empty = 1; |
| 799 | return -EINVAL; |
| 800 | _l1: |
| 801 | for (k = count; k-- > 0;) { |
| 802 | if (mask && !(mask & (1 << k))) |
| 803 | continue; |
| 804 | if (i->max == list[k] && !i->openmax) |
| 805 | goto _l2; |
| 806 | if (i->max > list[k]) { |
| 807 | i->max = list[k]; |
| 808 | i->openmax = 0; |
| 809 | changed = 1; |
| 810 | goto _l2; |
| 811 | } |
| 812 | } |
| 813 | i->empty = 1; |
| 814 | return -EINVAL; |
| 815 | _l2: |
| 816 | if (snd_interval_checkempty(i)) { |
| 817 | i->empty = 1; |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | return changed; |
| 821 | } |
| 822 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 823 | EXPORT_SYMBOL(snd_interval_list); |
| 824 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 825 | 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] | 826 | { |
| 827 | unsigned int n; |
| 828 | int changed = 0; |
| 829 | n = (i->min - min) % step; |
| 830 | if (n != 0 || i->openmin) { |
| 831 | i->min += step - n; |
| 832 | changed = 1; |
| 833 | } |
| 834 | n = (i->max - min) % step; |
| 835 | if (n != 0 || i->openmax) { |
| 836 | i->max -= n; |
| 837 | changed = 1; |
| 838 | } |
| 839 | if (snd_interval_checkempty(i)) { |
| 840 | i->empty = 1; |
| 841 | return -EINVAL; |
| 842 | } |
| 843 | return changed; |
| 844 | } |
| 845 | |
| 846 | /* Info constraints helpers */ |
| 847 | |
| 848 | /** |
| 849 | * snd_pcm_hw_rule_add - add the hw-constraint rule |
| 850 | * @runtime: the pcm runtime instance |
| 851 | * @cond: condition bits |
| 852 | * @var: the variable to evaluate |
| 853 | * @func: the evaluation function |
| 854 | * @private: the private data pointer passed to function |
| 855 | * @dep: the dependent variables |
| 856 | * |
| 857 | * Returns zero if successful, or a negative error code on failure. |
| 858 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 859 | 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] | 860 | int var, |
| 861 | snd_pcm_hw_rule_func_t func, void *private, |
| 862 | int dep, ...) |
| 863 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 864 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 865 | struct snd_pcm_hw_rule *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | unsigned int k; |
| 867 | va_list args; |
| 868 | va_start(args, dep); |
| 869 | if (constrs->rules_num >= constrs->rules_all) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 870 | struct snd_pcm_hw_rule *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | unsigned int new_rules = constrs->rules_all + 16; |
| 872 | new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL); |
| 873 | if (!new) |
| 874 | return -ENOMEM; |
| 875 | if (constrs->rules) { |
| 876 | memcpy(new, constrs->rules, |
| 877 | constrs->rules_num * sizeof(*c)); |
| 878 | kfree(constrs->rules); |
| 879 | } |
| 880 | constrs->rules = new; |
| 881 | constrs->rules_all = new_rules; |
| 882 | } |
| 883 | c = &constrs->rules[constrs->rules_num]; |
| 884 | c->cond = cond; |
| 885 | c->func = func; |
| 886 | c->var = var; |
| 887 | c->private = private; |
| 888 | k = 0; |
| 889 | while (1) { |
| 890 | snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL); |
| 891 | c->deps[k++] = dep; |
| 892 | if (dep < 0) |
| 893 | break; |
| 894 | dep = va_arg(args, int); |
| 895 | } |
| 896 | constrs->rules_num++; |
| 897 | va_end(args); |
| 898 | return 0; |
| 899 | } |
| 900 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 901 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /** |
| 904 | * snd_pcm_hw_constraint_mask |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 905 | * @runtime: PCM runtime instance |
| 906 | * @var: hw_params variable to apply the mask |
| 907 | * @mask: the bitmap mask |
| 908 | * |
| 909 | * Apply the constraint of the given bitmap mask to a mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 911 | 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] | 912 | u_int32_t mask) |
| 913 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 914 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 915 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | *maskp->bits &= mask; |
| 917 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ |
| 918 | if (*maskp->bits == 0) |
| 919 | return -EINVAL; |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * snd_pcm_hw_constraint_mask64 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 925 | * @runtime: PCM runtime instance |
| 926 | * @var: hw_params variable to apply the mask |
| 927 | * @mask: the 64bit bitmap mask |
| 928 | * |
| 929 | * Apply the constraint of the given bitmap mask to a mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 931 | 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] | 932 | u_int64_t mask) |
| 933 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 934 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 935 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | maskp->bits[0] &= (u_int32_t)mask; |
| 937 | maskp->bits[1] &= (u_int32_t)(mask >> 32); |
| 938 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ |
| 939 | if (! maskp->bits[0] && ! maskp->bits[1]) |
| 940 | return -EINVAL; |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * snd_pcm_hw_constraint_integer |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 946 | * @runtime: PCM runtime instance |
| 947 | * @var: hw_params variable to apply the integer constraint |
| 948 | * |
| 949 | * Apply the constraint of integer to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 951 | 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] | 952 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 953 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
| 955 | } |
| 956 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 957 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | /** |
| 960 | * snd_pcm_hw_constraint_minmax |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 961 | * @runtime: PCM runtime instance |
| 962 | * @var: hw_params variable to apply the range |
| 963 | * @min: the minimal value |
| 964 | * @max: the maximal value |
| 965 | * |
| 966 | * Apply the min/max range constraint to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 968 | 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] | 969 | unsigned int min, unsigned int max) |
| 970 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 971 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 972 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | t.min = min; |
| 974 | t.max = max; |
| 975 | t.openmin = t.openmax = 0; |
| 976 | t.integer = 0; |
| 977 | return snd_interval_refine(constrs_interval(constrs, var), &t); |
| 978 | } |
| 979 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 980 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
| 981 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 982 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
| 983 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 985 | struct snd_pcm_hw_constraint_list *list = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
| 987 | } |
| 988 | |
| 989 | |
| 990 | /** |
| 991 | * snd_pcm_hw_constraint_list |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 992 | * @runtime: PCM runtime instance |
| 993 | * @cond: condition bits |
| 994 | * @var: hw_params variable to apply the list constraint |
| 995 | * @l: list |
| 996 | * |
| 997 | * Apply the list of constraints to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 999 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | unsigned int cond, |
| 1001 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1002 | struct snd_pcm_hw_constraint_list *l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
| 1004 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1005 | snd_pcm_hw_rule_list, l, |
| 1006 | var, -1); |
| 1007 | } |
| 1008 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1009 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
| 1010 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1011 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
| 1012 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1014 | struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | unsigned int num = 0, den = 0; |
| 1016 | int err; |
| 1017 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), |
| 1018 | r->nrats, r->rats, &num, &den); |
| 1019 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1020 | params->rate_num = num; |
| 1021 | params->rate_den = den; |
| 1022 | } |
| 1023 | return err; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * snd_pcm_hw_constraint_ratnums |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1028 | * @runtime: PCM runtime instance |
| 1029 | * @cond: condition bits |
| 1030 | * @var: hw_params variable to apply the ratnums constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1031 | * @r: struct snd_ratnums constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1033 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | unsigned int cond, |
| 1035 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1036 | struct snd_pcm_hw_constraint_ratnums *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | { |
| 1038 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1039 | snd_pcm_hw_rule_ratnums, r, |
| 1040 | var, -1); |
| 1041 | } |
| 1042 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1043 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
| 1044 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1045 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
| 1046 | struct snd_pcm_hw_rule *rule) |
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 | struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | unsigned int num = 0, den = 0; |
| 1050 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), |
| 1051 | r->nrats, r->rats, &num, &den); |
| 1052 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1053 | params->rate_num = num; |
| 1054 | params->rate_den = den; |
| 1055 | } |
| 1056 | return err; |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * snd_pcm_hw_constraint_ratdens |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1061 | * @runtime: PCM runtime instance |
| 1062 | * @cond: condition bits |
| 1063 | * @var: hw_params variable to apply the ratdens constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1064 | * @r: struct snd_ratdens constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1066 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | unsigned int cond, |
| 1068 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1069 | struct snd_pcm_hw_constraint_ratdens *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
| 1071 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1072 | snd_pcm_hw_rule_ratdens, r, |
| 1073 | var, -1); |
| 1074 | } |
| 1075 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1076 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
| 1077 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1078 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
| 1079 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
| 1081 | unsigned int l = (unsigned long) rule->private; |
| 1082 | int width = l & 0xffff; |
| 1083 | unsigned int msbits = l >> 16; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1084 | 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] | 1085 | if (snd_interval_single(i) && snd_interval_value(i) == width) |
| 1086 | params->msbits = msbits; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * snd_pcm_hw_constraint_msbits |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1092 | * @runtime: PCM runtime instance |
| 1093 | * @cond: condition bits |
| 1094 | * @width: sample bits width |
| 1095 | * @msbits: msbits width |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1097 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | unsigned int cond, |
| 1099 | unsigned int width, |
| 1100 | unsigned int msbits) |
| 1101 | { |
| 1102 | unsigned long l = (msbits << 16) | width; |
| 1103 | return snd_pcm_hw_rule_add(runtime, cond, -1, |
| 1104 | snd_pcm_hw_rule_msbits, |
| 1105 | (void*) l, |
| 1106 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1107 | } |
| 1108 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1109 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
| 1110 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1111 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
| 1112 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | { |
| 1114 | unsigned long step = (unsigned long) rule->private; |
| 1115 | return snd_interval_step(hw_param_interval(params, rule->var), 0, step); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * snd_pcm_hw_constraint_step |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1120 | * @runtime: PCM runtime instance |
| 1121 | * @cond: condition bits |
| 1122 | * @var: hw_params variable to apply the step constraint |
| 1123 | * @step: step size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1125 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | unsigned int cond, |
| 1127 | snd_pcm_hw_param_t var, |
| 1128 | unsigned long step) |
| 1129 | { |
| 1130 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1131 | snd_pcm_hw_rule_step, (void *) step, |
| 1132 | var, -1); |
| 1133 | } |
| 1134 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1135 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
| 1136 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1137 | 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] | 1138 | { |
| 1139 | static int pow2_sizes[] = { |
| 1140 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
| 1141 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, |
| 1142 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, |
| 1143 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 |
| 1144 | }; |
| 1145 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 1146 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * snd_pcm_hw_constraint_pow2 |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1151 | * @runtime: PCM runtime instance |
| 1152 | * @cond: condition bits |
| 1153 | * @var: hw_params variable to apply the power-of-2 constraint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1155 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | unsigned int cond, |
| 1157 | snd_pcm_hw_param_t var) |
| 1158 | { |
| 1159 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1160 | snd_pcm_hw_rule_pow2, NULL, |
| 1161 | var, -1); |
| 1162 | } |
| 1163 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1164 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
| 1165 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1166 | 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] | 1167 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
| 1169 | if (hw_is_mask(var)) { |
| 1170 | snd_mask_any(hw_param_mask(params, var)); |
| 1171 | params->cmask |= 1 << var; |
| 1172 | params->rmask |= 1 << var; |
| 1173 | return; |
| 1174 | } |
| 1175 | if (hw_is_interval(var)) { |
| 1176 | snd_interval_any(hw_param_interval(params, var)); |
| 1177 | params->cmask |= 1 << var; |
| 1178 | params->rmask |= 1 << var; |
| 1179 | return; |
| 1180 | } |
| 1181 | snd_BUG(); |
| 1182 | } |
| 1183 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1184 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
| 1186 | unsigned int k; |
| 1187 | memset(params, 0, sizeof(*params)); |
| 1188 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) |
| 1189 | _snd_pcm_hw_param_any(params, k); |
| 1190 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
| 1191 | _snd_pcm_hw_param_any(params, k); |
| 1192 | params->info = ~0U; |
| 1193 | } |
| 1194 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1195 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
| 1197 | /** |
| 1198 | * snd_pcm_hw_param_value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1199 | * @params: the hw_params instance |
| 1200 | * @var: parameter to retrieve |
| 1201 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | * |
| 1203 | * Return the value for field PAR if it's fixed in configuration space |
| 1204 | * defined by PARAMS. Return -EINVAL otherwise |
| 1205 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1206 | int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
| 1207 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | if (hw_is_mask(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1210 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | if (!snd_mask_single(mask)) |
| 1212 | return -EINVAL; |
| 1213 | if (dir) |
| 1214 | *dir = 0; |
| 1215 | return snd_mask_value(mask); |
| 1216 | } |
| 1217 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1218 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | if (!snd_interval_single(i)) |
| 1220 | return -EINVAL; |
| 1221 | if (dir) |
| 1222 | *dir = i->openmin; |
| 1223 | return snd_interval_value(i); |
| 1224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1228 | EXPORT_SYMBOL(snd_pcm_hw_param_value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1230 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | snd_pcm_hw_param_t var) |
| 1232 | { |
| 1233 | if (hw_is_mask(var)) { |
| 1234 | snd_mask_none(hw_param_mask(params, var)); |
| 1235 | params->cmask |= 1 << var; |
| 1236 | params->rmask |= 1 << var; |
| 1237 | } else if (hw_is_interval(var)) { |
| 1238 | snd_interval_none(hw_param_interval(params, var)); |
| 1239 | params->cmask |= 1 << var; |
| 1240 | params->rmask |= 1 << var; |
| 1241 | } else { |
| 1242 | snd_BUG(); |
| 1243 | } |
| 1244 | } |
| 1245 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1246 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1248 | 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] | 1249 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | { |
| 1251 | int changed; |
| 1252 | if (hw_is_mask(var)) |
| 1253 | changed = snd_mask_refine_first(hw_param_mask(params, var)); |
| 1254 | else if (hw_is_interval(var)) |
| 1255 | changed = snd_interval_refine_first(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1256 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (changed) { |
| 1259 | params->cmask |= 1 << var; |
| 1260 | params->rmask |= 1 << var; |
| 1261 | } |
| 1262 | return changed; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | /** |
| 1267 | * snd_pcm_hw_param_first |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1268 | * @pcm: PCM instance |
| 1269 | * @params: the hw_params instance |
| 1270 | * @var: parameter to retrieve |
| 1271 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | * |
| 1273 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1274 | * values > minimum. Reduce configuration space accordingly. |
| 1275 | * Return the minimum. |
| 1276 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1277 | int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
| 1278 | struct snd_pcm_hw_params *params, |
| 1279 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | { |
| 1281 | int changed = _snd_pcm_hw_param_first(params, var); |
| 1282 | if (changed < 0) |
| 1283 | return changed; |
| 1284 | if (params->rmask) { |
| 1285 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1286 | snd_assert(err >= 0, return err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | return snd_pcm_hw_param_value(params, var, dir); |
| 1289 | } |
| 1290 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1291 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
| 1292 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1293 | 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] | 1294 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | { |
| 1296 | int changed; |
| 1297 | if (hw_is_mask(var)) |
| 1298 | changed = snd_mask_refine_last(hw_param_mask(params, var)); |
| 1299 | else if (hw_is_interval(var)) |
| 1300 | changed = snd_interval_refine_last(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1301 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | if (changed) { |
| 1304 | params->cmask |= 1 << var; |
| 1305 | params->rmask |= 1 << var; |
| 1306 | } |
| 1307 | return changed; |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | /** |
| 1312 | * snd_pcm_hw_param_last |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1313 | * @pcm: PCM instance |
| 1314 | * @params: the hw_params instance |
| 1315 | * @var: parameter to retrieve |
| 1316 | * @dir: pointer to the direction (-1,0,1) or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | * |
| 1318 | * Inside configuration space defined by PARAMS remove from PAR all |
| 1319 | * values < maximum. Reduce configuration space accordingly. |
| 1320 | * Return the maximum. |
| 1321 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1322 | int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
| 1323 | struct snd_pcm_hw_params *params, |
| 1324 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
| 1326 | int changed = _snd_pcm_hw_param_last(params, var); |
| 1327 | if (changed < 0) |
| 1328 | return changed; |
| 1329 | if (params->rmask) { |
| 1330 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1331 | snd_assert(err >= 0, return err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | return snd_pcm_hw_param_value(params, var, dir); |
| 1334 | } |
| 1335 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1336 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | /** |
| 1339 | * snd_pcm_hw_param_choose |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1340 | * @pcm: PCM instance |
| 1341 | * @params: the hw_params instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | * |
| 1343 | * Choose one configuration from configuration space defined by PARAMS |
| 1344 | * The configuration chosen is that obtained fixing in this order: |
| 1345 | * first access, first format, first subformat, min channels, |
| 1346 | * min rate, min period time, max buffer size, min tick time |
| 1347 | */ |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1348 | int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, |
| 1349 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | { |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1351 | static int vars[] = { |
| 1352 | SNDRV_PCM_HW_PARAM_ACCESS, |
| 1353 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1354 | SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 1355 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1356 | SNDRV_PCM_HW_PARAM_RATE, |
| 1357 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1358 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1359 | SNDRV_PCM_HW_PARAM_TICK_TIME, |
| 1360 | -1 |
| 1361 | }; |
| 1362 | int err, *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame^] | 1364 | for (v = vars; *v != -1; v++) { |
| 1365 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) |
| 1366 | err = snd_pcm_hw_param_first(pcm, params, *v, NULL); |
| 1367 | else |
| 1368 | err = snd_pcm_hw_param_last(pcm, params, *v, NULL); |
| 1369 | snd_assert(err >= 0, return err); |
| 1370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | return 0; |
| 1372 | } |
| 1373 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1374 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | void *arg) |
| 1376 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1377 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | unsigned long flags; |
| 1379 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1380 | if (snd_pcm_running(substream) && |
| 1381 | snd_pcm_update_hw_ptr(substream) >= 0) |
| 1382 | runtime->status->hw_ptr %= runtime->buffer_size; |
| 1383 | else |
| 1384 | runtime->status->hw_ptr = 0; |
| 1385 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1389 | 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] | 1390 | void *arg) |
| 1391 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1392 | struct snd_pcm_channel_info *info = arg; |
| 1393 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | int width; |
| 1395 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { |
| 1396 | info->offset = -1; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | width = snd_pcm_format_physical_width(runtime->format); |
| 1400 | if (width < 0) |
| 1401 | return width; |
| 1402 | info->offset = 0; |
| 1403 | switch (runtime->access) { |
| 1404 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: |
| 1405 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: |
| 1406 | info->first = info->channel * width; |
| 1407 | info->step = runtime->channels * width; |
| 1408 | break; |
| 1409 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: |
| 1410 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: |
| 1411 | { |
| 1412 | size_t size = runtime->dma_bytes / runtime->channels; |
| 1413 | info->first = info->channel * size * 8; |
| 1414 | info->step = width; |
| 1415 | break; |
| 1416 | } |
| 1417 | default: |
| 1418 | snd_BUG(); |
| 1419 | break; |
| 1420 | } |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback |
| 1426 | * @substream: the pcm substream instance |
| 1427 | * @cmd: ioctl command |
| 1428 | * @arg: ioctl argument |
| 1429 | * |
| 1430 | * Processes the generic ioctl commands for PCM. |
| 1431 | * Can be passed as the ioctl callback for PCM ops. |
| 1432 | * |
| 1433 | * Returns zero if successful, or a negative error code on failure. |
| 1434 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1435 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | unsigned int cmd, void *arg) |
| 1437 | { |
| 1438 | switch (cmd) { |
| 1439 | case SNDRV_PCM_IOCTL1_INFO: |
| 1440 | return 0; |
| 1441 | case SNDRV_PCM_IOCTL1_RESET: |
| 1442 | return snd_pcm_lib_ioctl_reset(substream, arg); |
| 1443 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: |
| 1444 | return snd_pcm_lib_ioctl_channel_info(substream, arg); |
| 1445 | } |
| 1446 | return -ENXIO; |
| 1447 | } |
| 1448 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1449 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
| 1450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * Conditions |
| 1453 | */ |
| 1454 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1455 | static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | unsigned long ticks) |
| 1457 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1458 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | if (ticks == 0) |
| 1460 | del_timer(&runtime->tick_timer); |
| 1461 | else { |
| 1462 | ticks += (1000000 / HZ) - 1; |
| 1463 | ticks /= (1000000 / HZ); |
| 1464 | mod_timer(&runtime->tick_timer, jiffies + ticks); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /* Temporary alias */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1469 | 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] | 1470 | { |
| 1471 | snd_pcm_system_tick_set(substream, ticks); |
| 1472 | } |
| 1473 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1474 | void snd_pcm_tick_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1476 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | snd_pcm_uframes_t frames = ULONG_MAX; |
| 1478 | snd_pcm_uframes_t avail, dist; |
| 1479 | unsigned int ticks; |
| 1480 | u_int64_t n; |
| 1481 | u_int32_t r; |
| 1482 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1483 | if (runtime->silence_size >= runtime->boundary) { |
| 1484 | frames = 1; |
| 1485 | } else if (runtime->silence_size > 0 && |
| 1486 | runtime->silence_filled < runtime->buffer_size) { |
| 1487 | snd_pcm_sframes_t noise_dist; |
| 1488 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
Takashi Iwai | 235475c | 2005-12-07 15:28:07 +0100 | [diff] [blame] | 1489 | if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold) |
| 1490 | frames = noise_dist - runtime->silence_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } |
| 1492 | avail = snd_pcm_playback_avail(runtime); |
| 1493 | } else { |
| 1494 | avail = snd_pcm_capture_avail(runtime); |
| 1495 | } |
| 1496 | if (avail < runtime->control->avail_min) { |
| 1497 | snd_pcm_sframes_t n = runtime->control->avail_min - avail; |
| 1498 | if (n > 0 && frames > (snd_pcm_uframes_t)n) |
| 1499 | frames = n; |
| 1500 | } |
| 1501 | if (avail < runtime->buffer_size) { |
| 1502 | snd_pcm_sframes_t n = runtime->buffer_size - avail; |
| 1503 | if (n > 0 && frames > (snd_pcm_uframes_t)n) |
| 1504 | frames = n; |
| 1505 | } |
| 1506 | if (frames == ULONG_MAX) { |
| 1507 | snd_pcm_tick_set(substream, 0); |
| 1508 | return; |
| 1509 | } |
| 1510 | dist = runtime->status->hw_ptr - runtime->hw_ptr_base; |
| 1511 | /* Distance to next interrupt */ |
| 1512 | dist = runtime->period_size - dist % runtime->period_size; |
| 1513 | if (dist <= frames) { |
| 1514 | snd_pcm_tick_set(substream, 0); |
| 1515 | return; |
| 1516 | } |
| 1517 | /* the base time is us */ |
| 1518 | n = frames; |
| 1519 | n *= 1000000; |
| 1520 | div64_32(&n, runtime->tick_time * runtime->rate, &r); |
| 1521 | ticks = n + (r > 0 ? 1 : 0); |
| 1522 | if (ticks < runtime->sleep_min) |
| 1523 | ticks = runtime->sleep_min; |
| 1524 | snd_pcm_tick_set(substream, (unsigned long) ticks); |
| 1525 | } |
| 1526 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1527 | void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1529 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | unsigned long flags; |
| 1531 | |
| 1532 | snd_assert(substream != NULL, return); |
| 1533 | runtime = substream->runtime; |
| 1534 | snd_assert(runtime != NULL, return); |
| 1535 | |
| 1536 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1537 | if (!snd_pcm_running(substream) || |
| 1538 | snd_pcm_update_hw_ptr(substream) < 0) |
| 1539 | goto _end; |
| 1540 | if (runtime->sleep_min) |
| 1541 | snd_pcm_tick_prepare(substream); |
| 1542 | _end: |
| 1543 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * snd_pcm_period_elapsed - update the pcm status for the next period |
| 1548 | * @substream: the pcm substream instance |
| 1549 | * |
| 1550 | * This function is called from the interrupt handler when the |
| 1551 | * PCM has processed the period size. It will update the current |
| 1552 | * pointer, set up the tick, wake up sleepers, etc. |
| 1553 | * |
| 1554 | * Even if more than one periods have elapsed since the last call, you |
| 1555 | * have to call this only once. |
| 1556 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1557 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1559 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | unsigned long flags; |
| 1561 | |
| 1562 | snd_assert(substream != NULL, return); |
| 1563 | runtime = substream->runtime; |
| 1564 | snd_assert(runtime != NULL, return); |
| 1565 | |
| 1566 | if (runtime->transfer_ack_begin) |
| 1567 | runtime->transfer_ack_begin(substream); |
| 1568 | |
| 1569 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1570 | if (!snd_pcm_running(substream) || |
| 1571 | snd_pcm_update_hw_ptr_interrupt(substream) < 0) |
| 1572 | goto _end; |
| 1573 | |
| 1574 | if (substream->timer_running) |
| 1575 | snd_timer_interrupt(substream->timer, 1); |
| 1576 | if (runtime->sleep_min) |
| 1577 | snd_pcm_tick_prepare(substream); |
| 1578 | _end: |
| 1579 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1580 | if (runtime->transfer_ack_end) |
| 1581 | runtime->transfer_ack_end(substream); |
| 1582 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); |
| 1583 | } |
| 1584 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1585 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
| 1586 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1587 | static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | unsigned int hwoff, |
| 1589 | unsigned long data, unsigned int off, |
| 1590 | snd_pcm_uframes_t frames) |
| 1591 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1592 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | int err; |
| 1594 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 1595 | if (substream->ops->copy) { |
| 1596 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 1597 | return err; |
| 1598 | } else { |
| 1599 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 1600 | snd_assert(runtime->dma_area, return -EFAULT); |
| 1601 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) |
| 1602 | return -EFAULT; |
| 1603 | } |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1607 | typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | unsigned long data, unsigned int off, |
| 1609 | snd_pcm_uframes_t size); |
| 1610 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1611 | 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] | 1612 | unsigned long data, |
| 1613 | snd_pcm_uframes_t size, |
| 1614 | int nonblock, |
| 1615 | transfer_f transfer) |
| 1616 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1617 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | snd_pcm_uframes_t xfer = 0; |
| 1619 | snd_pcm_uframes_t offset = 0; |
| 1620 | int err = 0; |
| 1621 | |
| 1622 | if (size == 0) |
| 1623 | return 0; |
| 1624 | if (size > runtime->xfer_align) |
| 1625 | size -= size % runtime->xfer_align; |
| 1626 | |
| 1627 | snd_pcm_stream_lock_irq(substream); |
| 1628 | switch (runtime->status->state) { |
| 1629 | case SNDRV_PCM_STATE_PREPARED: |
| 1630 | case SNDRV_PCM_STATE_RUNNING: |
| 1631 | case SNDRV_PCM_STATE_PAUSED: |
| 1632 | break; |
| 1633 | case SNDRV_PCM_STATE_XRUN: |
| 1634 | err = -EPIPE; |
| 1635 | goto _end_unlock; |
| 1636 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1637 | err = -ESTRPIPE; |
| 1638 | goto _end_unlock; |
| 1639 | default: |
| 1640 | err = -EBADFD; |
| 1641 | goto _end_unlock; |
| 1642 | } |
| 1643 | |
| 1644 | while (size > 0) { |
| 1645 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 1646 | snd_pcm_uframes_t avail; |
| 1647 | snd_pcm_uframes_t cont; |
| 1648 | if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 1649 | snd_pcm_update_hw_ptr(substream); |
| 1650 | avail = snd_pcm_playback_avail(runtime); |
| 1651 | if (((avail < runtime->control->avail_min && size > avail) || |
| 1652 | (size >= runtime->xfer_align && avail < runtime->xfer_align))) { |
| 1653 | wait_queue_t wait; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 1654 | enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | long tout; |
| 1656 | |
| 1657 | if (nonblock) { |
| 1658 | err = -EAGAIN; |
| 1659 | goto _end_unlock; |
| 1660 | } |
| 1661 | |
| 1662 | init_waitqueue_entry(&wait, current); |
| 1663 | add_wait_queue(&runtime->sleep, &wait); |
| 1664 | while (1) { |
| 1665 | if (signal_pending(current)) { |
| 1666 | state = SIGNALED; |
| 1667 | break; |
| 1668 | } |
| 1669 | set_current_state(TASK_INTERRUPTIBLE); |
| 1670 | snd_pcm_stream_unlock_irq(substream); |
| 1671 | tout = schedule_timeout(10 * HZ); |
| 1672 | snd_pcm_stream_lock_irq(substream); |
| 1673 | if (tout == 0) { |
| 1674 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED && |
| 1675 | runtime->status->state != SNDRV_PCM_STATE_PAUSED) { |
| 1676 | state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED; |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | switch (runtime->status->state) { |
| 1681 | case SNDRV_PCM_STATE_XRUN: |
| 1682 | case SNDRV_PCM_STATE_DRAINING: |
| 1683 | state = ERROR; |
| 1684 | goto _end_loop; |
| 1685 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1686 | state = SUSPENDED; |
| 1687 | goto _end_loop; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 1688 | case SNDRV_PCM_STATE_SETUP: |
| 1689 | state = DROPPED; |
| 1690 | goto _end_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | default: |
| 1692 | break; |
| 1693 | } |
| 1694 | avail = snd_pcm_playback_avail(runtime); |
| 1695 | if (avail >= runtime->control->avail_min) { |
| 1696 | state = READY; |
| 1697 | break; |
| 1698 | } |
| 1699 | } |
| 1700 | _end_loop: |
| 1701 | remove_wait_queue(&runtime->sleep, &wait); |
| 1702 | |
| 1703 | switch (state) { |
| 1704 | case ERROR: |
| 1705 | err = -EPIPE; |
| 1706 | goto _end_unlock; |
| 1707 | case SUSPENDED: |
| 1708 | err = -ESTRPIPE; |
| 1709 | goto _end_unlock; |
| 1710 | case SIGNALED: |
| 1711 | err = -ERESTARTSYS; |
| 1712 | goto _end_unlock; |
| 1713 | case EXPIRED: |
| 1714 | snd_printd("playback write error (DMA or IRQ trouble?)\n"); |
| 1715 | err = -EIO; |
| 1716 | goto _end_unlock; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 1717 | case DROPPED: |
| 1718 | err = -EBADFD; |
| 1719 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | default: |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | if (avail > runtime->xfer_align) |
| 1725 | avail -= avail % runtime->xfer_align; |
| 1726 | frames = size > avail ? avail : size; |
| 1727 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 1728 | if (frames > cont) |
| 1729 | frames = cont; |
| 1730 | snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL); |
| 1731 | appl_ptr = runtime->control->appl_ptr; |
| 1732 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 1733 | snd_pcm_stream_unlock_irq(substream); |
| 1734 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 1735 | goto _end; |
| 1736 | snd_pcm_stream_lock_irq(substream); |
| 1737 | switch (runtime->status->state) { |
| 1738 | case SNDRV_PCM_STATE_XRUN: |
| 1739 | err = -EPIPE; |
| 1740 | goto _end_unlock; |
| 1741 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1742 | err = -ESTRPIPE; |
| 1743 | goto _end_unlock; |
| 1744 | default: |
| 1745 | break; |
| 1746 | } |
| 1747 | appl_ptr += frames; |
| 1748 | if (appl_ptr >= runtime->boundary) |
| 1749 | appl_ptr -= runtime->boundary; |
| 1750 | runtime->control->appl_ptr = appl_ptr; |
| 1751 | if (substream->ops->ack) |
| 1752 | substream->ops->ack(substream); |
| 1753 | |
| 1754 | offset += frames; |
| 1755 | size -= frames; |
| 1756 | xfer += frames; |
| 1757 | if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && |
| 1758 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { |
| 1759 | err = snd_pcm_start(substream); |
| 1760 | if (err < 0) |
| 1761 | goto _end_unlock; |
| 1762 | } |
| 1763 | if (runtime->sleep_min && |
| 1764 | runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 1765 | snd_pcm_tick_prepare(substream); |
| 1766 | } |
| 1767 | _end_unlock: |
| 1768 | snd_pcm_stream_unlock_irq(substream); |
| 1769 | _end: |
| 1770 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 1771 | } |
| 1772 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1773 | 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] | 1774 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1775 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | int nonblock; |
| 1777 | |
| 1778 | snd_assert(substream != NULL, return -ENXIO); |
| 1779 | runtime = substream->runtime; |
| 1780 | snd_assert(runtime != NULL, return -ENXIO); |
| 1781 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 1782 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 1783 | return -EBADFD; |
| 1784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | |
| 1787 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && |
| 1788 | runtime->channels > 1) |
| 1789 | return -EINVAL; |
| 1790 | return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock, |
| 1791 | snd_pcm_lib_write_transfer); |
| 1792 | } |
| 1793 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1794 | EXPORT_SYMBOL(snd_pcm_lib_write); |
| 1795 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1796 | static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | unsigned int hwoff, |
| 1798 | unsigned long data, unsigned int off, |
| 1799 | snd_pcm_uframes_t frames) |
| 1800 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1801 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | int err; |
| 1803 | void __user **bufs = (void __user **)data; |
| 1804 | int channels = runtime->channels; |
| 1805 | int c; |
| 1806 | if (substream->ops->copy) { |
| 1807 | snd_assert(substream->ops->silence != NULL, return -EINVAL); |
| 1808 | for (c = 0; c < channels; ++c, ++bufs) { |
| 1809 | if (*bufs == NULL) { |
| 1810 | if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0) |
| 1811 | return err; |
| 1812 | } else { |
| 1813 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 1814 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 1815 | return err; |
| 1816 | } |
| 1817 | } |
| 1818 | } else { |
| 1819 | /* default transfer behaviour */ |
| 1820 | size_t dma_csize = runtime->dma_bytes / channels; |
| 1821 | snd_assert(runtime->dma_area, return -EFAULT); |
| 1822 | for (c = 0; c < channels; ++c, ++bufs) { |
| 1823 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 1824 | if (*bufs == NULL) { |
| 1825 | snd_pcm_format_set_silence(runtime->format, hwbuf, frames); |
| 1826 | } else { |
| 1827 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 1828 | if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames))) |
| 1829 | return -EFAULT; |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1836 | 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] | 1837 | void __user **bufs, |
| 1838 | snd_pcm_uframes_t frames) |
| 1839 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1840 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | int nonblock; |
| 1842 | |
| 1843 | snd_assert(substream != NULL, return -ENXIO); |
| 1844 | runtime = substream->runtime; |
| 1845 | snd_assert(runtime != NULL, return -ENXIO); |
| 1846 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 1847 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 1848 | return -EBADFD; |
| 1849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
| 1852 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 1853 | return -EINVAL; |
| 1854 | return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames, |
| 1855 | nonblock, snd_pcm_lib_writev_transfer); |
| 1856 | } |
| 1857 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1858 | EXPORT_SYMBOL(snd_pcm_lib_writev); |
| 1859 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1860 | static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | unsigned int hwoff, |
| 1862 | unsigned long data, unsigned int off, |
| 1863 | snd_pcm_uframes_t frames) |
| 1864 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1865 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | int err; |
| 1867 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 1868 | if (substream->ops->copy) { |
| 1869 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 1870 | return err; |
| 1871 | } else { |
| 1872 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 1873 | snd_assert(runtime->dma_area, return -EFAULT); |
| 1874 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) |
| 1875 | return -EFAULT; |
| 1876 | } |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1880 | 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] | 1881 | unsigned long data, |
| 1882 | snd_pcm_uframes_t size, |
| 1883 | int nonblock, |
| 1884 | transfer_f transfer) |
| 1885 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1886 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | snd_pcm_uframes_t xfer = 0; |
| 1888 | snd_pcm_uframes_t offset = 0; |
| 1889 | int err = 0; |
| 1890 | |
| 1891 | if (size == 0) |
| 1892 | return 0; |
| 1893 | if (size > runtime->xfer_align) |
| 1894 | size -= size % runtime->xfer_align; |
| 1895 | |
| 1896 | snd_pcm_stream_lock_irq(substream); |
| 1897 | switch (runtime->status->state) { |
| 1898 | case SNDRV_PCM_STATE_PREPARED: |
| 1899 | if (size >= runtime->start_threshold) { |
| 1900 | err = snd_pcm_start(substream); |
| 1901 | if (err < 0) |
| 1902 | goto _end_unlock; |
| 1903 | } |
| 1904 | break; |
| 1905 | case SNDRV_PCM_STATE_DRAINING: |
| 1906 | case SNDRV_PCM_STATE_RUNNING: |
| 1907 | case SNDRV_PCM_STATE_PAUSED: |
| 1908 | break; |
| 1909 | case SNDRV_PCM_STATE_XRUN: |
| 1910 | err = -EPIPE; |
| 1911 | goto _end_unlock; |
| 1912 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1913 | err = -ESTRPIPE; |
| 1914 | goto _end_unlock; |
| 1915 | default: |
| 1916 | err = -EBADFD; |
| 1917 | goto _end_unlock; |
| 1918 | } |
| 1919 | |
| 1920 | while (size > 0) { |
| 1921 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 1922 | snd_pcm_uframes_t avail; |
| 1923 | snd_pcm_uframes_t cont; |
| 1924 | if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 1925 | snd_pcm_update_hw_ptr(substream); |
| 1926 | __draining: |
| 1927 | avail = snd_pcm_capture_avail(runtime); |
| 1928 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
| 1929 | if (avail < runtime->xfer_align) { |
| 1930 | err = -EPIPE; |
| 1931 | goto _end_unlock; |
| 1932 | } |
| 1933 | } else if ((avail < runtime->control->avail_min && size > avail) || |
| 1934 | (size >= runtime->xfer_align && avail < runtime->xfer_align)) { |
| 1935 | wait_queue_t wait; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 1936 | enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | long tout; |
| 1938 | |
| 1939 | if (nonblock) { |
| 1940 | err = -EAGAIN; |
| 1941 | goto _end_unlock; |
| 1942 | } |
| 1943 | |
| 1944 | init_waitqueue_entry(&wait, current); |
| 1945 | add_wait_queue(&runtime->sleep, &wait); |
| 1946 | while (1) { |
| 1947 | if (signal_pending(current)) { |
| 1948 | state = SIGNALED; |
| 1949 | break; |
| 1950 | } |
| 1951 | set_current_state(TASK_INTERRUPTIBLE); |
| 1952 | snd_pcm_stream_unlock_irq(substream); |
| 1953 | tout = schedule_timeout(10 * HZ); |
| 1954 | snd_pcm_stream_lock_irq(substream); |
| 1955 | if (tout == 0) { |
| 1956 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED && |
| 1957 | runtime->status->state != SNDRV_PCM_STATE_PAUSED) { |
| 1958 | state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED; |
| 1959 | break; |
| 1960 | } |
| 1961 | } |
| 1962 | switch (runtime->status->state) { |
| 1963 | case SNDRV_PCM_STATE_XRUN: |
| 1964 | state = ERROR; |
| 1965 | goto _end_loop; |
| 1966 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1967 | state = SUSPENDED; |
| 1968 | goto _end_loop; |
| 1969 | case SNDRV_PCM_STATE_DRAINING: |
| 1970 | goto __draining; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 1971 | case SNDRV_PCM_STATE_SETUP: |
| 1972 | state = DROPPED; |
| 1973 | goto _end_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | default: |
| 1975 | break; |
| 1976 | } |
| 1977 | avail = snd_pcm_capture_avail(runtime); |
| 1978 | if (avail >= runtime->control->avail_min) { |
| 1979 | state = READY; |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | _end_loop: |
| 1984 | remove_wait_queue(&runtime->sleep, &wait); |
| 1985 | |
| 1986 | switch (state) { |
| 1987 | case ERROR: |
| 1988 | err = -EPIPE; |
| 1989 | goto _end_unlock; |
| 1990 | case SUSPENDED: |
| 1991 | err = -ESTRPIPE; |
| 1992 | goto _end_unlock; |
| 1993 | case SIGNALED: |
| 1994 | err = -ERESTARTSYS; |
| 1995 | goto _end_unlock; |
| 1996 | case EXPIRED: |
| 1997 | snd_printd("capture read error (DMA or IRQ trouble?)\n"); |
| 1998 | err = -EIO; |
| 1999 | goto _end_unlock; |
Karsten Wiese | 443feb8 | 2005-08-10 11:18:19 +0200 | [diff] [blame] | 2000 | case DROPPED: |
| 2001 | err = -EBADFD; |
| 2002 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | default: |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | if (avail > runtime->xfer_align) |
| 2008 | avail -= avail % runtime->xfer_align; |
| 2009 | frames = size > avail ? avail : size; |
| 2010 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 2011 | if (frames > cont) |
| 2012 | frames = cont; |
| 2013 | snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL); |
| 2014 | appl_ptr = runtime->control->appl_ptr; |
| 2015 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 2016 | snd_pcm_stream_unlock_irq(substream); |
| 2017 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 2018 | goto _end; |
| 2019 | snd_pcm_stream_lock_irq(substream); |
| 2020 | switch (runtime->status->state) { |
| 2021 | case SNDRV_PCM_STATE_XRUN: |
| 2022 | err = -EPIPE; |
| 2023 | goto _end_unlock; |
| 2024 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2025 | err = -ESTRPIPE; |
| 2026 | goto _end_unlock; |
| 2027 | default: |
| 2028 | break; |
| 2029 | } |
| 2030 | appl_ptr += frames; |
| 2031 | if (appl_ptr >= runtime->boundary) |
| 2032 | appl_ptr -= runtime->boundary; |
| 2033 | runtime->control->appl_ptr = appl_ptr; |
| 2034 | if (substream->ops->ack) |
| 2035 | substream->ops->ack(substream); |
| 2036 | |
| 2037 | offset += frames; |
| 2038 | size -= frames; |
| 2039 | xfer += frames; |
| 2040 | if (runtime->sleep_min && |
| 2041 | runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2042 | snd_pcm_tick_prepare(substream); |
| 2043 | } |
| 2044 | _end_unlock: |
| 2045 | snd_pcm_stream_unlock_irq(substream); |
| 2046 | _end: |
| 2047 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 2048 | } |
| 2049 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2050 | 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] | 2051 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2052 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | int nonblock; |
| 2054 | |
| 2055 | snd_assert(substream != NULL, return -ENXIO); |
| 2056 | runtime = substream->runtime; |
| 2057 | snd_assert(runtime != NULL, return -ENXIO); |
| 2058 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2059 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2060 | return -EBADFD; |
| 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
| 2064 | return -EINVAL; |
| 2065 | return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer); |
| 2066 | } |
| 2067 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2068 | EXPORT_SYMBOL(snd_pcm_lib_read); |
| 2069 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2070 | static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | unsigned int hwoff, |
| 2072 | unsigned long data, unsigned int off, |
| 2073 | snd_pcm_uframes_t frames) |
| 2074 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2075 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | int err; |
| 2077 | void __user **bufs = (void __user **)data; |
| 2078 | int channels = runtime->channels; |
| 2079 | int c; |
| 2080 | if (substream->ops->copy) { |
| 2081 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2082 | char __user *buf; |
| 2083 | if (*bufs == NULL) |
| 2084 | continue; |
| 2085 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2086 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2087 | return err; |
| 2088 | } |
| 2089 | } else { |
| 2090 | snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels; |
| 2091 | snd_assert(runtime->dma_area, return -EFAULT); |
| 2092 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2093 | char *hwbuf; |
| 2094 | char __user *buf; |
| 2095 | if (*bufs == NULL) |
| 2096 | continue; |
| 2097 | |
| 2098 | hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2099 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2100 | if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames))) |
| 2101 | return -EFAULT; |
| 2102 | } |
| 2103 | } |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2107 | 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] | 2108 | void __user **bufs, |
| 2109 | snd_pcm_uframes_t frames) |
| 2110 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2111 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | int nonblock; |
| 2113 | |
| 2114 | snd_assert(substream != NULL, return -ENXIO); |
| 2115 | runtime = substream->runtime; |
| 2116 | snd_assert(runtime != NULL, return -ENXIO); |
| 2117 | snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL); |
| 2118 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2119 | return -EBADFD; |
| 2120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | nonblock = !!(substream->ffile->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2123 | return -EINVAL; |
| 2124 | return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer); |
| 2125 | } |
| 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | EXPORT_SYMBOL(snd_pcm_lib_readv); |