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