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