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