Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Digital Audio (PCM) abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/control.h> |
| 27 | #include <sound/info.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/timer.h> |
| 31 | |
| 32 | /* |
| 33 | * fill ring buffer with silence |
| 34 | * runtime->silence_start: starting pointer to silence area |
| 35 | * runtime->silence_filled: size filled with silence |
| 36 | * runtime->silence_threshold: threshold from application |
| 37 | * runtime->silence_size: maximal size from application |
| 38 | * |
| 39 | * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately |
| 40 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 41 | 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] | 42 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 43 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | snd_pcm_uframes_t frames, ofs, transfer; |
| 45 | |
| 46 | if (runtime->silence_size < runtime->boundary) { |
| 47 | snd_pcm_sframes_t noise_dist, n; |
| 48 | if (runtime->silence_start != runtime->control->appl_ptr) { |
| 49 | n = runtime->control->appl_ptr - runtime->silence_start; |
| 50 | if (n < 0) |
| 51 | n += runtime->boundary; |
| 52 | if ((snd_pcm_uframes_t)n < runtime->silence_filled) |
| 53 | runtime->silence_filled -= n; |
| 54 | else |
| 55 | runtime->silence_filled = 0; |
| 56 | runtime->silence_start = runtime->control->appl_ptr; |
| 57 | } |
Takashi Iwai | 235475c | 2005-12-07 15:28:07 +0100 | [diff] [blame] | 58 | if (runtime->silence_filled >= runtime->buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
| 61 | if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) |
| 62 | return; |
| 63 | frames = runtime->silence_threshold - noise_dist; |
| 64 | if (frames > runtime->silence_size) |
| 65 | frames = runtime->silence_size; |
| 66 | } else { |
| 67 | if (new_hw_ptr == ULONG_MAX) { /* initialization */ |
| 68 | snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); |
| 69 | runtime->silence_filled = avail > 0 ? avail : 0; |
| 70 | runtime->silence_start = (runtime->status->hw_ptr + |
| 71 | runtime->silence_filled) % |
| 72 | runtime->boundary; |
| 73 | } else { |
| 74 | ofs = runtime->status->hw_ptr; |
| 75 | frames = new_hw_ptr - ofs; |
| 76 | if ((snd_pcm_sframes_t)frames < 0) |
| 77 | frames += runtime->boundary; |
| 78 | runtime->silence_filled -= frames; |
| 79 | if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { |
| 80 | runtime->silence_filled = 0; |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 81 | runtime->silence_start = new_hw_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } else { |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 83 | runtime->silence_start = ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | frames = runtime->buffer_size - runtime->silence_filled; |
| 87 | } |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 88 | if (snd_BUG_ON(frames > runtime->buffer_size)) |
| 89 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (frames == 0) |
| 91 | return; |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 92 | ofs = runtime->silence_start % runtime->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | while (frames > 0) { |
| 94 | transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; |
| 95 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || |
| 96 | runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) { |
| 97 | if (substream->ops->silence) { |
| 98 | int err; |
| 99 | err = substream->ops->silence(substream, -1, ofs, transfer); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 100 | snd_BUG_ON(err < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } else { |
| 102 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs); |
| 103 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels); |
| 104 | } |
| 105 | } else { |
| 106 | unsigned int c; |
| 107 | unsigned int channels = runtime->channels; |
| 108 | if (substream->ops->silence) { |
| 109 | for (c = 0; c < channels; ++c) { |
| 110 | int err; |
| 111 | err = substream->ops->silence(substream, c, ofs, transfer); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 112 | snd_BUG_ON(err < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | } else { |
| 115 | size_t dma_csize = runtime->dma_bytes / channels; |
| 116 | for (c = 0; c < channels; ++c) { |
| 117 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs); |
| 118 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | runtime->silence_filled += transfer; |
| 123 | frames -= transfer; |
| 124 | ofs = 0; |
| 125 | } |
| 126 | } |
| 127 | |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
| 129 | #define xrun_debug(substream) ((substream)->pstr->xrun_debug) |
| 130 | #else |
| 131 | #define xrun_debug(substream) 0 |
| 132 | #endif |
| 133 | |
| 134 | #define dump_stack_on_xrun(substream) do { \ |
| 135 | if (xrun_debug(substream) > 1) \ |
| 136 | dump_stack(); \ |
| 137 | } while (0) |
| 138 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 139 | static void xrun(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 142 | if (xrun_debug(substream)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n", |
| 144 | substream->pcm->card->number, |
| 145 | substream->pcm->device, |
| 146 | substream->stream ? 'c' : 'p'); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 147 | dump_stack_on_xrun(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Takashi Iwai | 9820464 | 2009-03-19 09:59:21 +0100 | [diff] [blame] | 151 | static snd_pcm_uframes_t |
| 152 | snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream, |
| 153 | struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | snd_pcm_uframes_t pos; |
| 156 | |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 157 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) |
| 158 | snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | pos = substream->ops->pointer(substream); |
| 160 | if (pos == SNDRV_PCM_POS_XRUN) |
| 161 | return pos; /* XRUN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (pos >= runtime->buffer_size) { |
Takashi Iwai | 5f513e1 | 2009-03-19 10:01:47 +0100 | [diff] [blame] | 163 | if (printk_ratelimit()) { |
| 164 | snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, " |
| 165 | "buffer size = 0x%lx, period size = 0x%lx\n", |
| 166 | substream->stream, pos, runtime->buffer_size, |
| 167 | runtime->period_size); |
| 168 | } |
| 169 | pos = 0; |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | pos -= pos % runtime->min_align; |
| 172 | return pos; |
| 173 | } |
| 174 | |
Takashi Iwai | 9820464 | 2009-03-19 09:59:21 +0100 | [diff] [blame] | 175 | static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream, |
| 176 | struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | snd_pcm_uframes_t avail; |
| 179 | |
| 180 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 181 | avail = snd_pcm_playback_avail(runtime); |
| 182 | else |
| 183 | avail = snd_pcm_capture_avail(runtime); |
| 184 | if (avail > runtime->avail_max) |
| 185 | runtime->avail_max = avail; |
| 186 | if (avail >= runtime->stop_threshold) { |
| 187 | if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING) |
| 188 | snd_pcm_drain_done(substream); |
| 189 | else |
| 190 | xrun(substream); |
| 191 | return -EPIPE; |
| 192 | } |
| 193 | if (avail >= runtime->control->avail_min) |
| 194 | wake_up(&runtime->sleep); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 198 | #define hw_ptr_error(substream, fmt, args...) \ |
| 199 | do { \ |
| 200 | if (xrun_debug(substream)) { \ |
| 201 | if (printk_ratelimit()) { \ |
Takashi Iwai | cad377a | 2009-03-19 09:55:15 +0100 | [diff] [blame] | 202 | snd_printd("PCM: " fmt, ##args); \ |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 203 | } \ |
| 204 | dump_stack_on_xrun(substream); \ |
| 205 | } \ |
| 206 | } while (0) |
| 207 | |
Takashi Iwai | 9820464 | 2009-03-19 09:59:21 +0100 | [diff] [blame] | 208 | static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 210 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | snd_pcm_uframes_t pos; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 212 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base; |
| 213 | snd_pcm_sframes_t hdelta, delta; |
| 214 | unsigned long jdelta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 216 | old_hw_ptr = runtime->status->hw_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); |
| 218 | if (pos == SNDRV_PCM_POS_XRUN) { |
| 219 | xrun(substream); |
| 220 | return -EPIPE; |
| 221 | } |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 222 | hw_base = runtime->hw_ptr_base; |
| 223 | new_hw_ptr = hw_base + pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size; |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 225 | delta = new_hw_ptr - hw_ptr_interrupt; |
Takashi Iwai | ded652f | 2009-03-19 10:08:49 +0100 | [diff] [blame] | 226 | if (hw_ptr_interrupt >= runtime->boundary) { |
Takashi Iwai | 8b22d94 | 2009-03-20 16:26:15 +0100 | [diff] [blame] | 227 | hw_ptr_interrupt -= runtime->boundary; |
| 228 | if (hw_base < runtime->boundary / 2) |
| 229 | /* hw_base was already lapped; recalc delta */ |
Takashi Iwai | ded652f | 2009-03-19 10:08:49 +0100 | [diff] [blame] | 230 | delta = new_hw_ptr - hw_ptr_interrupt; |
| 231 | } |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 232 | if (delta < 0) { |
| 233 | delta += runtime->buffer_size; |
| 234 | if (delta < 0) { |
| 235 | hw_ptr_error(substream, |
| 236 | "Unexpected hw_pointer value " |
| 237 | "(stream=%i, pos=%ld, intr_ptr=%ld)\n", |
| 238 | substream->stream, (long)pos, |
| 239 | (long)hw_ptr_interrupt); |
| 240 | /* rebase to interrupt position */ |
| 241 | hw_base = new_hw_ptr = hw_ptr_interrupt; |
Takashi Iwai | ded652f | 2009-03-19 10:08:49 +0100 | [diff] [blame] | 242 | /* align hw_base to buffer_size */ |
| 243 | hw_base -= hw_base % runtime->buffer_size; |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 244 | delta = 0; |
| 245 | } else { |
| 246 | hw_base += runtime->buffer_size; |
Takashi Iwai | 8b22d94 | 2009-03-20 16:26:15 +0100 | [diff] [blame] | 247 | if (hw_base >= runtime->boundary) |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 248 | hw_base = 0; |
| 249 | new_hw_ptr = hw_base + pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 252 | hdelta = new_hw_ptr - old_hw_ptr; |
| 253 | jdelta = jiffies - runtime->hw_ptr_jiffies; |
| 254 | if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) { |
| 255 | delta = jdelta / |
| 256 | (((runtime->period_size * HZ) / runtime->rate) |
| 257 | + HZ/100); |
| 258 | hw_ptr_error(substream, |
| 259 | "hw_ptr skipping! [Q] " |
| 260 | "(pos=%ld, delta=%ld, period=%ld, " |
| 261 | "jdelta=%lu/%lu/%lu)\n", |
| 262 | (long)pos, (long)hdelta, |
| 263 | (long)runtime->period_size, jdelta, |
| 264 | ((hdelta * HZ) / runtime->rate), delta); |
| 265 | hw_ptr_interrupt = runtime->hw_ptr_interrupt + |
| 266 | runtime->period_size * delta; |
| 267 | if (hw_ptr_interrupt >= runtime->boundary) |
| 268 | hw_ptr_interrupt -= runtime->boundary; |
| 269 | /* rebase to interrupt position */ |
| 270 | hw_base = new_hw_ptr = hw_ptr_interrupt; |
| 271 | /* align hw_base to buffer_size */ |
| 272 | hw_base -= hw_base % runtime->buffer_size; |
| 273 | delta = 0; |
| 274 | } |
| 275 | if (delta > runtime->period_size + runtime->period_size / 2) { |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 276 | hw_ptr_error(substream, |
| 277 | "Lost interrupts? " |
| 278 | "(stream=%i, delta=%ld, intr_ptr=%ld)\n", |
| 279 | substream->stream, (long)delta, |
| 280 | (long)hw_ptr_interrupt); |
| 281 | /* rebase hw_ptr_interrupt */ |
| 282 | hw_ptr_interrupt = |
| 283 | new_hw_ptr - new_hw_ptr % runtime->period_size; |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 286 | runtime->silence_size > 0) |
| 287 | snd_pcm_playback_silence(substream, new_hw_ptr); |
| 288 | |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 289 | runtime->hw_ptr_base = hw_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | runtime->status->hw_ptr = new_hw_ptr; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 291 | runtime->hw_ptr_jiffies = jiffies; |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 292 | runtime->hw_ptr_interrupt = hw_ptr_interrupt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | return snd_pcm_update_hw_ptr_post(substream, runtime); |
| 295 | } |
| 296 | |
| 297 | /* CAUTION: call it with irq disabled */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 298 | int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 300 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | snd_pcm_uframes_t pos; |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 302 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | snd_pcm_sframes_t delta; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 304 | unsigned long jdelta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | old_hw_ptr = runtime->status->hw_ptr; |
| 307 | pos = snd_pcm_update_hw_ptr_pos(substream, runtime); |
| 308 | if (pos == SNDRV_PCM_POS_XRUN) { |
| 309 | xrun(substream); |
| 310 | return -EPIPE; |
| 311 | } |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 312 | hw_base = runtime->hw_ptr_base; |
| 313 | new_hw_ptr = hw_base + pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 315 | delta = new_hw_ptr - old_hw_ptr; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 316 | jdelta = jiffies - runtime->hw_ptr_jiffies; |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 317 | if (delta < 0) { |
| 318 | delta += runtime->buffer_size; |
| 319 | if (delta < 0) { |
| 320 | hw_ptr_error(substream, |
| 321 | "Unexpected hw_pointer value [2] " |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 322 | "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n", |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 323 | substream->stream, (long)pos, |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 324 | (long)old_hw_ptr, jdelta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 327 | hw_base += runtime->buffer_size; |
Takashi Iwai | 8b22d94 | 2009-03-20 16:26:15 +0100 | [diff] [blame] | 328 | if (hw_base >= runtime->boundary) |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 329 | hw_base = 0; |
| 330 | new_hw_ptr = hw_base + pos; |
| 331 | } |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 332 | if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) { |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 333 | hw_ptr_error(substream, |
| 334 | "hw_ptr skipping! " |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 335 | "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n", |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 336 | (long)pos, (long)delta, |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 337 | (long)runtime->period_size, jdelta, |
| 338 | ((delta * HZ) / runtime->rate)); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 339 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 342 | runtime->silence_size > 0) |
| 343 | snd_pcm_playback_silence(substream, new_hw_ptr); |
| 344 | |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 345 | runtime->hw_ptr_base = hw_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | runtime->status->hw_ptr = new_hw_ptr; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 347 | runtime->hw_ptr_jiffies = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | return snd_pcm_update_hw_ptr_post(substream, runtime); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * snd_pcm_set_ops - set the PCM operators |
| 354 | * @pcm: the pcm instance |
| 355 | * @direction: stream direction, SNDRV_PCM_STREAM_XXX |
| 356 | * @ops: the operator table |
| 357 | * |
| 358 | * Sets the given PCM operators to the pcm instance. |
| 359 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 360 | 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] | 361 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 362 | struct snd_pcm_str *stream = &pcm->streams[direction]; |
| 363 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | for (substream = stream->substream; substream != NULL; substream = substream->next) |
| 366 | substream->ops = ops; |
| 367 | } |
| 368 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 369 | EXPORT_SYMBOL(snd_pcm_set_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * snd_pcm_sync - set the PCM sync id |
| 373 | * @substream: the pcm substream |
| 374 | * |
| 375 | * Sets the PCM sync identifier for the card. |
| 376 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 377 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 379 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | runtime->sync.id32[0] = substream->pcm->card->number; |
| 382 | runtime->sync.id32[1] = -1; |
| 383 | runtime->sync.id32[2] = -1; |
| 384 | runtime->sync.id32[3] = -1; |
| 385 | } |
| 386 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 387 | EXPORT_SYMBOL(snd_pcm_set_sync); |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Standard ioctl routine |
| 391 | */ |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | static inline unsigned int div32(unsigned int a, unsigned int b, |
| 394 | unsigned int *r) |
| 395 | { |
| 396 | if (b == 0) { |
| 397 | *r = 0; |
| 398 | return UINT_MAX; |
| 399 | } |
| 400 | *r = a % b; |
| 401 | return a / b; |
| 402 | } |
| 403 | |
| 404 | static inline unsigned int div_down(unsigned int a, unsigned int b) |
| 405 | { |
| 406 | if (b == 0) |
| 407 | return UINT_MAX; |
| 408 | return a / b; |
| 409 | } |
| 410 | |
| 411 | static inline unsigned int div_up(unsigned int a, unsigned int b) |
| 412 | { |
| 413 | unsigned int r; |
| 414 | unsigned int q; |
| 415 | if (b == 0) |
| 416 | return UINT_MAX; |
| 417 | q = div32(a, b, &r); |
| 418 | if (r) |
| 419 | ++q; |
| 420 | return q; |
| 421 | } |
| 422 | |
| 423 | static inline unsigned int mul(unsigned int a, unsigned int b) |
| 424 | { |
| 425 | if (a == 0) |
| 426 | return 0; |
| 427 | if (div_down(UINT_MAX, a) < b) |
| 428 | return UINT_MAX; |
| 429 | return a * b; |
| 430 | } |
| 431 | |
| 432 | static inline unsigned int muldiv32(unsigned int a, unsigned int b, |
| 433 | unsigned int c, unsigned int *r) |
| 434 | { |
| 435 | u_int64_t n = (u_int64_t) a * b; |
| 436 | if (c == 0) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 437 | snd_BUG_ON(!n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | *r = 0; |
| 439 | return UINT_MAX; |
| 440 | } |
| 441 | div64_32(&n, c, r); |
| 442 | if (n >= UINT_MAX) { |
| 443 | *r = 0; |
| 444 | return UINT_MAX; |
| 445 | } |
| 446 | return n; |
| 447 | } |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | /** |
| 450 | * snd_interval_refine - refine the interval value of configurator |
| 451 | * @i: the interval value to refine |
| 452 | * @v: the interval value to refer to |
| 453 | * |
| 454 | * Refines the interval value with the reference value. |
| 455 | * The interval is changed to the range satisfying both intervals. |
| 456 | * The interval status (min, max, integer, etc.) are evaluated. |
| 457 | * |
| 458 | * Returns non-zero if the value is changed, zero if not changed. |
| 459 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 460 | 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] | 461 | { |
| 462 | int changed = 0; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 463 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 464 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (i->min < v->min) { |
| 466 | i->min = v->min; |
| 467 | i->openmin = v->openmin; |
| 468 | changed = 1; |
| 469 | } else if (i->min == v->min && !i->openmin && v->openmin) { |
| 470 | i->openmin = 1; |
| 471 | changed = 1; |
| 472 | } |
| 473 | if (i->max > v->max) { |
| 474 | i->max = v->max; |
| 475 | i->openmax = v->openmax; |
| 476 | changed = 1; |
| 477 | } else if (i->max == v->max && !i->openmax && v->openmax) { |
| 478 | i->openmax = 1; |
| 479 | changed = 1; |
| 480 | } |
| 481 | if (!i->integer && v->integer) { |
| 482 | i->integer = 1; |
| 483 | changed = 1; |
| 484 | } |
| 485 | if (i->integer) { |
| 486 | if (i->openmin) { |
| 487 | i->min++; |
| 488 | i->openmin = 0; |
| 489 | } |
| 490 | if (i->openmax) { |
| 491 | i->max--; |
| 492 | i->openmax = 0; |
| 493 | } |
| 494 | } else if (!i->openmin && !i->openmax && i->min == i->max) |
| 495 | i->integer = 1; |
| 496 | if (snd_interval_checkempty(i)) { |
| 497 | snd_interval_none(i); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | return changed; |
| 501 | } |
| 502 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 503 | EXPORT_SYMBOL(snd_interval_refine); |
| 504 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 505 | static int snd_interval_refine_first(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 507 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 508 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | if (snd_interval_single(i)) |
| 510 | return 0; |
| 511 | i->max = i->min; |
| 512 | i->openmax = i->openmin; |
| 513 | if (i->openmax) |
| 514 | i->max++; |
| 515 | return 1; |
| 516 | } |
| 517 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 518 | static int snd_interval_refine_last(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 520 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 521 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (snd_interval_single(i)) |
| 523 | return 0; |
| 524 | i->min = i->max; |
| 525 | i->openmin = i->openmax; |
| 526 | if (i->openmin) |
| 527 | i->min--; |
| 528 | return 1; |
| 529 | } |
| 530 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 531 | 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] | 532 | { |
| 533 | if (a->empty || b->empty) { |
| 534 | snd_interval_none(c); |
| 535 | return; |
| 536 | } |
| 537 | c->empty = 0; |
| 538 | c->min = mul(a->min, b->min); |
| 539 | c->openmin = (a->openmin || b->openmin); |
| 540 | c->max = mul(a->max, b->max); |
| 541 | c->openmax = (a->openmax || b->openmax); |
| 542 | c->integer = (a->integer && b->integer); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * snd_interval_div - refine the interval value with division |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 547 | * @a: dividend |
| 548 | * @b: divisor |
| 549 | * @c: quotient |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | * |
| 551 | * c = a / b |
| 552 | * |
| 553 | * Returns non-zero if the value is changed, zero if not changed. |
| 554 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 555 | 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] | 556 | { |
| 557 | unsigned int r; |
| 558 | if (a->empty || b->empty) { |
| 559 | snd_interval_none(c); |
| 560 | return; |
| 561 | } |
| 562 | c->empty = 0; |
| 563 | c->min = div32(a->min, b->max, &r); |
| 564 | c->openmin = (r || a->openmin || b->openmax); |
| 565 | if (b->min > 0) { |
| 566 | c->max = div32(a->max, b->min, &r); |
| 567 | if (r) { |
| 568 | c->max++; |
| 569 | c->openmax = 1; |
| 570 | } else |
| 571 | c->openmax = (a->openmax || b->openmin); |
| 572 | } else { |
| 573 | c->max = UINT_MAX; |
| 574 | c->openmax = 0; |
| 575 | } |
| 576 | c->integer = 0; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * snd_interval_muldivk - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 581 | * @a: dividend 1 |
| 582 | * @b: dividend 2 |
| 583 | * @k: divisor (as integer) |
| 584 | * @c: result |
| 585 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | * c = a * b / k |
| 587 | * |
| 588 | * Returns non-zero if the value is changed, zero if not changed. |
| 589 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 590 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
| 591 | unsigned int k, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
| 593 | unsigned int r; |
| 594 | if (a->empty || b->empty) { |
| 595 | snd_interval_none(c); |
| 596 | return; |
| 597 | } |
| 598 | c->empty = 0; |
| 599 | c->min = muldiv32(a->min, b->min, k, &r); |
| 600 | c->openmin = (r || a->openmin || b->openmin); |
| 601 | c->max = muldiv32(a->max, b->max, k, &r); |
| 602 | if (r) { |
| 603 | c->max++; |
| 604 | c->openmax = 1; |
| 605 | } else |
| 606 | c->openmax = (a->openmax || b->openmax); |
| 607 | c->integer = 0; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * snd_interval_mulkdiv - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 612 | * @a: dividend 1 |
| 613 | * @k: dividend 2 (as integer) |
| 614 | * @b: divisor |
| 615 | * @c: result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | * |
| 617 | * c = a * k / b |
| 618 | * |
| 619 | * Returns non-zero if the value is changed, zero if not changed. |
| 620 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 621 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
| 622 | const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | unsigned int r; |
| 625 | if (a->empty || b->empty) { |
| 626 | snd_interval_none(c); |
| 627 | return; |
| 628 | } |
| 629 | c->empty = 0; |
| 630 | c->min = muldiv32(a->min, k, b->max, &r); |
| 631 | c->openmin = (r || a->openmin || b->openmax); |
| 632 | if (b->min > 0) { |
| 633 | c->max = muldiv32(a->max, k, b->min, &r); |
| 634 | if (r) { |
| 635 | c->max++; |
| 636 | c->openmax = 1; |
| 637 | } else |
| 638 | c->openmax = (a->openmax || b->openmin); |
| 639 | } else { |
| 640 | c->max = UINT_MAX; |
| 641 | c->openmax = 0; |
| 642 | } |
| 643 | c->integer = 0; |
| 644 | } |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* ---- */ |
| 647 | |
| 648 | |
| 649 | /** |
| 650 | * snd_interval_ratnum - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 651 | * @i: interval to refine |
| 652 | * @rats_count: number of ratnum_t |
| 653 | * @rats: ratnum_t array |
| 654 | * @nump: pointer to store the resultant numerator |
| 655 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | * |
| 657 | * Returns non-zero if the value is changed, zero if not changed. |
| 658 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 659 | int snd_interval_ratnum(struct snd_interval *i, |
| 660 | unsigned int rats_count, struct snd_ratnum *rats, |
| 661 | unsigned int *nump, unsigned int *denp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | unsigned int best_num, best_diff, best_den; |
| 664 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 665 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | int err; |
| 667 | |
| 668 | best_num = best_den = best_diff = 0; |
| 669 | for (k = 0; k < rats_count; ++k) { |
| 670 | unsigned int num = rats[k].num; |
| 671 | unsigned int den; |
| 672 | unsigned int q = i->min; |
| 673 | int diff; |
| 674 | if (q == 0) |
| 675 | q = 1; |
| 676 | den = div_down(num, q); |
| 677 | if (den < rats[k].den_min) |
| 678 | continue; |
| 679 | if (den > rats[k].den_max) |
| 680 | den = rats[k].den_max; |
| 681 | else { |
| 682 | unsigned int r; |
| 683 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 684 | if (r != 0) |
| 685 | den -= r; |
| 686 | } |
| 687 | diff = num - q * den; |
| 688 | if (best_num == 0 || |
| 689 | diff * best_den < best_diff * den) { |
| 690 | best_diff = diff; |
| 691 | best_den = den; |
| 692 | best_num = num; |
| 693 | } |
| 694 | } |
| 695 | if (best_den == 0) { |
| 696 | i->empty = 1; |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | t.min = div_down(best_num, best_den); |
| 700 | t.openmin = !!(best_num % best_den); |
| 701 | |
| 702 | best_num = best_den = best_diff = 0; |
| 703 | for (k = 0; k < rats_count; ++k) { |
| 704 | unsigned int num = rats[k].num; |
| 705 | unsigned int den; |
| 706 | unsigned int q = i->max; |
| 707 | int diff; |
| 708 | if (q == 0) { |
| 709 | i->empty = 1; |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | den = div_up(num, q); |
| 713 | if (den > rats[k].den_max) |
| 714 | continue; |
| 715 | if (den < rats[k].den_min) |
| 716 | den = rats[k].den_min; |
| 717 | else { |
| 718 | unsigned int r; |
| 719 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 720 | if (r != 0) |
| 721 | den += rats[k].den_step - r; |
| 722 | } |
| 723 | diff = q * den - num; |
| 724 | if (best_num == 0 || |
| 725 | diff * best_den < best_diff * den) { |
| 726 | best_diff = diff; |
| 727 | best_den = den; |
| 728 | best_num = num; |
| 729 | } |
| 730 | } |
| 731 | if (best_den == 0) { |
| 732 | i->empty = 1; |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | t.max = div_up(best_num, best_den); |
| 736 | t.openmax = !!(best_num % best_den); |
| 737 | t.integer = 0; |
| 738 | err = snd_interval_refine(i, &t); |
| 739 | if (err < 0) |
| 740 | return err; |
| 741 | |
| 742 | if (snd_interval_single(i)) { |
| 743 | if (nump) |
| 744 | *nump = best_num; |
| 745 | if (denp) |
| 746 | *denp = best_den; |
| 747 | } |
| 748 | return err; |
| 749 | } |
| 750 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 751 | EXPORT_SYMBOL(snd_interval_ratnum); |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | /** |
| 754 | * snd_interval_ratden - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 755 | * @i: interval to refine |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 756 | * @rats_count: number of struct ratden |
| 757 | * @rats: struct ratden array |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 758 | * @nump: pointer to store the resultant numerator |
| 759 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * |
| 761 | * Returns non-zero if the value is changed, zero if not changed. |
| 762 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 763 | static int snd_interval_ratden(struct snd_interval *i, |
| 764 | unsigned int rats_count, struct snd_ratden *rats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | unsigned int *nump, unsigned int *denp) |
| 766 | { |
| 767 | unsigned int best_num, best_diff, best_den; |
| 768 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 769 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | int err; |
| 771 | |
| 772 | best_num = best_den = best_diff = 0; |
| 773 | for (k = 0; k < rats_count; ++k) { |
| 774 | unsigned int num; |
| 775 | unsigned int den = rats[k].den; |
| 776 | unsigned int q = i->min; |
| 777 | int diff; |
| 778 | num = mul(q, den); |
| 779 | if (num > rats[k].num_max) |
| 780 | continue; |
| 781 | if (num < rats[k].num_min) |
| 782 | num = rats[k].num_max; |
| 783 | else { |
| 784 | unsigned int r; |
| 785 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 786 | if (r != 0) |
| 787 | num += rats[k].num_step - r; |
| 788 | } |
| 789 | diff = num - q * den; |
| 790 | if (best_num == 0 || |
| 791 | diff * best_den < best_diff * den) { |
| 792 | best_diff = diff; |
| 793 | best_den = den; |
| 794 | best_num = num; |
| 795 | } |
| 796 | } |
| 797 | if (best_den == 0) { |
| 798 | i->empty = 1; |
| 799 | return -EINVAL; |
| 800 | } |
| 801 | t.min = div_down(best_num, best_den); |
| 802 | t.openmin = !!(best_num % best_den); |
| 803 | |
| 804 | best_num = best_den = best_diff = 0; |
| 805 | for (k = 0; k < rats_count; ++k) { |
| 806 | unsigned int num; |
| 807 | unsigned int den = rats[k].den; |
| 808 | unsigned int q = i->max; |
| 809 | int diff; |
| 810 | num = mul(q, den); |
| 811 | if (num < rats[k].num_min) |
| 812 | continue; |
| 813 | if (num > rats[k].num_max) |
| 814 | num = rats[k].num_max; |
| 815 | else { |
| 816 | unsigned int r; |
| 817 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 818 | if (r != 0) |
| 819 | num -= r; |
| 820 | } |
| 821 | diff = q * den - num; |
| 822 | if (best_num == 0 || |
| 823 | diff * best_den < best_diff * den) { |
| 824 | best_diff = diff; |
| 825 | best_den = den; |
| 826 | best_num = num; |
| 827 | } |
| 828 | } |
| 829 | if (best_den == 0) { |
| 830 | i->empty = 1; |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | t.max = div_up(best_num, best_den); |
| 834 | t.openmax = !!(best_num % best_den); |
| 835 | t.integer = 0; |
| 836 | err = snd_interval_refine(i, &t); |
| 837 | if (err < 0) |
| 838 | return err; |
| 839 | |
| 840 | if (snd_interval_single(i)) { |
| 841 | if (nump) |
| 842 | *nump = best_num; |
| 843 | if (denp) |
| 844 | *denp = best_den; |
| 845 | } |
| 846 | return err; |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * snd_interval_list - refine the interval value from the list |
| 851 | * @i: the interval value to refine |
| 852 | * @count: the number of elements in the list |
| 853 | * @list: the value list |
| 854 | * @mask: the bit-mask to evaluate |
| 855 | * |
| 856 | * Refines the interval value from the list. |
| 857 | * When mask is non-zero, only the elements corresponding to bit 1 are |
| 858 | * evaluated. |
| 859 | * |
| 860 | * Returns non-zero if the value is changed, zero if not changed. |
| 861 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 862 | 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] | 863 | { |
| 864 | unsigned int k; |
| 865 | int changed = 0; |
Takashi Iwai | 0981a26 | 2007-02-01 14:53:49 +0100 | [diff] [blame] | 866 | |
| 867 | if (!count) { |
| 868 | i->empty = 1; |
| 869 | return -EINVAL; |
| 870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | for (k = 0; k < count; k++) { |
| 872 | if (mask && !(mask & (1 << k))) |
| 873 | continue; |
| 874 | if (i->min == list[k] && !i->openmin) |
| 875 | goto _l1; |
| 876 | if (i->min < list[k]) { |
| 877 | i->min = list[k]; |
| 878 | i->openmin = 0; |
| 879 | changed = 1; |
| 880 | goto _l1; |
| 881 | } |
| 882 | } |
| 883 | i->empty = 1; |
| 884 | return -EINVAL; |
| 885 | _l1: |
| 886 | for (k = count; k-- > 0;) { |
| 887 | if (mask && !(mask & (1 << k))) |
| 888 | continue; |
| 889 | if (i->max == list[k] && !i->openmax) |
| 890 | goto _l2; |
| 891 | if (i->max > list[k]) { |
| 892 | i->max = list[k]; |
| 893 | i->openmax = 0; |
| 894 | changed = 1; |
| 895 | goto _l2; |
| 896 | } |
| 897 | } |
| 898 | i->empty = 1; |
| 899 | return -EINVAL; |
| 900 | _l2: |
| 901 | if (snd_interval_checkempty(i)) { |
| 902 | i->empty = 1; |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | return changed; |
| 906 | } |
| 907 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 908 | EXPORT_SYMBOL(snd_interval_list); |
| 909 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 910 | 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] | 911 | { |
| 912 | unsigned int n; |
| 913 | int changed = 0; |
| 914 | n = (i->min - min) % step; |
| 915 | if (n != 0 || i->openmin) { |
| 916 | i->min += step - n; |
| 917 | changed = 1; |
| 918 | } |
| 919 | n = (i->max - min) % step; |
| 920 | if (n != 0 || i->openmax) { |
| 921 | i->max -= n; |
| 922 | changed = 1; |
| 923 | } |
| 924 | if (snd_interval_checkempty(i)) { |
| 925 | i->empty = 1; |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | return changed; |
| 929 | } |
| 930 | |
| 931 | /* Info constraints helpers */ |
| 932 | |
| 933 | /** |
| 934 | * snd_pcm_hw_rule_add - add the hw-constraint rule |
| 935 | * @runtime: the pcm runtime instance |
| 936 | * @cond: condition bits |
| 937 | * @var: the variable to evaluate |
| 938 | * @func: the evaluation function |
| 939 | * @private: the private data pointer passed to function |
| 940 | * @dep: the dependent variables |
| 941 | * |
| 942 | * Returns zero if successful, or a negative error code on failure. |
| 943 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 944 | 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] | 945 | int var, |
| 946 | snd_pcm_hw_rule_func_t func, void *private, |
| 947 | int dep, ...) |
| 948 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 949 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 950 | struct snd_pcm_hw_rule *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | unsigned int k; |
| 952 | va_list args; |
| 953 | va_start(args, dep); |
| 954 | if (constrs->rules_num >= constrs->rules_all) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 955 | struct snd_pcm_hw_rule *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | unsigned int new_rules = constrs->rules_all + 16; |
| 957 | new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL); |
| 958 | if (!new) |
| 959 | return -ENOMEM; |
| 960 | if (constrs->rules) { |
| 961 | memcpy(new, constrs->rules, |
| 962 | constrs->rules_num * sizeof(*c)); |
| 963 | kfree(constrs->rules); |
| 964 | } |
| 965 | constrs->rules = new; |
| 966 | constrs->rules_all = new_rules; |
| 967 | } |
| 968 | c = &constrs->rules[constrs->rules_num]; |
| 969 | c->cond = cond; |
| 970 | c->func = func; |
| 971 | c->var = var; |
| 972 | c->private = private; |
| 973 | k = 0; |
| 974 | while (1) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 975 | if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) |
| 976 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | c->deps[k++] = dep; |
| 978 | if (dep < 0) |
| 979 | break; |
| 980 | dep = va_arg(args, int); |
| 981 | } |
| 982 | constrs->rules_num++; |
| 983 | va_end(args); |
| 984 | return 0; |
| 985 | } |
| 986 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 987 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
| 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 990 | * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 991 | * @runtime: PCM runtime instance |
| 992 | * @var: hw_params variable to apply the mask |
| 993 | * @mask: the bitmap mask |
| 994 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 995 | * Apply the constraint of the given bitmap mask to a 32-bit mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 997 | 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] | 998 | u_int32_t mask) |
| 999 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1000 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1001 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | *maskp->bits &= mask; |
| 1003 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ |
| 1004 | if (*maskp->bits == 0) |
| 1005 | return -EINVAL; |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1010 | * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1011 | * @runtime: PCM runtime instance |
| 1012 | * @var: hw_params variable to apply the mask |
| 1013 | * @mask: the 64bit bitmap mask |
| 1014 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1015 | * Apply the constraint of the given bitmap mask to a 64-bit mask parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1017 | 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] | 1018 | u_int64_t mask) |
| 1019 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1020 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1021 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | maskp->bits[0] &= (u_int32_t)mask; |
| 1023 | maskp->bits[1] &= (u_int32_t)(mask >> 32); |
| 1024 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ |
| 1025 | if (! maskp->bits[0] && ! maskp->bits[1]) |
| 1026 | return -EINVAL; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1031 | * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1032 | * @runtime: PCM runtime instance |
| 1033 | * @var: hw_params variable to apply the integer constraint |
| 1034 | * |
| 1035 | * Apply the constraint of integer to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1037 | 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] | 1038 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1039 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
| 1041 | } |
| 1042 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1043 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1046 | * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1047 | * @runtime: PCM runtime instance |
| 1048 | * @var: hw_params variable to apply the range |
| 1049 | * @min: the minimal value |
| 1050 | * @max: the maximal value |
| 1051 | * |
| 1052 | * Apply the min/max range constraint to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1054 | 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] | 1055 | unsigned int min, unsigned int max) |
| 1056 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1057 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1058 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | t.min = min; |
| 1060 | t.max = max; |
| 1061 | t.openmin = t.openmax = 0; |
| 1062 | t.integer = 0; |
| 1063 | return snd_interval_refine(constrs_interval(constrs, var), &t); |
| 1064 | } |
| 1065 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1066 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
| 1067 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1068 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
| 1069 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1071 | struct snd_pcm_hw_constraint_list *list = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1077 | * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1078 | * @runtime: PCM runtime instance |
| 1079 | * @cond: condition bits |
| 1080 | * @var: hw_params variable to apply the list constraint |
| 1081 | * @l: list |
| 1082 | * |
| 1083 | * Apply the list of constraints to an interval parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1085 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | unsigned int cond, |
| 1087 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1088 | struct snd_pcm_hw_constraint_list *l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
| 1090 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1091 | snd_pcm_hw_rule_list, l, |
| 1092 | var, -1); |
| 1093 | } |
| 1094 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1095 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
| 1096 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1097 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
| 1098 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1100 | struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | unsigned int num = 0, den = 0; |
| 1102 | int err; |
| 1103 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), |
| 1104 | r->nrats, r->rats, &num, &den); |
| 1105 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1106 | params->rate_num = num; |
| 1107 | params->rate_den = den; |
| 1108 | } |
| 1109 | return err; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1113 | * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1114 | * @runtime: PCM runtime instance |
| 1115 | * @cond: condition bits |
| 1116 | * @var: hw_params variable to apply the ratnums constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1117 | * @r: struct snd_ratnums constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1119 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | unsigned int cond, |
| 1121 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1122 | struct snd_pcm_hw_constraint_ratnums *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
| 1124 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1125 | snd_pcm_hw_rule_ratnums, r, |
| 1126 | var, -1); |
| 1127 | } |
| 1128 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1129 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
| 1130 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1131 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
| 1132 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1134 | struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | unsigned int num = 0, den = 0; |
| 1136 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), |
| 1137 | r->nrats, r->rats, &num, &den); |
| 1138 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1139 | params->rate_num = num; |
| 1140 | params->rate_den = den; |
| 1141 | } |
| 1142 | return err; |
| 1143 | } |
| 1144 | |
| 1145 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1146 | * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1147 | * @runtime: PCM runtime instance |
| 1148 | * @cond: condition bits |
| 1149 | * @var: hw_params variable to apply the ratdens constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1150 | * @r: struct snd_ratdens constriants |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1152 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | unsigned int cond, |
| 1154 | snd_pcm_hw_param_t var, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1155 | struct snd_pcm_hw_constraint_ratdens *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | { |
| 1157 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1158 | snd_pcm_hw_rule_ratdens, r, |
| 1159 | var, -1); |
| 1160 | } |
| 1161 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1162 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
| 1163 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1164 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
| 1165 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
| 1167 | unsigned int l = (unsigned long) rule->private; |
| 1168 | int width = l & 0xffff; |
| 1169 | unsigned int msbits = l >> 16; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1170 | 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] | 1171 | if (snd_interval_single(i) && snd_interval_value(i) == width) |
| 1172 | params->msbits = msbits; |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1177 | * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1178 | * @runtime: PCM runtime instance |
| 1179 | * @cond: condition bits |
| 1180 | * @width: sample bits width |
| 1181 | * @msbits: msbits width |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1183 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | unsigned int cond, |
| 1185 | unsigned int width, |
| 1186 | unsigned int msbits) |
| 1187 | { |
| 1188 | unsigned long l = (msbits << 16) | width; |
| 1189 | return snd_pcm_hw_rule_add(runtime, cond, -1, |
| 1190 | snd_pcm_hw_rule_msbits, |
| 1191 | (void*) l, |
| 1192 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1193 | } |
| 1194 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1195 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
| 1196 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1197 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
| 1198 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | { |
| 1200 | unsigned long step = (unsigned long) rule->private; |
| 1201 | return snd_interval_step(hw_param_interval(params, rule->var), 0, step); |
| 1202 | } |
| 1203 | |
| 1204 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1205 | * snd_pcm_hw_constraint_step - add a hw constraint step rule |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1206 | * @runtime: PCM runtime instance |
| 1207 | * @cond: condition bits |
| 1208 | * @var: hw_params variable to apply the step constraint |
| 1209 | * @step: step size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1211 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | unsigned int cond, |
| 1213 | snd_pcm_hw_param_t var, |
| 1214 | unsigned long step) |
| 1215 | { |
| 1216 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1217 | snd_pcm_hw_rule_step, (void *) step, |
| 1218 | var, -1); |
| 1219 | } |
| 1220 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1221 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
| 1222 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1223 | 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] | 1224 | { |
Marcin Åšlusarz | 67c3931 | 2007-12-14 12:53:21 +0100 | [diff] [blame] | 1225 | static unsigned int pow2_sizes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
| 1227 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, |
| 1228 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, |
| 1229 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 |
| 1230 | }; |
| 1231 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 1232 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); |
| 1233 | } |
| 1234 | |
| 1235 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1236 | * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1237 | * @runtime: PCM runtime instance |
| 1238 | * @cond: condition bits |
| 1239 | * @var: hw_params variable to apply the power-of-2 constraint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1241 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | unsigned int cond, |
| 1243 | snd_pcm_hw_param_t var) |
| 1244 | { |
| 1245 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1246 | snd_pcm_hw_rule_pow2, NULL, |
| 1247 | var, -1); |
| 1248 | } |
| 1249 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1250 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
| 1251 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1252 | 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] | 1253 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | { |
| 1255 | if (hw_is_mask(var)) { |
| 1256 | snd_mask_any(hw_param_mask(params, var)); |
| 1257 | params->cmask |= 1 << var; |
| 1258 | params->rmask |= 1 << var; |
| 1259 | return; |
| 1260 | } |
| 1261 | if (hw_is_interval(var)) { |
| 1262 | snd_interval_any(hw_param_interval(params, var)); |
| 1263 | params->cmask |= 1 << var; |
| 1264 | params->rmask |= 1 << var; |
| 1265 | return; |
| 1266 | } |
| 1267 | snd_BUG(); |
| 1268 | } |
| 1269 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1270 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
| 1272 | unsigned int k; |
| 1273 | memset(params, 0, sizeof(*params)); |
| 1274 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) |
| 1275 | _snd_pcm_hw_param_any(params, k); |
| 1276 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
| 1277 | _snd_pcm_hw_param_any(params, k); |
| 1278 | params->info = ~0U; |
| 1279 | } |
| 1280 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1281 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1284 | * snd_pcm_hw_param_value - return @params field @var value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1285 | * @params: the hw_params instance |
| 1286 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1287 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1289 | * Return the value for field @var if it's fixed in configuration space |
| 1290 | * defined by @params. Return -%EINVAL otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1292 | int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
| 1293 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | { |
| 1295 | if (hw_is_mask(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1296 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | if (!snd_mask_single(mask)) |
| 1298 | return -EINVAL; |
| 1299 | if (dir) |
| 1300 | *dir = 0; |
| 1301 | return snd_mask_value(mask); |
| 1302 | } |
| 1303 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1304 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | if (!snd_interval_single(i)) |
| 1306 | return -EINVAL; |
| 1307 | if (dir) |
| 1308 | *dir = i->openmin; |
| 1309 | return snd_interval_value(i); |
| 1310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | return -EINVAL; |
| 1312 | } |
| 1313 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1314 | EXPORT_SYMBOL(snd_pcm_hw_param_value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1316 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | snd_pcm_hw_param_t var) |
| 1318 | { |
| 1319 | if (hw_is_mask(var)) { |
| 1320 | snd_mask_none(hw_param_mask(params, var)); |
| 1321 | params->cmask |= 1 << var; |
| 1322 | params->rmask |= 1 << var; |
| 1323 | } else if (hw_is_interval(var)) { |
| 1324 | snd_interval_none(hw_param_interval(params, var)); |
| 1325 | params->cmask |= 1 << var; |
| 1326 | params->rmask |= 1 << var; |
| 1327 | } else { |
| 1328 | snd_BUG(); |
| 1329 | } |
| 1330 | } |
| 1331 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1332 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1334 | 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] | 1335 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
| 1337 | int changed; |
| 1338 | if (hw_is_mask(var)) |
| 1339 | changed = snd_mask_refine_first(hw_param_mask(params, var)); |
| 1340 | else if (hw_is_interval(var)) |
| 1341 | changed = snd_interval_refine_first(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1342 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | if (changed) { |
| 1345 | params->cmask |= 1 << var; |
| 1346 | params->rmask |= 1 << var; |
| 1347 | } |
| 1348 | return changed; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1353 | * snd_pcm_hw_param_first - refine config space and return minimum value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1354 | * @pcm: PCM instance |
| 1355 | * @params: the hw_params instance |
| 1356 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1357 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1359 | * Inside configuration space defined by @params remove from @var all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | * values > minimum. Reduce configuration space accordingly. |
| 1361 | * Return the minimum. |
| 1362 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1363 | int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
| 1364 | struct snd_pcm_hw_params *params, |
| 1365 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
| 1367 | int changed = _snd_pcm_hw_param_first(params, var); |
| 1368 | if (changed < 0) |
| 1369 | return changed; |
| 1370 | if (params->rmask) { |
| 1371 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1372 | if (snd_BUG_ON(err < 0)) |
| 1373 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } |
| 1375 | return snd_pcm_hw_param_value(params, var, dir); |
| 1376 | } |
| 1377 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1378 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
| 1379 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1380 | 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] | 1381 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | { |
| 1383 | int changed; |
| 1384 | if (hw_is_mask(var)) |
| 1385 | changed = snd_mask_refine_last(hw_param_mask(params, var)); |
| 1386 | else if (hw_is_interval(var)) |
| 1387 | changed = snd_interval_refine_last(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1388 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | if (changed) { |
| 1391 | params->cmask |= 1 << var; |
| 1392 | params->rmask |= 1 << var; |
| 1393 | } |
| 1394 | return changed; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1399 | * snd_pcm_hw_param_last - refine config space and return maximum value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1400 | * @pcm: PCM instance |
| 1401 | * @params: the hw_params instance |
| 1402 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1403 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1405 | * Inside configuration space defined by @params remove from @var all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | * values < maximum. Reduce configuration space accordingly. |
| 1407 | * Return the maximum. |
| 1408 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1409 | int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
| 1410 | struct snd_pcm_hw_params *params, |
| 1411 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | { |
| 1413 | int changed = _snd_pcm_hw_param_last(params, var); |
| 1414 | if (changed < 0) |
| 1415 | return changed; |
| 1416 | if (params->rmask) { |
| 1417 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1418 | if (snd_BUG_ON(err < 0)) |
| 1419 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | return snd_pcm_hw_param_value(params, var, dir); |
| 1422 | } |
| 1423 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1424 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
| 1426 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1427 | * snd_pcm_hw_param_choose - choose a configuration defined by @params |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1428 | * @pcm: PCM instance |
| 1429 | * @params: the hw_params instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1431 | * Choose one configuration from configuration space defined by @params. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | * The configuration chosen is that obtained fixing in this order: |
| 1433 | * first access, first format, first subformat, min channels, |
| 1434 | * min rate, min period time, max buffer size, min tick time |
| 1435 | */ |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1436 | int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, |
| 1437 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | { |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1439 | static int vars[] = { |
| 1440 | SNDRV_PCM_HW_PARAM_ACCESS, |
| 1441 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1442 | SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 1443 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1444 | SNDRV_PCM_HW_PARAM_RATE, |
| 1445 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1446 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1447 | SNDRV_PCM_HW_PARAM_TICK_TIME, |
| 1448 | -1 |
| 1449 | }; |
| 1450 | int err, *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1452 | for (v = vars; *v != -1; v++) { |
| 1453 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) |
| 1454 | err = snd_pcm_hw_param_first(pcm, params, *v, NULL); |
| 1455 | else |
| 1456 | err = snd_pcm_hw_param_last(pcm, params, *v, NULL); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1457 | if (snd_BUG_ON(err < 0)) |
| 1458 | return err; |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1463 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | void *arg) |
| 1465 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1466 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | unsigned long flags; |
| 1468 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1469 | if (snd_pcm_running(substream) && |
| 1470 | snd_pcm_update_hw_ptr(substream) >= 0) |
| 1471 | runtime->status->hw_ptr %= runtime->buffer_size; |
| 1472 | else |
| 1473 | runtime->status->hw_ptr = 0; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 1474 | runtime->hw_ptr_jiffies = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1479 | 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] | 1480 | void *arg) |
| 1481 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1482 | struct snd_pcm_channel_info *info = arg; |
| 1483 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | int width; |
| 1485 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { |
| 1486 | info->offset = -1; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | width = snd_pcm_format_physical_width(runtime->format); |
| 1490 | if (width < 0) |
| 1491 | return width; |
| 1492 | info->offset = 0; |
| 1493 | switch (runtime->access) { |
| 1494 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: |
| 1495 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: |
| 1496 | info->first = info->channel * width; |
| 1497 | info->step = runtime->channels * width; |
| 1498 | break; |
| 1499 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: |
| 1500 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: |
| 1501 | { |
| 1502 | size_t size = runtime->dma_bytes / runtime->channels; |
| 1503 | info->first = info->channel * size * 8; |
| 1504 | info->step = width; |
| 1505 | break; |
| 1506 | } |
| 1507 | default: |
| 1508 | snd_BUG(); |
| 1509 | break; |
| 1510 | } |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback |
| 1516 | * @substream: the pcm substream instance |
| 1517 | * @cmd: ioctl command |
| 1518 | * @arg: ioctl argument |
| 1519 | * |
| 1520 | * Processes the generic ioctl commands for PCM. |
| 1521 | * Can be passed as the ioctl callback for PCM ops. |
| 1522 | * |
| 1523 | * Returns zero if successful, or a negative error code on failure. |
| 1524 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1525 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | unsigned int cmd, void *arg) |
| 1527 | { |
| 1528 | switch (cmd) { |
| 1529 | case SNDRV_PCM_IOCTL1_INFO: |
| 1530 | return 0; |
| 1531 | case SNDRV_PCM_IOCTL1_RESET: |
| 1532 | return snd_pcm_lib_ioctl_reset(substream, arg); |
| 1533 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: |
| 1534 | return snd_pcm_lib_ioctl_channel_info(substream, arg); |
| 1535 | } |
| 1536 | return -ENXIO; |
| 1537 | } |
| 1538 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1539 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
| 1540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | /** |
| 1542 | * snd_pcm_period_elapsed - update the pcm status for the next period |
| 1543 | * @substream: the pcm substream instance |
| 1544 | * |
| 1545 | * This function is called from the interrupt handler when the |
| 1546 | * PCM has processed the period size. It will update the current |
Takashi Iwai | 31e8960 | 2008-01-08 18:09:57 +0100 | [diff] [blame] | 1547 | * pointer, wake up sleepers, etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | * |
| 1549 | * Even if more than one periods have elapsed since the last call, you |
| 1550 | * have to call this only once. |
| 1551 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1552 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1554 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | unsigned long flags; |
| 1556 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1557 | if (PCM_RUNTIME_CHECK(substream)) |
| 1558 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
| 1561 | if (runtime->transfer_ack_begin) |
| 1562 | runtime->transfer_ack_begin(substream); |
| 1563 | |
| 1564 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1565 | if (!snd_pcm_running(substream) || |
| 1566 | snd_pcm_update_hw_ptr_interrupt(substream) < 0) |
| 1567 | goto _end; |
| 1568 | |
| 1569 | if (substream->timer_running) |
| 1570 | snd_timer_interrupt(substream->timer, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | _end: |
| 1572 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1573 | if (runtime->transfer_ack_end) |
| 1574 | runtime->transfer_ack_end(substream); |
| 1575 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); |
| 1576 | } |
| 1577 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1578 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
| 1579 | |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1580 | /* |
| 1581 | * Wait until avail_min data becomes available |
| 1582 | * Returns a negative error code if any error occurs during operation. |
| 1583 | * The available space is stored on availp. When err = 0 and avail = 0 |
| 1584 | * on the capture stream, it indicates the stream is in DRAINING state. |
| 1585 | */ |
| 1586 | static int wait_for_avail_min(struct snd_pcm_substream *substream, |
| 1587 | snd_pcm_uframes_t *availp) |
| 1588 | { |
| 1589 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1590 | int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 1591 | wait_queue_t wait; |
| 1592 | int err = 0; |
| 1593 | snd_pcm_uframes_t avail = 0; |
| 1594 | long tout; |
| 1595 | |
| 1596 | init_waitqueue_entry(&wait, current); |
| 1597 | add_wait_queue(&runtime->sleep, &wait); |
| 1598 | for (;;) { |
| 1599 | if (signal_pending(current)) { |
| 1600 | err = -ERESTARTSYS; |
| 1601 | break; |
| 1602 | } |
| 1603 | set_current_state(TASK_INTERRUPTIBLE); |
| 1604 | snd_pcm_stream_unlock_irq(substream); |
| 1605 | tout = schedule_timeout(msecs_to_jiffies(10000)); |
| 1606 | snd_pcm_stream_lock_irq(substream); |
| 1607 | switch (runtime->status->state) { |
| 1608 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1609 | err = -ESTRPIPE; |
| 1610 | goto _endloop; |
| 1611 | case SNDRV_PCM_STATE_XRUN: |
| 1612 | err = -EPIPE; |
| 1613 | goto _endloop; |
| 1614 | case SNDRV_PCM_STATE_DRAINING: |
| 1615 | if (is_playback) |
| 1616 | err = -EPIPE; |
| 1617 | else |
| 1618 | avail = 0; /* indicate draining */ |
| 1619 | goto _endloop; |
| 1620 | case SNDRV_PCM_STATE_OPEN: |
| 1621 | case SNDRV_PCM_STATE_SETUP: |
| 1622 | case SNDRV_PCM_STATE_DISCONNECTED: |
| 1623 | err = -EBADFD; |
| 1624 | goto _endloop; |
| 1625 | } |
| 1626 | if (!tout) { |
| 1627 | snd_printd("%s write error (DMA or IRQ trouble?)\n", |
| 1628 | is_playback ? "playback" : "capture"); |
| 1629 | err = -EIO; |
| 1630 | break; |
| 1631 | } |
| 1632 | if (is_playback) |
| 1633 | avail = snd_pcm_playback_avail(runtime); |
| 1634 | else |
| 1635 | avail = snd_pcm_capture_avail(runtime); |
| 1636 | if (avail >= runtime->control->avail_min) |
| 1637 | break; |
| 1638 | } |
| 1639 | _endloop: |
| 1640 | remove_wait_queue(&runtime->sleep, &wait); |
| 1641 | *availp = avail; |
| 1642 | return err; |
| 1643 | } |
| 1644 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1645 | static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | unsigned int hwoff, |
| 1647 | unsigned long data, unsigned int off, |
| 1648 | snd_pcm_uframes_t frames) |
| 1649 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1650 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | int err; |
| 1652 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 1653 | if (substream->ops->copy) { |
| 1654 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 1655 | return err; |
| 1656 | } else { |
| 1657 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) |
| 1659 | return -EFAULT; |
| 1660 | } |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1664 | typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | unsigned long data, unsigned int off, |
| 1666 | snd_pcm_uframes_t size); |
| 1667 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1668 | 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] | 1669 | unsigned long data, |
| 1670 | snd_pcm_uframes_t size, |
| 1671 | int nonblock, |
| 1672 | transfer_f transfer) |
| 1673 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1674 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | snd_pcm_uframes_t xfer = 0; |
| 1676 | snd_pcm_uframes_t offset = 0; |
| 1677 | int err = 0; |
| 1678 | |
| 1679 | if (size == 0) |
| 1680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
| 1682 | snd_pcm_stream_lock_irq(substream); |
| 1683 | switch (runtime->status->state) { |
| 1684 | case SNDRV_PCM_STATE_PREPARED: |
| 1685 | case SNDRV_PCM_STATE_RUNNING: |
| 1686 | case SNDRV_PCM_STATE_PAUSED: |
| 1687 | break; |
| 1688 | case SNDRV_PCM_STATE_XRUN: |
| 1689 | err = -EPIPE; |
| 1690 | goto _end_unlock; |
| 1691 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1692 | err = -ESTRPIPE; |
| 1693 | goto _end_unlock; |
| 1694 | default: |
| 1695 | err = -EBADFD; |
| 1696 | goto _end_unlock; |
| 1697 | } |
| 1698 | |
| 1699 | while (size > 0) { |
| 1700 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 1701 | snd_pcm_uframes_t avail; |
| 1702 | snd_pcm_uframes_t cont; |
Takashi Iwai | 31e8960 | 2008-01-08 18:09:57 +0100 | [diff] [blame] | 1703 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | snd_pcm_update_hw_ptr(substream); |
| 1705 | avail = snd_pcm_playback_avail(runtime); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1706 | if (!avail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | if (nonblock) { |
| 1708 | err = -EAGAIN; |
| 1709 | goto _end_unlock; |
| 1710 | } |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1711 | err = wait_for_avail_min(substream, &avail); |
| 1712 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | frames = size > avail ? avail : size; |
| 1716 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 1717 | if (frames > cont) |
| 1718 | frames = cont; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1719 | if (snd_BUG_ON(!frames)) { |
| 1720 | snd_pcm_stream_unlock_irq(substream); |
| 1721 | return -EINVAL; |
| 1722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | appl_ptr = runtime->control->appl_ptr; |
| 1724 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 1725 | snd_pcm_stream_unlock_irq(substream); |
| 1726 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 1727 | goto _end; |
| 1728 | snd_pcm_stream_lock_irq(substream); |
| 1729 | switch (runtime->status->state) { |
| 1730 | case SNDRV_PCM_STATE_XRUN: |
| 1731 | err = -EPIPE; |
| 1732 | goto _end_unlock; |
| 1733 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1734 | err = -ESTRPIPE; |
| 1735 | goto _end_unlock; |
| 1736 | default: |
| 1737 | break; |
| 1738 | } |
| 1739 | appl_ptr += frames; |
| 1740 | if (appl_ptr >= runtime->boundary) |
| 1741 | appl_ptr -= runtime->boundary; |
| 1742 | runtime->control->appl_ptr = appl_ptr; |
| 1743 | if (substream->ops->ack) |
| 1744 | substream->ops->ack(substream); |
| 1745 | |
| 1746 | offset += frames; |
| 1747 | size -= frames; |
| 1748 | xfer += frames; |
| 1749 | if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && |
| 1750 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { |
| 1751 | err = snd_pcm_start(substream); |
| 1752 | if (err < 0) |
| 1753 | goto _end_unlock; |
| 1754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | } |
| 1756 | _end_unlock: |
| 1757 | snd_pcm_stream_unlock_irq(substream); |
| 1758 | _end: |
| 1759 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 1760 | } |
| 1761 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1762 | /* sanity-check for read/write methods */ |
| 1763 | static int pcm_sanity_check(struct snd_pcm_substream *substream) |
| 1764 | { |
| 1765 | struct snd_pcm_runtime *runtime; |
| 1766 | if (PCM_RUNTIME_CHECK(substream)) |
| 1767 | return -ENXIO; |
| 1768 | runtime = substream->runtime; |
| 1769 | if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area)) |
| 1770 | return -EINVAL; |
| 1771 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 1772 | return -EBADFD; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1776 | 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] | 1777 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1778 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1780 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1782 | err = pcm_sanity_check(substream); |
| 1783 | if (err < 0) |
| 1784 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1786 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && |
| 1789 | runtime->channels > 1) |
| 1790 | return -EINVAL; |
| 1791 | return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock, |
| 1792 | snd_pcm_lib_write_transfer); |
| 1793 | } |
| 1794 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1795 | EXPORT_SYMBOL(snd_pcm_lib_write); |
| 1796 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1797 | static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | unsigned int hwoff, |
| 1799 | unsigned long data, unsigned int off, |
| 1800 | snd_pcm_uframes_t frames) |
| 1801 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1802 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | int err; |
| 1804 | void __user **bufs = (void __user **)data; |
| 1805 | int channels = runtime->channels; |
| 1806 | int c; |
| 1807 | if (substream->ops->copy) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1808 | if (snd_BUG_ON(!substream->ops->silence)) |
| 1809 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | for (c = 0; c < channels; ++c, ++bufs) { |
| 1811 | if (*bufs == NULL) { |
| 1812 | if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0) |
| 1813 | return err; |
| 1814 | } else { |
| 1815 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 1816 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 1817 | return err; |
| 1818 | } |
| 1819 | } |
| 1820 | } else { |
| 1821 | /* default transfer behaviour */ |
| 1822 | size_t dma_csize = runtime->dma_bytes / channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | for (c = 0; c < channels; ++c, ++bufs) { |
| 1824 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 1825 | if (*bufs == NULL) { |
| 1826 | snd_pcm_format_set_silence(runtime->format, hwbuf, frames); |
| 1827 | } else { |
| 1828 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 1829 | if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames))) |
| 1830 | return -EFAULT; |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1837 | 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] | 1838 | void __user **bufs, |
| 1839 | snd_pcm_uframes_t frames) |
| 1840 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1841 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1843 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1845 | err = pcm_sanity_check(substream); |
| 1846 | if (err < 0) |
| 1847 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1849 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | |
| 1851 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 1852 | return -EINVAL; |
| 1853 | return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames, |
| 1854 | nonblock, snd_pcm_lib_writev_transfer); |
| 1855 | } |
| 1856 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1857 | EXPORT_SYMBOL(snd_pcm_lib_writev); |
| 1858 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1859 | static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | unsigned int hwoff, |
| 1861 | unsigned long data, unsigned int off, |
| 1862 | snd_pcm_uframes_t frames) |
| 1863 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1864 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | int err; |
| 1866 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 1867 | if (substream->ops->copy) { |
| 1868 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 1869 | return err; |
| 1870 | } else { |
| 1871 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) |
| 1873 | return -EFAULT; |
| 1874 | } |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1878 | 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] | 1879 | unsigned long data, |
| 1880 | snd_pcm_uframes_t size, |
| 1881 | int nonblock, |
| 1882 | transfer_f transfer) |
| 1883 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1884 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | snd_pcm_uframes_t xfer = 0; |
| 1886 | snd_pcm_uframes_t offset = 0; |
| 1887 | int err = 0; |
| 1888 | |
| 1889 | if (size == 0) |
| 1890 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
| 1892 | snd_pcm_stream_lock_irq(substream); |
| 1893 | switch (runtime->status->state) { |
| 1894 | case SNDRV_PCM_STATE_PREPARED: |
| 1895 | if (size >= runtime->start_threshold) { |
| 1896 | err = snd_pcm_start(substream); |
| 1897 | if (err < 0) |
| 1898 | goto _end_unlock; |
| 1899 | } |
| 1900 | break; |
| 1901 | case SNDRV_PCM_STATE_DRAINING: |
| 1902 | case SNDRV_PCM_STATE_RUNNING: |
| 1903 | case SNDRV_PCM_STATE_PAUSED: |
| 1904 | break; |
| 1905 | case SNDRV_PCM_STATE_XRUN: |
| 1906 | err = -EPIPE; |
| 1907 | goto _end_unlock; |
| 1908 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1909 | err = -ESTRPIPE; |
| 1910 | goto _end_unlock; |
| 1911 | default: |
| 1912 | err = -EBADFD; |
| 1913 | goto _end_unlock; |
| 1914 | } |
| 1915 | |
| 1916 | while (size > 0) { |
| 1917 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
| 1918 | snd_pcm_uframes_t avail; |
| 1919 | snd_pcm_uframes_t cont; |
Takashi Iwai | 31e8960 | 2008-01-08 18:09:57 +0100 | [diff] [blame] | 1920 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | snd_pcm_update_hw_ptr(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | avail = snd_pcm_capture_avail(runtime); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1923 | if (!avail) { |
| 1924 | if (runtime->status->state == |
| 1925 | SNDRV_PCM_STATE_DRAINING) { |
| 1926 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | goto _end_unlock; |
| 1928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | if (nonblock) { |
| 1930 | err = -EAGAIN; |
| 1931 | goto _end_unlock; |
| 1932 | } |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1933 | err = wait_for_avail_min(substream, &avail); |
| 1934 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | goto _end_unlock; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1936 | if (!avail) |
| 1937 | continue; /* draining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | frames = size > avail ? avail : size; |
| 1940 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 1941 | if (frames > cont) |
| 1942 | frames = cont; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1943 | if (snd_BUG_ON(!frames)) { |
| 1944 | snd_pcm_stream_unlock_irq(substream); |
| 1945 | return -EINVAL; |
| 1946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | appl_ptr = runtime->control->appl_ptr; |
| 1948 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 1949 | snd_pcm_stream_unlock_irq(substream); |
| 1950 | if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0) |
| 1951 | goto _end; |
| 1952 | snd_pcm_stream_lock_irq(substream); |
| 1953 | switch (runtime->status->state) { |
| 1954 | case SNDRV_PCM_STATE_XRUN: |
| 1955 | err = -EPIPE; |
| 1956 | goto _end_unlock; |
| 1957 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1958 | err = -ESTRPIPE; |
| 1959 | goto _end_unlock; |
| 1960 | default: |
| 1961 | break; |
| 1962 | } |
| 1963 | appl_ptr += frames; |
| 1964 | if (appl_ptr >= runtime->boundary) |
| 1965 | appl_ptr -= runtime->boundary; |
| 1966 | runtime->control->appl_ptr = appl_ptr; |
| 1967 | if (substream->ops->ack) |
| 1968 | substream->ops->ack(substream); |
| 1969 | |
| 1970 | offset += frames; |
| 1971 | size -= frames; |
| 1972 | xfer += frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | _end_unlock: |
| 1975 | snd_pcm_stream_unlock_irq(substream); |
| 1976 | _end: |
| 1977 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 1978 | } |
| 1979 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1980 | 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] | 1981 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1982 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1984 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1986 | err = pcm_sanity_check(substream); |
| 1987 | if (err < 0) |
| 1988 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1990 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
| 1992 | return -EINVAL; |
| 1993 | return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer); |
| 1994 | } |
| 1995 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1996 | EXPORT_SYMBOL(snd_pcm_lib_read); |
| 1997 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1998 | static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | unsigned int hwoff, |
| 2000 | unsigned long data, unsigned int off, |
| 2001 | snd_pcm_uframes_t frames) |
| 2002 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2003 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | int err; |
| 2005 | void __user **bufs = (void __user **)data; |
| 2006 | int channels = runtime->channels; |
| 2007 | int c; |
| 2008 | if (substream->ops->copy) { |
| 2009 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2010 | char __user *buf; |
| 2011 | if (*bufs == NULL) |
| 2012 | continue; |
| 2013 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2014 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2015 | return err; |
| 2016 | } |
| 2017 | } else { |
| 2018 | snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2020 | char *hwbuf; |
| 2021 | char __user *buf; |
| 2022 | if (*bufs == NULL) |
| 2023 | continue; |
| 2024 | |
| 2025 | hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2026 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2027 | if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames))) |
| 2028 | return -EFAULT; |
| 2029 | } |
| 2030 | } |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2034 | 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] | 2035 | void __user **bufs, |
| 2036 | snd_pcm_uframes_t frames) |
| 2037 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2038 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2040 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2042 | err = pcm_sanity_check(substream); |
| 2043 | if (err < 0) |
| 2044 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2047 | return -EBADFD; |
| 2048 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2049 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2051 | return -EINVAL; |
| 2052 | return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer); |
| 2053 | } |
| 2054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | EXPORT_SYMBOL(snd_pcm_lib_readv); |