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