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