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