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