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