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