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