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