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