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