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