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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame^] | 24 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/time.h> |
Takashi Iwai | 3f7440a | 2009-06-05 17:40:04 +0200 | [diff] [blame] | 26 | #include <linux/math64.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include <sound/control.h> |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 30 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/info.h> |
| 32 | #include <sound/pcm.h> |
| 33 | #include <sound/pcm_params.h> |
| 34 | #include <sound/timer.h> |
| 35 | |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 36 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
| 37 | #define CREATE_TRACE_POINTS |
| 38 | #include "pcm_trace.h" |
| 39 | #else |
| 40 | #define trace_hwptr(substream, pos, in_interrupt) |
| 41 | #define trace_xrun(substream) |
| 42 | #define trace_hw_ptr_error(substream, reason) |
| 43 | #endif |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * fill ring buffer with silence |
| 47 | * runtime->silence_start: starting pointer to silence area |
| 48 | * runtime->silence_filled: size filled with silence |
| 49 | * runtime->silence_threshold: threshold from application |
| 50 | * runtime->silence_size: maximal size from application |
| 51 | * |
| 52 | * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately |
| 53 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 54 | 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] | 55 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 56 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | snd_pcm_uframes_t frames, ofs, transfer; |
| 58 | |
| 59 | if (runtime->silence_size < runtime->boundary) { |
| 60 | snd_pcm_sframes_t noise_dist, n; |
| 61 | if (runtime->silence_start != runtime->control->appl_ptr) { |
| 62 | n = runtime->control->appl_ptr - runtime->silence_start; |
| 63 | if (n < 0) |
| 64 | n += runtime->boundary; |
| 65 | if ((snd_pcm_uframes_t)n < runtime->silence_filled) |
| 66 | runtime->silence_filled -= n; |
| 67 | else |
| 68 | runtime->silence_filled = 0; |
| 69 | runtime->silence_start = runtime->control->appl_ptr; |
| 70 | } |
Takashi Iwai | 235475c | 2005-12-07 15:28:07 +0100 | [diff] [blame] | 71 | if (runtime->silence_filled >= runtime->buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; |
| 74 | if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) |
| 75 | return; |
| 76 | frames = runtime->silence_threshold - noise_dist; |
| 77 | if (frames > runtime->silence_size) |
| 78 | frames = runtime->silence_size; |
| 79 | } else { |
| 80 | if (new_hw_ptr == ULONG_MAX) { /* initialization */ |
| 81 | snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); |
Jaroslav Kysela | 9e216e8 | 2010-07-19 16:37:39 +0200 | [diff] [blame] | 82 | if (avail > runtime->buffer_size) |
| 83 | avail = runtime->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | runtime->silence_filled = avail > 0 ? avail : 0; |
| 85 | runtime->silence_start = (runtime->status->hw_ptr + |
| 86 | runtime->silence_filled) % |
| 87 | runtime->boundary; |
| 88 | } else { |
| 89 | ofs = runtime->status->hw_ptr; |
| 90 | frames = new_hw_ptr - ofs; |
| 91 | if ((snd_pcm_sframes_t)frames < 0) |
| 92 | frames += runtime->boundary; |
| 93 | runtime->silence_filled -= frames; |
| 94 | if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { |
| 95 | runtime->silence_filled = 0; |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 96 | runtime->silence_start = new_hw_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } else { |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 98 | runtime->silence_start = ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | frames = runtime->buffer_size - runtime->silence_filled; |
| 102 | } |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 103 | if (snd_BUG_ON(frames > runtime->buffer_size)) |
| 104 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (frames == 0) |
| 106 | return; |
Clemens Ladisch | 9a826dd | 2006-10-23 16:26:57 +0200 | [diff] [blame] | 107 | ofs = runtime->silence_start % runtime->buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | while (frames > 0) { |
| 109 | transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; |
| 110 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || |
| 111 | runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) { |
| 112 | if (substream->ops->silence) { |
| 113 | int err; |
| 114 | err = substream->ops->silence(substream, -1, ofs, transfer); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 115 | snd_BUG_ON(err < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } else { |
| 117 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs); |
| 118 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels); |
| 119 | } |
| 120 | } else { |
| 121 | unsigned int c; |
| 122 | unsigned int channels = runtime->channels; |
| 123 | if (substream->ops->silence) { |
| 124 | for (c = 0; c < channels; ++c) { |
| 125 | int err; |
| 126 | err = substream->ops->silence(substream, c, ofs, transfer); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 127 | snd_BUG_ON(err < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | size_t dma_csize = runtime->dma_bytes / channels; |
| 131 | for (c = 0; c < channels; ++c) { |
| 132 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs); |
| 133 | snd_pcm_format_set_silence(runtime->format, hwbuf, transfer); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | runtime->silence_filled += transfer; |
| 138 | frames -= transfer; |
| 139 | ofs = 0; |
| 140 | } |
| 141 | } |
| 142 | |
Eliot Blennerhassett | acb03d4 | 2011-07-23 12:36:25 +1200 | [diff] [blame] | 143 | #ifdef CONFIG_SND_DEBUG |
| 144 | void snd_pcm_debug_name(struct snd_pcm_substream *substream, |
Takashi Iwai | c007011 | 2009-06-08 15:58:48 +0200 | [diff] [blame] | 145 | char *name, size_t len) |
| 146 | { |
| 147 | snprintf(name, len, "pcmC%dD%d%c:%d", |
| 148 | substream->pcm->card->number, |
| 149 | substream->pcm->device, |
| 150 | substream->stream ? 'c' : 'p', |
| 151 | substream->number); |
| 152 | } |
Eliot Blennerhassett | acb03d4 | 2011-07-23 12:36:25 +1200 | [diff] [blame] | 153 | EXPORT_SYMBOL(snd_pcm_debug_name); |
| 154 | #endif |
Takashi Iwai | c007011 | 2009-06-08 15:58:48 +0200 | [diff] [blame] | 155 | |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 156 | #define XRUN_DEBUG_BASIC (1<<0) |
| 157 | #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */ |
| 158 | #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */ |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 159 | |
| 160 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
| 161 | |
| 162 | #define xrun_debug(substream, mask) \ |
| 163 | ((substream)->pstr->xrun_debug & (mask)) |
Jarkko Nikula | 0f17014 | 2010-03-26 16:07:25 +0200 | [diff] [blame] | 164 | #else |
| 165 | #define xrun_debug(substream, mask) 0 |
| 166 | #endif |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 167 | |
| 168 | #define dump_stack_on_xrun(substream) do { \ |
| 169 | if (xrun_debug(substream, XRUN_DEBUG_STACK)) \ |
| 170 | dump_stack(); \ |
| 171 | } while (0) |
| 172 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 173 | static void xrun(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Jaroslav Kysela | 13f040f | 2009-05-28 11:31:20 +0200 | [diff] [blame] | 175 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 176 | |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 177 | trace_xrun(substream); |
Jaroslav Kysela | 13f040f | 2009-05-28 11:31:20 +0200 | [diff] [blame] | 178 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) |
| 179 | snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
Jaroslav Kysela | 741b20c | 2009-12-17 17:34:39 +0100 | [diff] [blame] | 181 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { |
Takashi Iwai | c007011 | 2009-06-08 15:58:48 +0200 | [diff] [blame] | 182 | char name[16]; |
Eliot Blennerhassett | acb03d4 | 2011-07-23 12:36:25 +1200 | [diff] [blame] | 183 | snd_pcm_debug_name(substream, name, sizeof(name)); |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 184 | pcm_warn(substream->pcm, "XRUN: %s\n", name); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 185 | dump_stack_on_xrun(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jarkko Nikula | 0f17014 | 2010-03-26 16:07:25 +0200 | [diff] [blame] | 189 | #ifdef CONFIG_SND_PCM_XRUN_DEBUG |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 190 | #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \ |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 191 | do { \ |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 192 | trace_hw_ptr_error(substream, reason); \ |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 193 | if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \ |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 194 | pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \ |
| 195 | (in_interrupt) ? 'Q' : 'P', ##args); \ |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 196 | dump_stack_on_xrun(substream); \ |
| 197 | } \ |
| 198 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 200 | #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */ |
| 201 | |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 202 | #define hw_ptr_error(substream, fmt, args...) do { } while (0) |
Jaroslav Kysela | 4d96eb2 | 2009-12-20 11:47:57 +0100 | [diff] [blame] | 203 | |
| 204 | #endif |
| 205 | |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 206 | int snd_pcm_update_state(struct snd_pcm_substream *substream, |
| 207 | struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | snd_pcm_uframes_t avail; |
| 210 | |
| 211 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 212 | avail = snd_pcm_playback_avail(runtime); |
| 213 | else |
| 214 | avail = snd_pcm_capture_avail(runtime); |
| 215 | if (avail > runtime->avail_max) |
| 216 | runtime->avail_max = avail; |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 217 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
| 218 | if (avail >= runtime->buffer_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | snd_pcm_drain_done(substream); |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 220 | return -EPIPE; |
| 221 | } |
| 222 | } else { |
| 223 | if (avail >= runtime->stop_threshold) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | xrun(substream); |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 225 | return -EPIPE; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 228 | if (runtime->twake) { |
| 229 | if (avail >= runtime->twake) |
| 230 | wake_up(&runtime->tsleep); |
| 231 | } else if (avail >= runtime->control->avail_min) |
| 232 | wake_up(&runtime->sleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 236 | static void update_audio_tstamp(struct snd_pcm_substream *substream, |
| 237 | struct timespec *curr_tstamp, |
| 238 | struct timespec *audio_tstamp) |
| 239 | { |
| 240 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 241 | u64 audio_frames, audio_nsecs; |
| 242 | struct timespec driver_tstamp; |
| 243 | |
| 244 | if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE) |
| 245 | return; |
| 246 | |
| 247 | if (!(substream->ops->get_time_info) || |
| 248 | (runtime->audio_tstamp_report.actual_type == |
| 249 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { |
| 250 | |
| 251 | /* |
| 252 | * provide audio timestamp derived from pointer position |
| 253 | * add delay only if requested |
| 254 | */ |
| 255 | |
| 256 | audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr; |
| 257 | |
| 258 | if (runtime->audio_tstamp_config.report_delay) { |
| 259 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 260 | audio_frames -= runtime->delay; |
| 261 | else |
| 262 | audio_frames += runtime->delay; |
| 263 | } |
| 264 | audio_nsecs = div_u64(audio_frames * 1000000000LL, |
| 265 | runtime->rate); |
| 266 | *audio_tstamp = ns_to_timespec(audio_nsecs); |
| 267 | } |
| 268 | runtime->status->audio_tstamp = *audio_tstamp; |
| 269 | runtime->status->tstamp = *curr_tstamp; |
| 270 | |
| 271 | /* |
| 272 | * re-take a driver timestamp to let apps detect if the reference tstamp |
| 273 | * read by low-level hardware was provided with a delay |
| 274 | */ |
| 275 | snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp); |
| 276 | runtime->driver_tstamp = driver_tstamp; |
| 277 | } |
| 278 | |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 279 | static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, |
| 280 | unsigned int in_interrupt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 282 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | snd_pcm_uframes_t pos; |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 284 | snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 285 | snd_pcm_sframes_t hdelta, delta; |
| 286 | unsigned long jdelta; |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 287 | unsigned long curr_jiffies; |
| 288 | struct timespec curr_tstamp; |
Pierre-Louis Bossart | 4eeaaea | 2012-10-22 16:42:15 -0500 | [diff] [blame] | 289 | struct timespec audio_tstamp; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 290 | int crossed_boundary = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 292 | old_hw_ptr = runtime->status->hw_ptr; |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * group pointer, time and jiffies reads to allow for more |
| 296 | * accurate correlations/corrections. |
| 297 | * The values are stored at the end of this routine after |
| 298 | * corrections for hw_ptr position |
| 299 | */ |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 300 | pos = substream->ops->pointer(substream); |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 301 | curr_jiffies = jiffies; |
Pierre-Louis Bossart | 4eeaaea | 2012-10-22 16:42:15 -0500 | [diff] [blame] | 302 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 303 | if ((substream->ops->get_time_info) && |
| 304 | (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { |
| 305 | substream->ops->get_time_info(substream, &curr_tstamp, |
| 306 | &audio_tstamp, |
| 307 | &runtime->audio_tstamp_config, |
| 308 | &runtime->audio_tstamp_report); |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 309 | |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 310 | /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */ |
| 311 | if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT) |
| 312 | snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp); |
| 313 | } else |
| 314 | snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp); |
Pierre-Louis Bossart | 4eeaaea | 2012-10-22 16:42:15 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (pos == SNDRV_PCM_POS_XRUN) { |
| 318 | xrun(substream); |
| 319 | return -EPIPE; |
| 320 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 321 | if (pos >= runtime->buffer_size) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 322 | if (printk_ratelimit()) { |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 323 | char name[16]; |
Eliot Blennerhassett | acb03d4 | 2011-07-23 12:36:25 +1200 | [diff] [blame] | 324 | snd_pcm_debug_name(substream, name, sizeof(name)); |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 325 | pcm_err(substream->pcm, |
Takashi Iwai | 0ab1ace | 2016-03-10 20:56:20 +0100 | [diff] [blame] | 326 | "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n", |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 327 | name, pos, runtime->buffer_size, |
| 328 | runtime->period_size); |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 329 | } |
| 330 | pos = 0; |
Takashi Iwai | cedb811 | 2009-07-23 11:04:13 +0200 | [diff] [blame] | 331 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 332 | pos -= pos % runtime->min_align; |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 333 | trace_hwptr(substream, pos, in_interrupt); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 334 | hw_base = runtime->hw_ptr_base; |
| 335 | new_hw_ptr = hw_base + pos; |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 336 | if (in_interrupt) { |
| 337 | /* we know that one period was processed */ |
| 338 | /* delta = "expected next hw_ptr" for in_interrupt != 0 */ |
Jaroslav Kysela | e763692 | 2010-01-26 17:08:24 +0100 | [diff] [blame] | 339 | delta = runtime->hw_ptr_interrupt + runtime->period_size; |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 340 | if (delta > new_hw_ptr) { |
Jaroslav Kysela | bd76af0 | 2010-08-18 14:16:54 +0200 | [diff] [blame] | 341 | /* check for double acknowledged interrupts */ |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 342 | hdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
Koro Chen | 13a9883 | 2015-05-13 22:39:03 +0800 | [diff] [blame] | 343 | if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) { |
Jaroslav Kysela | bd76af0 | 2010-08-18 14:16:54 +0200 | [diff] [blame] | 344 | hw_base += runtime->buffer_size; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 345 | if (hw_base >= runtime->boundary) { |
Jaroslav Kysela | bd76af0 | 2010-08-18 14:16:54 +0200 | [diff] [blame] | 346 | hw_base = 0; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 347 | crossed_boundary++; |
| 348 | } |
Jaroslav Kysela | bd76af0 | 2010-08-18 14:16:54 +0200 | [diff] [blame] | 349 | new_hw_ptr = hw_base + pos; |
| 350 | goto __delta; |
| 351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 354 | /* new_hw_ptr might be lower than old_hw_ptr in case when */ |
| 355 | /* pointer crosses the end of the ring buffer */ |
| 356 | if (new_hw_ptr < old_hw_ptr) { |
| 357 | hw_base += runtime->buffer_size; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 358 | if (hw_base >= runtime->boundary) { |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 359 | hw_base = 0; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 360 | crossed_boundary++; |
| 361 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 362 | new_hw_ptr = hw_base + pos; |
| 363 | } |
| 364 | __delta: |
Clemens Ladisch | b406e61 | 2010-05-25 09:01:46 +0200 | [diff] [blame] | 365 | delta = new_hw_ptr - old_hw_ptr; |
| 366 | if (delta < 0) |
| 367 | delta += runtime->boundary; |
Clemens Ladisch | ab69a49 | 2010-11-15 10:46:23 +0100 | [diff] [blame] | 368 | |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 369 | if (runtime->no_period_wakeup) { |
Kelly Anderson | 12ff414 | 2011-04-01 11:58:25 +0200 | [diff] [blame] | 370 | snd_pcm_sframes_t xrun_threshold; |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 371 | /* |
| 372 | * Without regular period interrupts, we have to check |
| 373 | * the elapsed time to detect xruns. |
| 374 | */ |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 375 | jdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
Clemens Ladisch | 47228e4 | 2010-11-18 09:53:07 +0100 | [diff] [blame] | 376 | if (jdelta < runtime->hw_ptr_buffer_jiffies / 2) |
| 377 | goto no_delta_check; |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 378 | hdelta = jdelta - delta * HZ / runtime->rate; |
Kelly Anderson | 12ff414 | 2011-04-01 11:58:25 +0200 | [diff] [blame] | 379 | xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1; |
| 380 | while (hdelta > xrun_threshold) { |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 381 | delta += runtime->buffer_size; |
| 382 | hw_base += runtime->buffer_size; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 383 | if (hw_base >= runtime->boundary) { |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 384 | hw_base = 0; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 385 | crossed_boundary++; |
| 386 | } |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 387 | new_hw_ptr = hw_base + pos; |
| 388 | hdelta -= runtime->hw_ptr_buffer_jiffies; |
| 389 | } |
Clemens Ladisch | ab69a49 | 2010-11-15 10:46:23 +0100 | [diff] [blame] | 390 | goto no_delta_check; |
Clemens Ladisch | 59ff878 | 2010-11-18 09:43:52 +0100 | [diff] [blame] | 391 | } |
Clemens Ladisch | ab69a49 | 2010-11-15 10:46:23 +0100 | [diff] [blame] | 392 | |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 393 | /* something must be really wrong */ |
Jaroslav Kysela | 7b3a177 | 2010-01-08 08:43:01 +0100 | [diff] [blame] | 394 | if (delta >= runtime->buffer_size + runtime->period_size) { |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 395 | hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr", |
| 396 | "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", |
| 397 | substream->stream, (long)pos, |
| 398 | (long)new_hw_ptr, (long)old_hw_ptr); |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 399 | return 0; |
| 400 | } |
Takashi Iwai | c87d973 | 2009-05-27 10:53:33 +0200 | [diff] [blame] | 401 | |
| 402 | /* Do jiffies check only in xrun_debug mode */ |
Jaroslav Kysela | 741b20c | 2009-12-17 17:34:39 +0100 | [diff] [blame] | 403 | if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK)) |
Takashi Iwai | c87d973 | 2009-05-27 10:53:33 +0200 | [diff] [blame] | 404 | goto no_jiffies_check; |
| 405 | |
Takashi Iwai | 3e5b501 | 2009-04-28 12:07:08 +0200 | [diff] [blame] | 406 | /* Skip the jiffies check for hardwares with BATCH flag. |
| 407 | * Such hardware usually just increases the position at each IRQ, |
| 408 | * thus it can't give any strange position. |
| 409 | */ |
| 410 | if (runtime->hw.info & SNDRV_PCM_INFO_BATCH) |
| 411 | goto no_jiffies_check; |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 412 | hdelta = delta; |
Jaroslav Kysela | a4444da | 2009-05-28 12:31:56 +0200 | [diff] [blame] | 413 | if (hdelta < runtime->delay) |
| 414 | goto no_jiffies_check; |
| 415 | hdelta -= runtime->delay; |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 416 | jdelta = curr_jiffies - runtime->hw_ptr_jiffies; |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 417 | if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) { |
| 418 | delta = jdelta / |
| 419 | (((runtime->period_size * HZ) / runtime->rate) |
| 420 | + HZ/100); |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 421 | /* move new_hw_ptr according jiffies not pos variable */ |
| 422 | new_hw_ptr = old_hw_ptr; |
Jaroslav Kysela | ed69c6a | 2010-01-13 08:12:31 +0100 | [diff] [blame] | 423 | hw_base = delta; |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 424 | /* use loop to avoid checks for delta overflows */ |
| 425 | /* the delta value is small or zero in most cases */ |
| 426 | while (delta > 0) { |
| 427 | new_hw_ptr += runtime->period_size; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 428 | if (new_hw_ptr >= runtime->boundary) { |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 429 | new_hw_ptr -= runtime->boundary; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 430 | crossed_boundary--; |
| 431 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 432 | delta--; |
| 433 | } |
| 434 | /* align hw_base to buffer_size */ |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 435 | hw_ptr_error(substream, in_interrupt, "hw_ptr skipping", |
| 436 | "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n", |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 437 | (long)pos, (long)hdelta, |
| 438 | (long)runtime->period_size, jdelta, |
Jaroslav Kysela | ed69c6a | 2010-01-13 08:12:31 +0100 | [diff] [blame] | 439 | ((hdelta * HZ) / runtime->rate), hw_base, |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 440 | (unsigned long)old_hw_ptr, |
| 441 | (unsigned long)new_hw_ptr); |
Jaroslav Kysela | ed69c6a | 2010-01-13 08:12:31 +0100 | [diff] [blame] | 442 | /* reset values to proper state */ |
| 443 | delta = 0; |
| 444 | hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size); |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 445 | } |
Takashi Iwai | 3e5b501 | 2009-04-28 12:07:08 +0200 | [diff] [blame] | 446 | no_jiffies_check: |
Jaroslav Kysela | bbf6ad1 | 2009-04-10 12:28:58 +0200 | [diff] [blame] | 447 | if (delta > runtime->period_size + runtime->period_size / 2) { |
Takashi Iwai | f591490 | 2014-11-04 12:45:59 +0100 | [diff] [blame] | 448 | hw_ptr_error(substream, in_interrupt, |
| 449 | "Lost interrupts?", |
| 450 | "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 451 | substream->stream, (long)delta, |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 452 | (long)new_hw_ptr, |
| 453 | (long)old_hw_ptr); |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 454 | } |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 455 | |
Clemens Ladisch | ab69a49 | 2010-11-15 10:46:23 +0100 | [diff] [blame] | 456 | no_delta_check: |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 457 | if (runtime->status->hw_ptr == new_hw_ptr) { |
| 458 | update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 459 | return 0; |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 460 | } |
Takashi Iwai | ab1863f | 2009-06-07 12:09:17 +0200 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 463 | runtime->silence_size > 0) |
| 464 | snd_pcm_playback_silence(substream, new_hw_ptr); |
| 465 | |
Jaroslav Kysela | e763692 | 2010-01-26 17:08:24 +0100 | [diff] [blame] | 466 | if (in_interrupt) { |
Clemens Ladisch | ead4046 | 2010-05-21 09:15:59 +0200 | [diff] [blame] | 467 | delta = new_hw_ptr - runtime->hw_ptr_interrupt; |
| 468 | if (delta < 0) |
| 469 | delta += runtime->boundary; |
| 470 | delta -= (snd_pcm_uframes_t)delta % runtime->period_size; |
| 471 | runtime->hw_ptr_interrupt += delta; |
| 472 | if (runtime->hw_ptr_interrupt >= runtime->boundary) |
| 473 | runtime->hw_ptr_interrupt -= runtime->boundary; |
Jaroslav Kysela | e763692 | 2010-01-26 17:08:24 +0100 | [diff] [blame] | 474 | } |
Takashi Iwai | ed3da3d | 2009-03-03 17:00:15 +0100 | [diff] [blame] | 475 | runtime->hw_ptr_base = hw_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | runtime->status->hw_ptr = new_hw_ptr; |
Pierre-Louis Bossart | 3509a03 | 2012-05-22 14:54:02 -0500 | [diff] [blame] | 477 | runtime->hw_ptr_jiffies = curr_jiffies; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 478 | if (crossed_boundary) { |
| 479 | snd_BUG_ON(crossed_boundary != 1); |
| 480 | runtime->hw_ptr_wrap += runtime->boundary; |
| 481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 483 | update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); |
Pierre-Louis Bossart | 4eeaaea | 2012-10-22 16:42:15 -0500 | [diff] [blame] | 484 | |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 485 | return snd_pcm_update_state(substream, runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* CAUTION: call it with irq disabled */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 489 | int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 491 | return snd_pcm_update_hw_ptr0(substream, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
| 495 | * snd_pcm_set_ops - set the PCM operators |
| 496 | * @pcm: the pcm instance |
| 497 | * @direction: stream direction, SNDRV_PCM_STREAM_XXX |
| 498 | * @ops: the operator table |
| 499 | * |
| 500 | * Sets the given PCM operators to the pcm instance. |
| 501 | */ |
Lars-Peter Clausen | e6c2e7e | 2013-05-24 15:18:10 +0200 | [diff] [blame] | 502 | void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, |
| 503 | const struct snd_pcm_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 505 | struct snd_pcm_str *stream = &pcm->streams[direction]; |
| 506 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | for (substream = stream->substream; substream != NULL; substream = substream->next) |
| 509 | substream->ops = ops; |
| 510 | } |
| 511 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 512 | EXPORT_SYMBOL(snd_pcm_set_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * snd_pcm_sync - set the PCM sync id |
| 516 | * @substream: the pcm substream |
| 517 | * |
| 518 | * Sets the PCM sync identifier for the card. |
| 519 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 520 | void snd_pcm_set_sync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 522 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | runtime->sync.id32[0] = substream->pcm->card->number; |
| 525 | runtime->sync.id32[1] = -1; |
| 526 | runtime->sync.id32[2] = -1; |
| 527 | runtime->sync.id32[3] = -1; |
| 528 | } |
| 529 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 530 | EXPORT_SYMBOL(snd_pcm_set_sync); |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* |
| 533 | * Standard ioctl routine |
| 534 | */ |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | static inline unsigned int div32(unsigned int a, unsigned int b, |
| 537 | unsigned int *r) |
| 538 | { |
| 539 | if (b == 0) { |
| 540 | *r = 0; |
| 541 | return UINT_MAX; |
| 542 | } |
| 543 | *r = a % b; |
| 544 | return a / b; |
| 545 | } |
| 546 | |
| 547 | static inline unsigned int div_down(unsigned int a, unsigned int b) |
| 548 | { |
| 549 | if (b == 0) |
| 550 | return UINT_MAX; |
| 551 | return a / b; |
| 552 | } |
| 553 | |
| 554 | static inline unsigned int div_up(unsigned int a, unsigned int b) |
| 555 | { |
| 556 | unsigned int r; |
| 557 | unsigned int q; |
| 558 | if (b == 0) |
| 559 | return UINT_MAX; |
| 560 | q = div32(a, b, &r); |
| 561 | if (r) |
| 562 | ++q; |
| 563 | return q; |
| 564 | } |
| 565 | |
| 566 | static inline unsigned int mul(unsigned int a, unsigned int b) |
| 567 | { |
| 568 | if (a == 0) |
| 569 | return 0; |
| 570 | if (div_down(UINT_MAX, a) < b) |
| 571 | return UINT_MAX; |
| 572 | return a * b; |
| 573 | } |
| 574 | |
| 575 | static inline unsigned int muldiv32(unsigned int a, unsigned int b, |
| 576 | unsigned int c, unsigned int *r) |
| 577 | { |
| 578 | u_int64_t n = (u_int64_t) a * b; |
| 579 | if (c == 0) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 580 | snd_BUG_ON(!n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | *r = 0; |
| 582 | return UINT_MAX; |
| 583 | } |
Takashi Iwai | 3f7440a | 2009-06-05 17:40:04 +0200 | [diff] [blame] | 584 | n = div_u64_rem(n, c, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (n >= UINT_MAX) { |
| 586 | *r = 0; |
| 587 | return UINT_MAX; |
| 588 | } |
| 589 | return n; |
| 590 | } |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | /** |
| 593 | * snd_interval_refine - refine the interval value of configurator |
| 594 | * @i: the interval value to refine |
| 595 | * @v: the interval value to refer to |
| 596 | * |
| 597 | * Refines the interval value with the reference value. |
| 598 | * The interval is changed to the range satisfying both intervals. |
| 599 | * The interval status (min, max, integer, etc.) are evaluated. |
| 600 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 601 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 602 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 604 | 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] | 605 | { |
| 606 | int changed = 0; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 607 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 608 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (i->min < v->min) { |
| 610 | i->min = v->min; |
| 611 | i->openmin = v->openmin; |
| 612 | changed = 1; |
| 613 | } else if (i->min == v->min && !i->openmin && v->openmin) { |
| 614 | i->openmin = 1; |
| 615 | changed = 1; |
| 616 | } |
| 617 | if (i->max > v->max) { |
| 618 | i->max = v->max; |
| 619 | i->openmax = v->openmax; |
| 620 | changed = 1; |
| 621 | } else if (i->max == v->max && !i->openmax && v->openmax) { |
| 622 | i->openmax = 1; |
| 623 | changed = 1; |
| 624 | } |
| 625 | if (!i->integer && v->integer) { |
| 626 | i->integer = 1; |
| 627 | changed = 1; |
| 628 | } |
| 629 | if (i->integer) { |
| 630 | if (i->openmin) { |
| 631 | i->min++; |
| 632 | i->openmin = 0; |
| 633 | } |
| 634 | if (i->openmax) { |
| 635 | i->max--; |
| 636 | i->openmax = 0; |
| 637 | } |
| 638 | } else if (!i->openmin && !i->openmax && i->min == i->max) |
| 639 | i->integer = 1; |
| 640 | if (snd_interval_checkempty(i)) { |
| 641 | snd_interval_none(i); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | return changed; |
| 645 | } |
| 646 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 647 | EXPORT_SYMBOL(snd_interval_refine); |
| 648 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 649 | static int snd_interval_refine_first(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 651 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 652 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (snd_interval_single(i)) |
| 654 | return 0; |
| 655 | i->max = i->min; |
| 656 | i->openmax = i->openmin; |
| 657 | if (i->openmax) |
| 658 | i->max++; |
| 659 | return 1; |
| 660 | } |
| 661 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 662 | static int snd_interval_refine_last(struct snd_interval *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 664 | if (snd_BUG_ON(snd_interval_empty(i))) |
| 665 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (snd_interval_single(i)) |
| 667 | return 0; |
| 668 | i->min = i->max; |
| 669 | i->openmin = i->openmax; |
| 670 | if (i->openmin) |
| 671 | i->min--; |
| 672 | return 1; |
| 673 | } |
| 674 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 675 | 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] | 676 | { |
| 677 | if (a->empty || b->empty) { |
| 678 | snd_interval_none(c); |
| 679 | return; |
| 680 | } |
| 681 | c->empty = 0; |
| 682 | c->min = mul(a->min, b->min); |
| 683 | c->openmin = (a->openmin || b->openmin); |
| 684 | c->max = mul(a->max, b->max); |
| 685 | c->openmax = (a->openmax || b->openmax); |
| 686 | c->integer = (a->integer && b->integer); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * snd_interval_div - refine the interval value with division |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 691 | * @a: dividend |
| 692 | * @b: divisor |
| 693 | * @c: quotient |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | * |
| 695 | * c = a / b |
| 696 | * |
| 697 | * Returns non-zero if the value is changed, zero if not changed. |
| 698 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 699 | 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] | 700 | { |
| 701 | unsigned int r; |
| 702 | if (a->empty || b->empty) { |
| 703 | snd_interval_none(c); |
| 704 | return; |
| 705 | } |
| 706 | c->empty = 0; |
| 707 | c->min = div32(a->min, b->max, &r); |
| 708 | c->openmin = (r || a->openmin || b->openmax); |
| 709 | if (b->min > 0) { |
| 710 | c->max = div32(a->max, b->min, &r); |
| 711 | if (r) { |
| 712 | c->max++; |
| 713 | c->openmax = 1; |
| 714 | } else |
| 715 | c->openmax = (a->openmax || b->openmin); |
| 716 | } else { |
| 717 | c->max = UINT_MAX; |
| 718 | c->openmax = 0; |
| 719 | } |
| 720 | c->integer = 0; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * snd_interval_muldivk - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 725 | * @a: dividend 1 |
| 726 | * @b: dividend 2 |
| 727 | * @k: divisor (as integer) |
| 728 | * @c: result |
| 729 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | * c = a * b / k |
| 731 | * |
| 732 | * Returns non-zero if the value is changed, zero if not changed. |
| 733 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 734 | void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, |
| 735 | unsigned int k, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
| 737 | unsigned int r; |
| 738 | if (a->empty || b->empty) { |
| 739 | snd_interval_none(c); |
| 740 | return; |
| 741 | } |
| 742 | c->empty = 0; |
| 743 | c->min = muldiv32(a->min, b->min, k, &r); |
| 744 | c->openmin = (r || a->openmin || b->openmin); |
| 745 | c->max = muldiv32(a->max, b->max, k, &r); |
| 746 | if (r) { |
| 747 | c->max++; |
| 748 | c->openmax = 1; |
| 749 | } else |
| 750 | c->openmax = (a->openmax || b->openmax); |
| 751 | c->integer = 0; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * snd_interval_mulkdiv - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 756 | * @a: dividend 1 |
| 757 | * @k: dividend 2 (as integer) |
| 758 | * @b: divisor |
| 759 | * @c: result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * |
| 761 | * c = a * k / b |
| 762 | * |
| 763 | * Returns non-zero if the value is changed, zero if not changed. |
| 764 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 765 | void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, |
| 766 | const struct snd_interval *b, struct snd_interval *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | unsigned int r; |
| 769 | if (a->empty || b->empty) { |
| 770 | snd_interval_none(c); |
| 771 | return; |
| 772 | } |
| 773 | c->empty = 0; |
| 774 | c->min = muldiv32(a->min, k, b->max, &r); |
| 775 | c->openmin = (r || a->openmin || b->openmax); |
| 776 | if (b->min > 0) { |
| 777 | c->max = muldiv32(a->max, k, b->min, &r); |
| 778 | if (r) { |
| 779 | c->max++; |
| 780 | c->openmax = 1; |
| 781 | } else |
| 782 | c->openmax = (a->openmax || b->openmin); |
| 783 | } else { |
| 784 | c->max = UINT_MAX; |
| 785 | c->openmax = 0; |
| 786 | } |
| 787 | c->integer = 0; |
| 788 | } |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | /* ---- */ |
| 791 | |
| 792 | |
| 793 | /** |
| 794 | * snd_interval_ratnum - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 795 | * @i: interval to refine |
| 796 | * @rats_count: number of ratnum_t |
| 797 | * @rats: ratnum_t array |
| 798 | * @nump: pointer to store the resultant numerator |
| 799 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 801 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 802 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 804 | int snd_interval_ratnum(struct snd_interval *i, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 805 | unsigned int rats_count, const struct snd_ratnum *rats, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 806 | unsigned int *nump, unsigned int *denp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 808 | unsigned int best_num, best_den; |
| 809 | int best_diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 811 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | int err; |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 813 | unsigned int result_num, result_den; |
| 814 | int result_diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | best_num = best_den = best_diff = 0; |
| 817 | for (k = 0; k < rats_count; ++k) { |
| 818 | unsigned int num = rats[k].num; |
| 819 | unsigned int den; |
| 820 | unsigned int q = i->min; |
| 821 | int diff; |
| 822 | if (q == 0) |
| 823 | q = 1; |
Krzysztof Helt | 40962d7 | 2009-12-19 18:31:04 +0100 | [diff] [blame] | 824 | den = div_up(num, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (den < rats[k].den_min) |
| 826 | continue; |
| 827 | if (den > rats[k].den_max) |
| 828 | den = rats[k].den_max; |
| 829 | else { |
| 830 | unsigned int r; |
| 831 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 832 | if (r != 0) |
| 833 | den -= r; |
| 834 | } |
| 835 | diff = num - q * den; |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 836 | if (diff < 0) |
| 837 | diff = -diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (best_num == 0 || |
| 839 | diff * best_den < best_diff * den) { |
| 840 | best_diff = diff; |
| 841 | best_den = den; |
| 842 | best_num = num; |
| 843 | } |
| 844 | } |
| 845 | if (best_den == 0) { |
| 846 | i->empty = 1; |
| 847 | return -EINVAL; |
| 848 | } |
| 849 | t.min = div_down(best_num, best_den); |
| 850 | t.openmin = !!(best_num % best_den); |
| 851 | |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 852 | result_num = best_num; |
| 853 | result_diff = best_diff; |
| 854 | result_den = best_den; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | best_num = best_den = best_diff = 0; |
| 856 | for (k = 0; k < rats_count; ++k) { |
| 857 | unsigned int num = rats[k].num; |
| 858 | unsigned int den; |
| 859 | unsigned int q = i->max; |
| 860 | int diff; |
| 861 | if (q == 0) { |
| 862 | i->empty = 1; |
| 863 | return -EINVAL; |
| 864 | } |
Krzysztof Helt | 40962d7 | 2009-12-19 18:31:04 +0100 | [diff] [blame] | 865 | den = div_down(num, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (den > rats[k].den_max) |
| 867 | continue; |
| 868 | if (den < rats[k].den_min) |
| 869 | den = rats[k].den_min; |
| 870 | else { |
| 871 | unsigned int r; |
| 872 | r = (den - rats[k].den_min) % rats[k].den_step; |
| 873 | if (r != 0) |
| 874 | den += rats[k].den_step - r; |
| 875 | } |
| 876 | diff = q * den - num; |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 877 | if (diff < 0) |
| 878 | diff = -diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | if (best_num == 0 || |
| 880 | diff * best_den < best_diff * den) { |
| 881 | best_diff = diff; |
| 882 | best_den = den; |
| 883 | best_num = num; |
| 884 | } |
| 885 | } |
| 886 | if (best_den == 0) { |
| 887 | i->empty = 1; |
| 888 | return -EINVAL; |
| 889 | } |
| 890 | t.max = div_up(best_num, best_den); |
| 891 | t.openmax = !!(best_num % best_den); |
| 892 | t.integer = 0; |
| 893 | err = snd_interval_refine(i, &t); |
| 894 | if (err < 0) |
| 895 | return err; |
| 896 | |
| 897 | if (snd_interval_single(i)) { |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 898 | if (best_diff * result_den < result_diff * best_den) { |
| 899 | result_num = best_num; |
| 900 | result_den = best_den; |
| 901 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | if (nump) |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 903 | *nump = result_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | if (denp) |
Krzysztof Helt | 8374e24 | 2009-12-21 17:07:08 +0100 | [diff] [blame] | 905 | *denp = result_den; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | return err; |
| 908 | } |
| 909 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 910 | EXPORT_SYMBOL(snd_interval_ratnum); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
| 913 | * snd_interval_ratden - refine the interval value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 914 | * @i: interval to refine |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 915 | * @rats_count: number of struct ratden |
| 916 | * @rats: struct ratden array |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 917 | * @nump: pointer to store the resultant numerator |
| 918 | * @denp: pointer to store the resultant denominator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 920 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 921 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 923 | static int snd_interval_ratden(struct snd_interval *i, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 924 | unsigned int rats_count, |
| 925 | const struct snd_ratden *rats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | unsigned int *nump, unsigned int *denp) |
| 927 | { |
| 928 | unsigned int best_num, best_diff, best_den; |
| 929 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 930 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | int err; |
| 932 | |
| 933 | best_num = best_den = best_diff = 0; |
| 934 | for (k = 0; k < rats_count; ++k) { |
| 935 | unsigned int num; |
| 936 | unsigned int den = rats[k].den; |
| 937 | unsigned int q = i->min; |
| 938 | int diff; |
| 939 | num = mul(q, den); |
| 940 | if (num > rats[k].num_max) |
| 941 | continue; |
| 942 | if (num < rats[k].num_min) |
| 943 | num = rats[k].num_max; |
| 944 | else { |
| 945 | unsigned int r; |
| 946 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 947 | if (r != 0) |
| 948 | num += rats[k].num_step - r; |
| 949 | } |
| 950 | diff = num - q * den; |
| 951 | if (best_num == 0 || |
| 952 | diff * best_den < best_diff * den) { |
| 953 | best_diff = diff; |
| 954 | best_den = den; |
| 955 | best_num = num; |
| 956 | } |
| 957 | } |
| 958 | if (best_den == 0) { |
| 959 | i->empty = 1; |
| 960 | return -EINVAL; |
| 961 | } |
| 962 | t.min = div_down(best_num, best_den); |
| 963 | t.openmin = !!(best_num % best_den); |
| 964 | |
| 965 | best_num = best_den = best_diff = 0; |
| 966 | for (k = 0; k < rats_count; ++k) { |
| 967 | unsigned int num; |
| 968 | unsigned int den = rats[k].den; |
| 969 | unsigned int q = i->max; |
| 970 | int diff; |
| 971 | num = mul(q, den); |
| 972 | if (num < rats[k].num_min) |
| 973 | continue; |
| 974 | if (num > rats[k].num_max) |
| 975 | num = rats[k].num_max; |
| 976 | else { |
| 977 | unsigned int r; |
| 978 | r = (num - rats[k].num_min) % rats[k].num_step; |
| 979 | if (r != 0) |
| 980 | num -= r; |
| 981 | } |
| 982 | diff = q * den - num; |
| 983 | if (best_num == 0 || |
| 984 | diff * best_den < best_diff * den) { |
| 985 | best_diff = diff; |
| 986 | best_den = den; |
| 987 | best_num = num; |
| 988 | } |
| 989 | } |
| 990 | if (best_den == 0) { |
| 991 | i->empty = 1; |
| 992 | return -EINVAL; |
| 993 | } |
| 994 | t.max = div_up(best_num, best_den); |
| 995 | t.openmax = !!(best_num % best_den); |
| 996 | t.integer = 0; |
| 997 | err = snd_interval_refine(i, &t); |
| 998 | if (err < 0) |
| 999 | return err; |
| 1000 | |
| 1001 | if (snd_interval_single(i)) { |
| 1002 | if (nump) |
| 1003 | *nump = best_num; |
| 1004 | if (denp) |
| 1005 | *denp = best_den; |
| 1006 | } |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * snd_interval_list - refine the interval value from the list |
| 1012 | * @i: the interval value to refine |
| 1013 | * @count: the number of elements in the list |
| 1014 | * @list: the value list |
| 1015 | * @mask: the bit-mask to evaluate |
| 1016 | * |
| 1017 | * Refines the interval value from the list. |
| 1018 | * When mask is non-zero, only the elements corresponding to bit 1 are |
| 1019 | * evaluated. |
| 1020 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1021 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 1022 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | */ |
Mark Brown | 4af87a9 | 2012-03-14 19:48:43 +0000 | [diff] [blame] | 1024 | int snd_interval_list(struct snd_interval *i, unsigned int count, |
| 1025 | const unsigned int *list, unsigned int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | { |
| 1027 | unsigned int k; |
Clemens Ladisch | b1ddaf6 | 2009-08-25 08:15:41 +0200 | [diff] [blame] | 1028 | struct snd_interval list_range; |
Takashi Iwai | 0981a26 | 2007-02-01 14:53:49 +0100 | [diff] [blame] | 1029 | |
| 1030 | if (!count) { |
| 1031 | i->empty = 1; |
| 1032 | return -EINVAL; |
| 1033 | } |
Clemens Ladisch | b1ddaf6 | 2009-08-25 08:15:41 +0200 | [diff] [blame] | 1034 | snd_interval_any(&list_range); |
| 1035 | list_range.min = UINT_MAX; |
| 1036 | list_range.max = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | for (k = 0; k < count; k++) { |
| 1038 | if (mask && !(mask & (1 << k))) |
| 1039 | continue; |
Clemens Ladisch | b1ddaf6 | 2009-08-25 08:15:41 +0200 | [diff] [blame] | 1040 | if (!snd_interval_test(i, list[k])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | continue; |
Clemens Ladisch | b1ddaf6 | 2009-08-25 08:15:41 +0200 | [diff] [blame] | 1042 | list_range.min = min(list_range.min, list[k]); |
| 1043 | list_range.max = max(list_range.max, list[k]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
Clemens Ladisch | b1ddaf6 | 2009-08-25 08:15:41 +0200 | [diff] [blame] | 1045 | return snd_interval_refine(i, &list_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1048 | EXPORT_SYMBOL(snd_interval_list); |
| 1049 | |
Peter Rosin | f66f898 | 2015-01-28 15:16:06 +0100 | [diff] [blame] | 1050 | /** |
| 1051 | * snd_interval_ranges - refine the interval value from the list of ranges |
| 1052 | * @i: the interval value to refine |
| 1053 | * @count: the number of elements in the list of ranges |
| 1054 | * @ranges: the ranges list |
| 1055 | * @mask: the bit-mask to evaluate |
| 1056 | * |
| 1057 | * Refines the interval value from the list of ranges. |
| 1058 | * When mask is non-zero, only the elements corresponding to bit 1 are |
| 1059 | * evaluated. |
| 1060 | * |
| 1061 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 1062 | * negative error code. |
| 1063 | */ |
| 1064 | int snd_interval_ranges(struct snd_interval *i, unsigned int count, |
| 1065 | const struct snd_interval *ranges, unsigned int mask) |
| 1066 | { |
| 1067 | unsigned int k; |
| 1068 | struct snd_interval range_union; |
| 1069 | struct snd_interval range; |
| 1070 | |
| 1071 | if (!count) { |
| 1072 | snd_interval_none(i); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | snd_interval_any(&range_union); |
| 1076 | range_union.min = UINT_MAX; |
| 1077 | range_union.max = 0; |
| 1078 | for (k = 0; k < count; k++) { |
| 1079 | if (mask && !(mask & (1 << k))) |
| 1080 | continue; |
| 1081 | snd_interval_copy(&range, &ranges[k]); |
| 1082 | if (snd_interval_refine(&range, i) < 0) |
| 1083 | continue; |
| 1084 | if (snd_interval_empty(&range)) |
| 1085 | continue; |
| 1086 | |
| 1087 | if (range.min < range_union.min) { |
| 1088 | range_union.min = range.min; |
| 1089 | range_union.openmin = 1; |
| 1090 | } |
| 1091 | if (range.min == range_union.min && !range.openmin) |
| 1092 | range_union.openmin = 0; |
| 1093 | if (range.max > range_union.max) { |
| 1094 | range_union.max = range.max; |
| 1095 | range_union.openmax = 1; |
| 1096 | } |
| 1097 | if (range.max == range_union.max && !range.openmax) |
| 1098 | range_union.openmax = 0; |
| 1099 | } |
| 1100 | return snd_interval_refine(i, &range_union); |
| 1101 | } |
| 1102 | EXPORT_SYMBOL(snd_interval_ranges); |
| 1103 | |
Clemens Ladisch | 0f519b6 | 2014-09-07 21:43:07 +0200 | [diff] [blame] | 1104 | static int snd_interval_step(struct snd_interval *i, unsigned int step) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | { |
| 1106 | unsigned int n; |
| 1107 | int changed = 0; |
Clemens Ladisch | 0f519b6 | 2014-09-07 21:43:07 +0200 | [diff] [blame] | 1108 | n = i->min % step; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | if (n != 0 || i->openmin) { |
| 1110 | i->min += step - n; |
Clemens Ladisch | df1e471 | 2014-09-07 21:43:41 +0200 | [diff] [blame] | 1111 | i->openmin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | changed = 1; |
| 1113 | } |
Clemens Ladisch | 0f519b6 | 2014-09-07 21:43:07 +0200 | [diff] [blame] | 1114 | n = i->max % step; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | if (n != 0 || i->openmax) { |
| 1116 | i->max -= n; |
Clemens Ladisch | df1e471 | 2014-09-07 21:43:41 +0200 | [diff] [blame] | 1117 | i->openmax = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | changed = 1; |
| 1119 | } |
| 1120 | if (snd_interval_checkempty(i)) { |
| 1121 | i->empty = 1; |
| 1122 | return -EINVAL; |
| 1123 | } |
| 1124 | return changed; |
| 1125 | } |
| 1126 | |
| 1127 | /* Info constraints helpers */ |
| 1128 | |
| 1129 | /** |
| 1130 | * snd_pcm_hw_rule_add - add the hw-constraint rule |
| 1131 | * @runtime: the pcm runtime instance |
| 1132 | * @cond: condition bits |
| 1133 | * @var: the variable to evaluate |
| 1134 | * @func: the evaluation function |
| 1135 | * @private: the private data pointer passed to function |
| 1136 | * @dep: the dependent variables |
| 1137 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1138 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1140 | int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | int var, |
| 1142 | snd_pcm_hw_rule_func_t func, void *private, |
| 1143 | int dep, ...) |
| 1144 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1145 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1146 | struct snd_pcm_hw_rule *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | unsigned int k; |
| 1148 | va_list args; |
| 1149 | va_start(args, dep); |
| 1150 | if (constrs->rules_num >= constrs->rules_all) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1151 | struct snd_pcm_hw_rule *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | unsigned int new_rules = constrs->rules_all + 16; |
| 1153 | new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL); |
Jesper Juhl | 87a1c8a | 2010-12-21 00:03:17 +0100 | [diff] [blame] | 1154 | if (!new) { |
| 1155 | va_end(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | return -ENOMEM; |
Jesper Juhl | 87a1c8a | 2010-12-21 00:03:17 +0100 | [diff] [blame] | 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | if (constrs->rules) { |
| 1159 | memcpy(new, constrs->rules, |
| 1160 | constrs->rules_num * sizeof(*c)); |
| 1161 | kfree(constrs->rules); |
| 1162 | } |
| 1163 | constrs->rules = new; |
| 1164 | constrs->rules_all = new_rules; |
| 1165 | } |
| 1166 | c = &constrs->rules[constrs->rules_num]; |
| 1167 | c->cond = cond; |
| 1168 | c->func = func; |
| 1169 | c->var = var; |
| 1170 | c->private = private; |
| 1171 | k = 0; |
| 1172 | while (1) { |
Jesper Juhl | 87a1c8a | 2010-12-21 00:03:17 +0100 | [diff] [blame] | 1173 | if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) { |
| 1174 | va_end(args); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1175 | return -EINVAL; |
Jesper Juhl | 87a1c8a | 2010-12-21 00:03:17 +0100 | [diff] [blame] | 1176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | c->deps[k++] = dep; |
| 1178 | if (dep < 0) |
| 1179 | break; |
| 1180 | dep = va_arg(args, int); |
| 1181 | } |
| 1182 | constrs->rules_num++; |
| 1183 | va_end(args); |
| 1184 | return 0; |
Jesper Juhl | 87a1c8a | 2010-12-21 00:03:17 +0100 | [diff] [blame] | 1185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1187 | EXPORT_SYMBOL(snd_pcm_hw_rule_add); |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1190 | * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1191 | * @runtime: PCM runtime instance |
| 1192 | * @var: hw_params variable to apply the mask |
| 1193 | * @mask: the bitmap mask |
| 1194 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1195 | * Apply the constraint of the given bitmap mask to a 32-bit mask parameter. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1196 | * |
| 1197 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1199 | 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] | 1200 | u_int32_t mask) |
| 1201 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1202 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1203 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | *maskp->bits &= mask; |
| 1205 | memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ |
| 1206 | if (*maskp->bits == 0) |
| 1207 | return -EINVAL; |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1212 | * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1213 | * @runtime: PCM runtime instance |
| 1214 | * @var: hw_params variable to apply the mask |
| 1215 | * @mask: the 64bit bitmap mask |
| 1216 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1217 | * Apply the constraint of the given bitmap mask to a 64-bit mask parameter. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1218 | * |
| 1219 | * Return: Zero if successful, or a negative error code on failure. |
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_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | u_int64_t mask) |
| 1223 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1224 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1225 | struct snd_mask *maskp = constrs_mask(constrs, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | maskp->bits[0] &= (u_int32_t)mask; |
| 1227 | maskp->bits[1] &= (u_int32_t)(mask >> 32); |
| 1228 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ |
| 1229 | if (! maskp->bits[0] && ! maskp->bits[1]) |
| 1230 | return -EINVAL; |
| 1231 | return 0; |
| 1232 | } |
Mark Brown | 63a5d4c | 2014-02-20 12:13:52 +0900 | [diff] [blame] | 1233 | EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1236 | * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1237 | * @runtime: PCM runtime instance |
| 1238 | * @var: hw_params variable to apply the integer constraint |
| 1239 | * |
| 1240 | * Apply the constraint of integer to an interval parameter. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1241 | * |
| 1242 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 1243 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1245 | 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] | 1246 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1247 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | return snd_interval_setinteger(constrs_interval(constrs, var)); |
| 1249 | } |
| 1250 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1251 | EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1254 | * 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] | 1255 | * @runtime: PCM runtime instance |
| 1256 | * @var: hw_params variable to apply the range |
| 1257 | * @min: the minimal value |
| 1258 | * @max: the maximal value |
| 1259 | * |
| 1260 | * Apply the min/max range constraint to an interval parameter. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1261 | * |
| 1262 | * Return: Positive if the value is changed, zero if it's not changed, or a |
| 1263 | * negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1265 | 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] | 1266 | unsigned int min, unsigned int max) |
| 1267 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1268 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
| 1269 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | t.min = min; |
| 1271 | t.max = max; |
| 1272 | t.openmin = t.openmax = 0; |
| 1273 | t.integer = 0; |
| 1274 | return snd_interval_refine(constrs_interval(constrs, var), &t); |
| 1275 | } |
| 1276 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1277 | EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); |
| 1278 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1279 | static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, |
| 1280 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1282 | struct snd_pcm_hw_constraint_list *list = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1288 | * 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] | 1289 | * @runtime: PCM runtime instance |
| 1290 | * @cond: condition bits |
| 1291 | * @var: hw_params variable to apply the list constraint |
| 1292 | * @l: list |
| 1293 | * |
| 1294 | * Apply the list of constraints to an interval parameter. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1295 | * |
| 1296 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1298 | int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | unsigned int cond, |
| 1300 | snd_pcm_hw_param_t var, |
Mark Brown | 1464189 | 2012-07-05 12:15:01 +0100 | [diff] [blame] | 1301 | const struct snd_pcm_hw_constraint_list *l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | { |
| 1303 | return snd_pcm_hw_rule_add(runtime, cond, var, |
Mark Brown | 1464189 | 2012-07-05 12:15:01 +0100 | [diff] [blame] | 1304 | snd_pcm_hw_rule_list, (void *)l, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | var, -1); |
| 1306 | } |
| 1307 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1308 | EXPORT_SYMBOL(snd_pcm_hw_constraint_list); |
| 1309 | |
Peter Rosin | f66f898 | 2015-01-28 15:16:06 +0100 | [diff] [blame] | 1310 | static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params, |
| 1311 | struct snd_pcm_hw_rule *rule) |
| 1312 | { |
| 1313 | struct snd_pcm_hw_constraint_ranges *r = rule->private; |
| 1314 | return snd_interval_ranges(hw_param_interval(params, rule->var), |
| 1315 | r->count, r->ranges, r->mask); |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | /** |
| 1320 | * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter |
| 1321 | * @runtime: PCM runtime instance |
| 1322 | * @cond: condition bits |
| 1323 | * @var: hw_params variable to apply the list of range constraints |
| 1324 | * @r: ranges |
| 1325 | * |
| 1326 | * Apply the list of range constraints to an interval parameter. |
| 1327 | * |
| 1328 | * Return: Zero if successful, or a negative error code on failure. |
| 1329 | */ |
| 1330 | int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime, |
| 1331 | unsigned int cond, |
| 1332 | snd_pcm_hw_param_t var, |
| 1333 | const struct snd_pcm_hw_constraint_ranges *r) |
| 1334 | { |
| 1335 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1336 | snd_pcm_hw_rule_ranges, (void *)r, |
| 1337 | var, -1); |
| 1338 | } |
| 1339 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges); |
| 1340 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1341 | static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, |
| 1342 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1344 | const struct snd_pcm_hw_constraint_ratnums *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | unsigned int num = 0, den = 0; |
| 1346 | int err; |
| 1347 | err = snd_interval_ratnum(hw_param_interval(params, rule->var), |
| 1348 | r->nrats, r->rats, &num, &den); |
| 1349 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1350 | params->rate_num = num; |
| 1351 | params->rate_den = den; |
| 1352 | } |
| 1353 | return err; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1357 | * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1358 | * @runtime: PCM runtime instance |
| 1359 | * @cond: condition bits |
| 1360 | * @var: hw_params variable to apply the ratnums constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1361 | * @r: struct snd_ratnums constriants |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1362 | * |
| 1363 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1365 | int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | unsigned int cond, |
| 1367 | snd_pcm_hw_param_t var, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1368 | const struct snd_pcm_hw_constraint_ratnums *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | { |
| 1370 | return snd_pcm_hw_rule_add(runtime, cond, var, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1371 | snd_pcm_hw_rule_ratnums, (void *)r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | var, -1); |
| 1373 | } |
| 1374 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1375 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); |
| 1376 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1377 | static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, |
| 1378 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | { |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1380 | const struct snd_pcm_hw_constraint_ratdens *r = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | unsigned int num = 0, den = 0; |
| 1382 | int err = snd_interval_ratden(hw_param_interval(params, rule->var), |
| 1383 | r->nrats, r->rats, &num, &den); |
| 1384 | if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { |
| 1385 | params->rate_num = num; |
| 1386 | params->rate_den = den; |
| 1387 | } |
| 1388 | return err; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1392 | * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1393 | * @runtime: PCM runtime instance |
| 1394 | * @cond: condition bits |
| 1395 | * @var: hw_params variable to apply the ratdens constraint |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1396 | * @r: struct snd_ratdens constriants |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1397 | * |
| 1398 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1400 | int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | unsigned int cond, |
| 1402 | snd_pcm_hw_param_t var, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1403 | const struct snd_pcm_hw_constraint_ratdens *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
| 1405 | return snd_pcm_hw_rule_add(runtime, cond, var, |
Lars-Peter Clausen | e5e113c | 2015-10-28 11:37:53 +0100 | [diff] [blame] | 1406 | snd_pcm_hw_rule_ratdens, (void *)r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | var, -1); |
| 1408 | } |
| 1409 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1410 | EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); |
| 1411 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1412 | static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, |
| 1413 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
| 1415 | unsigned int l = (unsigned long) rule->private; |
| 1416 | int width = l & 0xffff; |
| 1417 | unsigned int msbits = l >> 16; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1418 | struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
Lars-Peter Clausen | 8ef9df5 | 2014-12-29 18:43:37 +0100 | [diff] [blame] | 1419 | |
| 1420 | if (!snd_interval_single(i)) |
| 1421 | return 0; |
| 1422 | |
| 1423 | if ((snd_interval_value(i) == width) || |
| 1424 | (width == 0 && snd_interval_value(i) > msbits)) |
Lars-Peter Clausen | 19f52fa | 2014-12-29 18:43:36 +0100 | [diff] [blame] | 1425 | params->msbits = min_not_zero(params->msbits, msbits); |
Lars-Peter Clausen | 8ef9df5 | 2014-12-29 18:43:37 +0100 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1431 | * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1432 | * @runtime: PCM runtime instance |
| 1433 | * @cond: condition bits |
| 1434 | * @width: sample bits width |
| 1435 | * @msbits: msbits width |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1436 | * |
Lars-Peter Clausen | 8ef9df5 | 2014-12-29 18:43:37 +0100 | [diff] [blame] | 1437 | * This constraint will set the number of most significant bits (msbits) if a |
| 1438 | * sample format with the specified width has been select. If width is set to 0 |
| 1439 | * the msbits will be set for any sample format with a width larger than the |
| 1440 | * specified msbits. |
| 1441 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1442 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1444 | int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | unsigned int cond, |
| 1446 | unsigned int width, |
| 1447 | unsigned int msbits) |
| 1448 | { |
| 1449 | unsigned long l = (msbits << 16) | width; |
| 1450 | return snd_pcm_hw_rule_add(runtime, cond, -1, |
| 1451 | snd_pcm_hw_rule_msbits, |
| 1452 | (void*) l, |
| 1453 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1454 | } |
| 1455 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1456 | EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); |
| 1457 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1458 | static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, |
| 1459 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | { |
| 1461 | unsigned long step = (unsigned long) rule->private; |
Clemens Ladisch | 0f519b6 | 2014-09-07 21:43:07 +0200 | [diff] [blame] | 1462 | return snd_interval_step(hw_param_interval(params, rule->var), step); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1466 | * snd_pcm_hw_constraint_step - add a hw constraint step rule |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1467 | * @runtime: PCM runtime instance |
| 1468 | * @cond: condition bits |
| 1469 | * @var: hw_params variable to apply the step constraint |
| 1470 | * @step: step size |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1471 | * |
| 1472 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1474 | int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | unsigned int cond, |
| 1476 | snd_pcm_hw_param_t var, |
| 1477 | unsigned long step) |
| 1478 | { |
| 1479 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1480 | snd_pcm_hw_rule_step, (void *) step, |
| 1481 | var, -1); |
| 1482 | } |
| 1483 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1484 | EXPORT_SYMBOL(snd_pcm_hw_constraint_step); |
| 1485 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1486 | 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] | 1487 | { |
Marcin Åšlusarz | 67c3931 | 2007-12-14 12:53:21 +0100 | [diff] [blame] | 1488 | static unsigned int pow2_sizes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, |
| 1490 | 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, |
| 1491 | 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, |
| 1492 | 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 |
| 1493 | }; |
| 1494 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 1495 | ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); |
| 1496 | } |
| 1497 | |
| 1498 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1499 | * 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] | 1500 | * @runtime: PCM runtime instance |
| 1501 | * @cond: condition bits |
| 1502 | * @var: hw_params variable to apply the power-of-2 constraint |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1503 | * |
| 1504 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1506 | int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | unsigned int cond, |
| 1508 | snd_pcm_hw_param_t var) |
| 1509 | { |
| 1510 | return snd_pcm_hw_rule_add(runtime, cond, var, |
| 1511 | snd_pcm_hw_rule_pow2, NULL, |
| 1512 | var, -1); |
| 1513 | } |
| 1514 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1515 | EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); |
| 1516 | |
Clemens Ladisch | d5b702a | 2011-09-16 23:03:02 +0200 | [diff] [blame] | 1517 | static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params, |
| 1518 | struct snd_pcm_hw_rule *rule) |
| 1519 | { |
| 1520 | unsigned int base_rate = (unsigned int)(uintptr_t)rule->private; |
| 1521 | struct snd_interval *rate; |
| 1522 | |
| 1523 | rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1524 | return snd_interval_list(rate, 1, &base_rate, 0); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling |
| 1529 | * @runtime: PCM runtime instance |
| 1530 | * @base_rate: the rate at which the hardware does not resample |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1531 | * |
| 1532 | * Return: Zero if successful, or a negative error code on failure. |
Clemens Ladisch | d5b702a | 2011-09-16 23:03:02 +0200 | [diff] [blame] | 1533 | */ |
| 1534 | int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime, |
| 1535 | unsigned int base_rate) |
| 1536 | { |
| 1537 | return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE, |
| 1538 | SNDRV_PCM_HW_PARAM_RATE, |
| 1539 | snd_pcm_hw_rule_noresample_func, |
| 1540 | (void *)(uintptr_t)base_rate, |
| 1541 | SNDRV_PCM_HW_PARAM_RATE, -1); |
| 1542 | } |
| 1543 | EXPORT_SYMBOL(snd_pcm_hw_rule_noresample); |
| 1544 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1545 | 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] | 1546 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | { |
| 1548 | if (hw_is_mask(var)) { |
| 1549 | snd_mask_any(hw_param_mask(params, var)); |
| 1550 | params->cmask |= 1 << var; |
| 1551 | params->rmask |= 1 << var; |
| 1552 | return; |
| 1553 | } |
| 1554 | if (hw_is_interval(var)) { |
| 1555 | snd_interval_any(hw_param_interval(params, var)); |
| 1556 | params->cmask |= 1 << var; |
| 1557 | params->rmask |= 1 << var; |
| 1558 | return; |
| 1559 | } |
| 1560 | snd_BUG(); |
| 1561 | } |
| 1562 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1563 | void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | { |
| 1565 | unsigned int k; |
| 1566 | memset(params, 0, sizeof(*params)); |
| 1567 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) |
| 1568 | _snd_pcm_hw_param_any(params, k); |
| 1569 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
| 1570 | _snd_pcm_hw_param_any(params, k); |
| 1571 | params->info = ~0U; |
| 1572 | } |
| 1573 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1574 | EXPORT_SYMBOL(_snd_pcm_hw_params_any); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
| 1576 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1577 | * snd_pcm_hw_param_value - return @params field @var value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1578 | * @params: the hw_params instance |
| 1579 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1580 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1582 | * Return: The value for field @var if it's fixed in configuration space |
| 1583 | * defined by @params. -%EINVAL otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1585 | int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, |
| 1586 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | { |
| 1588 | if (hw_is_mask(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1589 | const struct snd_mask *mask = hw_param_mask_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | if (!snd_mask_single(mask)) |
| 1591 | return -EINVAL; |
| 1592 | if (dir) |
| 1593 | *dir = 0; |
| 1594 | return snd_mask_value(mask); |
| 1595 | } |
| 1596 | if (hw_is_interval(var)) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1597 | const struct snd_interval *i = hw_param_interval_c(params, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | if (!snd_interval_single(i)) |
| 1599 | return -EINVAL; |
| 1600 | if (dir) |
| 1601 | *dir = i->openmin; |
| 1602 | return snd_interval_value(i); |
| 1603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | return -EINVAL; |
| 1605 | } |
| 1606 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1607 | EXPORT_SYMBOL(snd_pcm_hw_param_value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1609 | void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | snd_pcm_hw_param_t var) |
| 1611 | { |
| 1612 | if (hw_is_mask(var)) { |
| 1613 | snd_mask_none(hw_param_mask(params, var)); |
| 1614 | params->cmask |= 1 << var; |
| 1615 | params->rmask |= 1 << var; |
| 1616 | } else if (hw_is_interval(var)) { |
| 1617 | snd_interval_none(hw_param_interval(params, var)); |
| 1618 | params->cmask |= 1 << var; |
| 1619 | params->rmask |= 1 << var; |
| 1620 | } else { |
| 1621 | snd_BUG(); |
| 1622 | } |
| 1623 | } |
| 1624 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1625 | EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1627 | 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] | 1628 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | { |
| 1630 | int changed; |
| 1631 | if (hw_is_mask(var)) |
| 1632 | changed = snd_mask_refine_first(hw_param_mask(params, var)); |
| 1633 | else if (hw_is_interval(var)) |
| 1634 | changed = snd_interval_refine_first(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1635 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | if (changed) { |
| 1638 | params->cmask |= 1 << var; |
| 1639 | params->rmask |= 1 << var; |
| 1640 | } |
| 1641 | return changed; |
| 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1646 | * snd_pcm_hw_param_first - refine config space and return minimum value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1647 | * @pcm: PCM instance |
| 1648 | * @params: the hw_params instance |
| 1649 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1650 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1652 | * Inside configuration space defined by @params remove from @var all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | * values > minimum. Reduce configuration space accordingly. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1654 | * |
| 1655 | * Return: The minimum, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1657 | int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, |
| 1658 | struct snd_pcm_hw_params *params, |
| 1659 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | { |
| 1661 | int changed = _snd_pcm_hw_param_first(params, var); |
| 1662 | if (changed < 0) |
| 1663 | return changed; |
| 1664 | if (params->rmask) { |
| 1665 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1666 | if (snd_BUG_ON(err < 0)) |
| 1667 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | return snd_pcm_hw_param_value(params, var, dir); |
| 1670 | } |
| 1671 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1672 | EXPORT_SYMBOL(snd_pcm_hw_param_first); |
| 1673 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1674 | 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] | 1675 | snd_pcm_hw_param_t var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | { |
| 1677 | int changed; |
| 1678 | if (hw_is_mask(var)) |
| 1679 | changed = snd_mask_refine_last(hw_param_mask(params, var)); |
| 1680 | else if (hw_is_interval(var)) |
| 1681 | changed = snd_interval_refine_last(hw_param_interval(params, var)); |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1682 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | if (changed) { |
| 1685 | params->cmask |= 1 << var; |
| 1686 | params->rmask |= 1 << var; |
| 1687 | } |
| 1688 | return changed; |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1693 | * snd_pcm_hw_param_last - refine config space and return maximum value |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1694 | * @pcm: PCM instance |
| 1695 | * @params: the hw_params instance |
| 1696 | * @var: parameter to retrieve |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1697 | * @dir: pointer to the direction (-1,0,1) or %NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1699 | * Inside configuration space defined by @params remove from @var all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | * values < maximum. Reduce configuration space accordingly. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1701 | * |
| 1702 | * Return: The maximum, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | */ |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1704 | int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, |
| 1705 | struct snd_pcm_hw_params *params, |
| 1706 | snd_pcm_hw_param_t var, int *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | { |
| 1708 | int changed = _snd_pcm_hw_param_last(params, var); |
| 1709 | if (changed < 0) |
| 1710 | return changed; |
| 1711 | if (params->rmask) { |
| 1712 | int err = snd_pcm_hw_refine(pcm, params); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1713 | if (snd_BUG_ON(err < 0)) |
| 1714 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | } |
| 1716 | return snd_pcm_hw_param_value(params, var, dir); |
| 1717 | } |
| 1718 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1719 | EXPORT_SYMBOL(snd_pcm_hw_param_last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
| 1721 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1722 | * snd_pcm_hw_param_choose - choose a configuration defined by @params |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1723 | * @pcm: PCM instance |
| 1724 | * @params: the hw_params instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1726 | * Choose one configuration from configuration space defined by @params. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | * The configuration chosen is that obtained fixing in this order: |
| 1728 | * first access, first format, first subformat, min channels, |
| 1729 | * min rate, min period time, max buffer size, min tick time |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1730 | * |
| 1731 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | */ |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1733 | int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, |
| 1734 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | { |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1736 | static int vars[] = { |
| 1737 | SNDRV_PCM_HW_PARAM_ACCESS, |
| 1738 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1739 | SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 1740 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1741 | SNDRV_PCM_HW_PARAM_RATE, |
| 1742 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1743 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1744 | SNDRV_PCM_HW_PARAM_TICK_TIME, |
| 1745 | -1 |
| 1746 | }; |
| 1747 | int err, *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1749 | for (v = vars; *v != -1; v++) { |
| 1750 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) |
| 1751 | err = snd_pcm_hw_param_first(pcm, params, *v, NULL); |
| 1752 | else |
| 1753 | err = snd_pcm_hw_param_last(pcm, params, *v, NULL); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1754 | if (snd_BUG_ON(err < 0)) |
| 1755 | return err; |
Takashi Iwai | 2f4ca8e | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1760 | static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | void *arg) |
| 1762 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1763 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | unsigned long flags; |
| 1765 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1766 | if (snd_pcm_running(substream) && |
| 1767 | snd_pcm_update_hw_ptr(substream) >= 0) |
| 1768 | runtime->status->hw_ptr %= runtime->buffer_size; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 1769 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | runtime->status->hw_ptr = 0; |
Pierre-Louis Bossart | 0e8014d | 2012-10-22 16:42:14 -0500 | [diff] [blame] | 1771 | runtime->hw_ptr_wrap = 0; |
| 1772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1777 | 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] | 1778 | void *arg) |
| 1779 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1780 | struct snd_pcm_channel_info *info = arg; |
| 1781 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | int width; |
| 1783 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { |
| 1784 | info->offset = -1; |
| 1785 | return 0; |
| 1786 | } |
| 1787 | width = snd_pcm_format_physical_width(runtime->format); |
| 1788 | if (width < 0) |
| 1789 | return width; |
| 1790 | info->offset = 0; |
| 1791 | switch (runtime->access) { |
| 1792 | case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: |
| 1793 | case SNDRV_PCM_ACCESS_RW_INTERLEAVED: |
| 1794 | info->first = info->channel * width; |
| 1795 | info->step = runtime->channels * width; |
| 1796 | break; |
| 1797 | case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: |
| 1798 | case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: |
| 1799 | { |
| 1800 | size_t size = runtime->dma_bytes / runtime->channels; |
| 1801 | info->first = info->channel * size * 8; |
| 1802 | info->step = width; |
| 1803 | break; |
| 1804 | } |
| 1805 | default: |
| 1806 | snd_BUG(); |
| 1807 | break; |
| 1808 | } |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 1812 | static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream, |
| 1813 | void *arg) |
| 1814 | { |
| 1815 | struct snd_pcm_hw_params *params = arg; |
| 1816 | snd_pcm_format_t format; |
Clemens Ladisch | a9960e6 | 2014-09-21 22:50:57 +0200 | [diff] [blame] | 1817 | int channels; |
| 1818 | ssize_t frame_size; |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 1819 | |
| 1820 | params->fifo_size = substream->runtime->hw.fifo_size; |
| 1821 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) { |
| 1822 | format = params_format(params); |
| 1823 | channels = params_channels(params); |
Clemens Ladisch | a9960e6 | 2014-09-21 22:50:57 +0200 | [diff] [blame] | 1824 | frame_size = snd_pcm_format_size(format, channels); |
| 1825 | if (frame_size > 0) |
| 1826 | params->fifo_size /= (unsigned)frame_size; |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 1827 | } |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | /** |
| 1832 | * snd_pcm_lib_ioctl - a generic PCM ioctl callback |
| 1833 | * @substream: the pcm substream instance |
| 1834 | * @cmd: ioctl command |
| 1835 | * @arg: ioctl argument |
| 1836 | * |
| 1837 | * Processes the generic ioctl commands for PCM. |
| 1838 | * Can be passed as the ioctl callback for PCM ops. |
| 1839 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1840 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1842 | int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | unsigned int cmd, void *arg) |
| 1844 | { |
| 1845 | switch (cmd) { |
| 1846 | case SNDRV_PCM_IOCTL1_INFO: |
| 1847 | return 0; |
| 1848 | case SNDRV_PCM_IOCTL1_RESET: |
| 1849 | return snd_pcm_lib_ioctl_reset(substream, arg); |
| 1850 | case SNDRV_PCM_IOCTL1_CHANNEL_INFO: |
| 1851 | return snd_pcm_lib_ioctl_channel_info(substream, arg); |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 1852 | case SNDRV_PCM_IOCTL1_FIFO_SIZE: |
| 1853 | return snd_pcm_lib_ioctl_fifo_size(substream, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | } |
| 1855 | return -ENXIO; |
| 1856 | } |
| 1857 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1858 | EXPORT_SYMBOL(snd_pcm_lib_ioctl); |
| 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | /** |
| 1861 | * snd_pcm_period_elapsed - update the pcm status for the next period |
| 1862 | * @substream: the pcm substream instance |
| 1863 | * |
| 1864 | * This function is called from the interrupt handler when the |
| 1865 | * PCM has processed the period size. It will update the current |
Takashi Iwai | 31e8960 | 2008-01-08 18:09:57 +0100 | [diff] [blame] | 1866 | * pointer, wake up sleepers, etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | * |
| 1868 | * Even if more than one periods have elapsed since the last call, you |
| 1869 | * have to call this only once. |
| 1870 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1871 | void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1873 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | unsigned long flags; |
| 1875 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1876 | if (PCM_RUNTIME_CHECK(substream)) |
| 1877 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1881 | if (!snd_pcm_running(substream) || |
Jaroslav Kysela | f240406 | 2010-01-05 17:19:34 +0100 | [diff] [blame] | 1882 | snd_pcm_update_hw_ptr0(substream, 1) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | goto _end; |
| 1884 | |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1885 | #ifdef CONFIG_SND_PCM_TIMER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | if (substream->timer_running) |
| 1887 | snd_timer_interrupt(substream->timer, 1); |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1888 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | _end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | kill_fasync(&runtime->fasync, SIGIO, POLL_IN); |
Takashi Iwai | 3aa02cb | 2016-04-14 18:02:37 +0200 | [diff] [blame] | 1891 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1894 | EXPORT_SYMBOL(snd_pcm_period_elapsed); |
| 1895 | |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1896 | /* |
| 1897 | * Wait until avail_min data becomes available |
| 1898 | * Returns a negative error code if any error occurs during operation. |
| 1899 | * The available space is stored on availp. When err = 0 and avail = 0 |
| 1900 | * on the capture stream, it indicates the stream is in DRAINING state. |
| 1901 | */ |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 1902 | static int wait_for_avail(struct snd_pcm_substream *substream, |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1903 | snd_pcm_uframes_t *availp) |
| 1904 | { |
| 1905 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1906 | int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 1907 | wait_queue_t wait; |
| 1908 | int err = 0; |
| 1909 | snd_pcm_uframes_t avail = 0; |
Takashi Iwai | f2b3614 | 2011-05-26 08:09:38 +0200 | [diff] [blame] | 1910 | long wait_time, tout; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1911 | |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1912 | init_waitqueue_entry(&wait, current); |
| 1913 | set_current_state(TASK_INTERRUPTIBLE); |
| 1914 | add_wait_queue(&runtime->tsleep, &wait); |
| 1915 | |
Takashi Iwai | f2b3614 | 2011-05-26 08:09:38 +0200 | [diff] [blame] | 1916 | if (runtime->no_period_wakeup) |
| 1917 | wait_time = MAX_SCHEDULE_TIMEOUT; |
| 1918 | else { |
| 1919 | wait_time = 10; |
| 1920 | if (runtime->rate) { |
| 1921 | long t = runtime->period_size * 2 / runtime->rate; |
| 1922 | wait_time = max(t, wait_time); |
| 1923 | } |
| 1924 | wait_time = msecs_to_jiffies(wait_time * 1000); |
| 1925 | } |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1926 | |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1927 | for (;;) { |
| 1928 | if (signal_pending(current)) { |
| 1929 | err = -ERESTARTSYS; |
| 1930 | break; |
| 1931 | } |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1932 | |
| 1933 | /* |
| 1934 | * We need to check if space became available already |
| 1935 | * (and thus the wakeup happened already) first to close |
| 1936 | * the race of space already having become available. |
| 1937 | * This check must happen after been added to the waitqueue |
| 1938 | * and having current state be INTERRUPTIBLE. |
| 1939 | */ |
| 1940 | if (is_playback) |
| 1941 | avail = snd_pcm_playback_avail(runtime); |
| 1942 | else |
| 1943 | avail = snd_pcm_capture_avail(runtime); |
| 1944 | if (avail >= runtime->twake) |
| 1945 | break; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1946 | snd_pcm_stream_unlock_irq(substream); |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1947 | |
| 1948 | tout = schedule_timeout(wait_time); |
| 1949 | |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1950 | snd_pcm_stream_lock_irq(substream); |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1951 | set_current_state(TASK_INTERRUPTIBLE); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1952 | switch (runtime->status->state) { |
| 1953 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1954 | err = -ESTRPIPE; |
| 1955 | goto _endloop; |
| 1956 | case SNDRV_PCM_STATE_XRUN: |
| 1957 | err = -EPIPE; |
| 1958 | goto _endloop; |
| 1959 | case SNDRV_PCM_STATE_DRAINING: |
| 1960 | if (is_playback) |
| 1961 | err = -EPIPE; |
| 1962 | else |
| 1963 | avail = 0; /* indicate draining */ |
| 1964 | goto _endloop; |
| 1965 | case SNDRV_PCM_STATE_OPEN: |
| 1966 | case SNDRV_PCM_STATE_SETUP: |
| 1967 | case SNDRV_PCM_STATE_DISCONNECTED: |
| 1968 | err = -EBADFD; |
| 1969 | goto _endloop; |
JongHo Kim | ed697e1 | 2013-12-17 23:02:24 +0900 | [diff] [blame] | 1970 | case SNDRV_PCM_STATE_PAUSED: |
| 1971 | continue; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1972 | } |
| 1973 | if (!tout) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 1974 | pcm_dbg(substream->pcm, |
| 1975 | "%s write error (DMA or IRQ trouble?)\n", |
| 1976 | is_playback ? "playback" : "capture"); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1977 | err = -EIO; |
| 1978 | break; |
| 1979 | } |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1980 | } |
| 1981 | _endloop: |
Arjan van de Ven | 763437a | 2011-09-15 08:49:25 +0200 | [diff] [blame] | 1982 | set_current_state(TASK_RUNNING); |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 1983 | remove_wait_queue(&runtime->tsleep, &wait); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 1984 | *availp = avail; |
| 1985 | return err; |
| 1986 | } |
| 1987 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1988 | static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | unsigned int hwoff, |
| 1990 | unsigned long data, unsigned int off, |
| 1991 | snd_pcm_uframes_t frames) |
| 1992 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1993 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | int err; |
| 1995 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 1996 | if (substream->ops->copy) { |
| 1997 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 1998 | return err; |
| 1999 | } else { |
| 2000 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) |
| 2002 | return -EFAULT; |
| 2003 | } |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2007 | typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | unsigned long data, unsigned int off, |
| 2009 | snd_pcm_uframes_t size); |
| 2010 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2011 | 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] | 2012 | unsigned long data, |
| 2013 | snd_pcm_uframes_t size, |
| 2014 | int nonblock, |
| 2015 | transfer_f transfer) |
| 2016 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2017 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | snd_pcm_uframes_t xfer = 0; |
| 2019 | snd_pcm_uframes_t offset = 0; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2020 | snd_pcm_uframes_t avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | int err = 0; |
| 2022 | |
| 2023 | if (size == 0) |
| 2024 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
| 2026 | snd_pcm_stream_lock_irq(substream); |
| 2027 | switch (runtime->status->state) { |
| 2028 | case SNDRV_PCM_STATE_PREPARED: |
| 2029 | case SNDRV_PCM_STATE_RUNNING: |
| 2030 | case SNDRV_PCM_STATE_PAUSED: |
| 2031 | break; |
| 2032 | case SNDRV_PCM_STATE_XRUN: |
| 2033 | err = -EPIPE; |
| 2034 | goto _end_unlock; |
| 2035 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2036 | err = -ESTRPIPE; |
| 2037 | goto _end_unlock; |
| 2038 | default: |
| 2039 | err = -EBADFD; |
| 2040 | goto _end_unlock; |
| 2041 | } |
| 2042 | |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 2043 | runtime->twake = runtime->control->avail_min ? : 1; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2044 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2045 | snd_pcm_update_hw_ptr(substream); |
| 2046 | avail = snd_pcm_playback_avail(runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | while (size > 0) { |
| 2048 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | snd_pcm_uframes_t cont; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 2050 | if (!avail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | if (nonblock) { |
| 2052 | err = -EAGAIN; |
| 2053 | goto _end_unlock; |
| 2054 | } |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 2055 | runtime->twake = min_t(snd_pcm_uframes_t, size, |
| 2056 | runtime->control->avail_min ? : 1); |
| 2057 | err = wait_for_avail(substream, &avail); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 2058 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | frames = size > avail ? avail : size; |
| 2062 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 2063 | if (frames > cont) |
| 2064 | frames = cont; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2065 | if (snd_BUG_ON(!frames)) { |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 2066 | runtime->twake = 0; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2067 | snd_pcm_stream_unlock_irq(substream); |
| 2068 | return -EINVAL; |
| 2069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | appl_ptr = runtime->control->appl_ptr; |
| 2071 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 2072 | snd_pcm_stream_unlock_irq(substream); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2073 | err = transfer(substream, appl_ofs, data, offset, frames); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | snd_pcm_stream_lock_irq(substream); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2075 | if (err < 0) |
| 2076 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | switch (runtime->status->state) { |
| 2078 | case SNDRV_PCM_STATE_XRUN: |
| 2079 | err = -EPIPE; |
| 2080 | goto _end_unlock; |
| 2081 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2082 | err = -ESTRPIPE; |
| 2083 | goto _end_unlock; |
| 2084 | default: |
| 2085 | break; |
| 2086 | } |
| 2087 | appl_ptr += frames; |
| 2088 | if (appl_ptr >= runtime->boundary) |
| 2089 | appl_ptr -= runtime->boundary; |
| 2090 | runtime->control->appl_ptr = appl_ptr; |
| 2091 | if (substream->ops->ack) |
| 2092 | substream->ops->ack(substream); |
| 2093 | |
| 2094 | offset += frames; |
| 2095 | size -= frames; |
| 2096 | xfer += frames; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2097 | avail -= frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && |
| 2099 | snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { |
| 2100 | err = snd_pcm_start(substream); |
| 2101 | if (err < 0) |
| 2102 | goto _end_unlock; |
| 2103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | _end_unlock: |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 2106 | runtime->twake = 0; |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2107 | if (xfer > 0 && err >= 0) |
| 2108 | snd_pcm_update_state(substream, runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | snd_pcm_stream_unlock_irq(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 2111 | } |
| 2112 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2113 | /* sanity-check for read/write methods */ |
| 2114 | static int pcm_sanity_check(struct snd_pcm_substream *substream) |
| 2115 | { |
| 2116 | struct snd_pcm_runtime *runtime; |
| 2117 | if (PCM_RUNTIME_CHECK(substream)) |
| 2118 | return -ENXIO; |
| 2119 | runtime = substream->runtime; |
| 2120 | if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area)) |
| 2121 | return -EINVAL; |
| 2122 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2123 | return -EBADFD; |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2127 | 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] | 2128 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2129 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2131 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2133 | err = pcm_sanity_check(substream); |
| 2134 | if (err < 0) |
| 2135 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2137 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
| 2139 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && |
| 2140 | runtime->channels > 1) |
| 2141 | return -EINVAL; |
| 2142 | return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock, |
| 2143 | snd_pcm_lib_write_transfer); |
| 2144 | } |
| 2145 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2146 | EXPORT_SYMBOL(snd_pcm_lib_write); |
| 2147 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2148 | static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | unsigned int hwoff, |
| 2150 | unsigned long data, unsigned int off, |
| 2151 | snd_pcm_uframes_t frames) |
| 2152 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2153 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | int err; |
| 2155 | void __user **bufs = (void __user **)data; |
| 2156 | int channels = runtime->channels; |
| 2157 | int c; |
| 2158 | if (substream->ops->copy) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2159 | if (snd_BUG_ON(!substream->ops->silence)) |
| 2160 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2162 | if (*bufs == NULL) { |
| 2163 | if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0) |
| 2164 | return err; |
| 2165 | } else { |
| 2166 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 2167 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2168 | return err; |
| 2169 | } |
| 2170 | } |
| 2171 | } else { |
| 2172 | /* default transfer behaviour */ |
| 2173 | size_t dma_csize = runtime->dma_bytes / channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2175 | char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2176 | if (*bufs == NULL) { |
| 2177 | snd_pcm_format_set_silence(runtime->format, hwbuf, frames); |
| 2178 | } else { |
| 2179 | char __user *buf = *bufs + samples_to_bytes(runtime, off); |
| 2180 | if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames))) |
| 2181 | return -EFAULT; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2188 | 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] | 2189 | void __user **bufs, |
| 2190 | snd_pcm_uframes_t frames) |
| 2191 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2192 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2194 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2196 | err = pcm_sanity_check(substream); |
| 2197 | if (err < 0) |
| 2198 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2200 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
| 2202 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2203 | return -EINVAL; |
| 2204 | return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames, |
| 2205 | nonblock, snd_pcm_lib_writev_transfer); |
| 2206 | } |
| 2207 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2208 | EXPORT_SYMBOL(snd_pcm_lib_writev); |
| 2209 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2210 | static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | unsigned int hwoff, |
| 2212 | unsigned long data, unsigned int off, |
| 2213 | snd_pcm_uframes_t frames) |
| 2214 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2215 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | int err; |
| 2217 | char __user *buf = (char __user *) data + frames_to_bytes(runtime, off); |
| 2218 | if (substream->ops->copy) { |
| 2219 | if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0) |
| 2220 | return err; |
| 2221 | } else { |
| 2222 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) |
| 2224 | return -EFAULT; |
| 2225 | } |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2229 | 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] | 2230 | unsigned long data, |
| 2231 | snd_pcm_uframes_t size, |
| 2232 | int nonblock, |
| 2233 | transfer_f transfer) |
| 2234 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2235 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | snd_pcm_uframes_t xfer = 0; |
| 2237 | snd_pcm_uframes_t offset = 0; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2238 | snd_pcm_uframes_t avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | int err = 0; |
| 2240 | |
| 2241 | if (size == 0) |
| 2242 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
| 2244 | snd_pcm_stream_lock_irq(substream); |
| 2245 | switch (runtime->status->state) { |
| 2246 | case SNDRV_PCM_STATE_PREPARED: |
| 2247 | if (size >= runtime->start_threshold) { |
| 2248 | err = snd_pcm_start(substream); |
| 2249 | if (err < 0) |
| 2250 | goto _end_unlock; |
| 2251 | } |
| 2252 | break; |
| 2253 | case SNDRV_PCM_STATE_DRAINING: |
| 2254 | case SNDRV_PCM_STATE_RUNNING: |
| 2255 | case SNDRV_PCM_STATE_PAUSED: |
| 2256 | break; |
| 2257 | case SNDRV_PCM_STATE_XRUN: |
| 2258 | err = -EPIPE; |
| 2259 | goto _end_unlock; |
| 2260 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2261 | err = -ESTRPIPE; |
| 2262 | goto _end_unlock; |
| 2263 | default: |
| 2264 | err = -EBADFD; |
| 2265 | goto _end_unlock; |
| 2266 | } |
| 2267 | |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 2268 | runtime->twake = runtime->control->avail_min ? : 1; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2269 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
| 2270 | snd_pcm_update_hw_ptr(substream); |
| 2271 | avail = snd_pcm_capture_avail(runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | while (size > 0) { |
| 2273 | snd_pcm_uframes_t frames, appl_ptr, appl_ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | snd_pcm_uframes_t cont; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 2275 | if (!avail) { |
| 2276 | if (runtime->status->state == |
| 2277 | SNDRV_PCM_STATE_DRAINING) { |
| 2278 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | goto _end_unlock; |
| 2280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | if (nonblock) { |
| 2282 | err = -EAGAIN; |
| 2283 | goto _end_unlock; |
| 2284 | } |
David Dillow | 5daeba3 | 2010-06-27 00:13:20 +0200 | [diff] [blame] | 2285 | runtime->twake = min_t(snd_pcm_uframes_t, size, |
| 2286 | runtime->control->avail_min ? : 1); |
| 2287 | err = wait_for_avail(substream, &avail); |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 2288 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | goto _end_unlock; |
Takashi Iwai | 1307551 | 2008-01-08 18:08:14 +0100 | [diff] [blame] | 2290 | if (!avail) |
| 2291 | continue; /* draining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | frames = size > avail ? avail : size; |
| 2294 | cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size; |
| 2295 | if (frames > cont) |
| 2296 | frames = cont; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2297 | if (snd_BUG_ON(!frames)) { |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 2298 | runtime->twake = 0; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2299 | snd_pcm_stream_unlock_irq(substream); |
| 2300 | return -EINVAL; |
| 2301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | appl_ptr = runtime->control->appl_ptr; |
| 2303 | appl_ofs = appl_ptr % runtime->buffer_size; |
| 2304 | snd_pcm_stream_unlock_irq(substream); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2305 | err = transfer(substream, appl_ofs, data, offset, frames); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | snd_pcm_stream_lock_irq(substream); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2307 | if (err < 0) |
| 2308 | goto _end_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | switch (runtime->status->state) { |
| 2310 | case SNDRV_PCM_STATE_XRUN: |
| 2311 | err = -EPIPE; |
| 2312 | goto _end_unlock; |
| 2313 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2314 | err = -ESTRPIPE; |
| 2315 | goto _end_unlock; |
| 2316 | default: |
| 2317 | break; |
| 2318 | } |
| 2319 | appl_ptr += frames; |
| 2320 | if (appl_ptr >= runtime->boundary) |
| 2321 | appl_ptr -= runtime->boundary; |
| 2322 | runtime->control->appl_ptr = appl_ptr; |
| 2323 | if (substream->ops->ack) |
| 2324 | substream->ops->ack(substream); |
| 2325 | |
| 2326 | offset += frames; |
| 2327 | size -= frames; |
| 2328 | xfer += frames; |
Takashi Iwai | 0910c21 | 2012-05-11 17:50:49 +0200 | [diff] [blame] | 2329 | avail -= frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | } |
| 2331 | _end_unlock: |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 2332 | runtime->twake = 0; |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 2333 | if (xfer > 0 && err >= 0) |
| 2334 | snd_pcm_update_state(substream, runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | snd_pcm_stream_unlock_irq(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; |
| 2337 | } |
| 2338 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2339 | 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] | 2340 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2341 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2343 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2345 | err = pcm_sanity_check(substream); |
| 2346 | if (err < 0) |
| 2347 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | runtime = substream->runtime; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2349 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
| 2351 | return -EINVAL; |
| 2352 | return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer); |
| 2353 | } |
| 2354 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2355 | EXPORT_SYMBOL(snd_pcm_lib_read); |
| 2356 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2357 | static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | unsigned int hwoff, |
| 2359 | unsigned long data, unsigned int off, |
| 2360 | snd_pcm_uframes_t frames) |
| 2361 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2362 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | int err; |
| 2364 | void __user **bufs = (void __user **)data; |
| 2365 | int channels = runtime->channels; |
| 2366 | int c; |
| 2367 | if (substream->ops->copy) { |
| 2368 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2369 | char __user *buf; |
| 2370 | if (*bufs == NULL) |
| 2371 | continue; |
| 2372 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2373 | if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0) |
| 2374 | return err; |
| 2375 | } |
| 2376 | } else { |
| 2377 | snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | for (c = 0; c < channels; ++c, ++bufs) { |
| 2379 | char *hwbuf; |
| 2380 | char __user *buf; |
| 2381 | if (*bufs == NULL) |
| 2382 | continue; |
| 2383 | |
| 2384 | hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff); |
| 2385 | buf = *bufs + samples_to_bytes(runtime, off); |
| 2386 | if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames))) |
| 2387 | return -EFAULT; |
| 2388 | } |
| 2389 | } |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2393 | 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] | 2394 | void __user **bufs, |
| 2395 | snd_pcm_uframes_t frames) |
| 2396 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2397 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | int nonblock; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2399 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2401 | err = pcm_sanity_check(substream); |
| 2402 | if (err < 0) |
| 2403 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2406 | return -EBADFD; |
| 2407 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2408 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 2410 | return -EINVAL; |
| 2411 | return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer); |
| 2412 | } |
| 2413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | EXPORT_SYMBOL(snd_pcm_lib_readv); |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2415 | |
| 2416 | /* |
| 2417 | * standard channel mapping helpers |
| 2418 | */ |
| 2419 | |
| 2420 | /* default channel maps for multi-channel playbacks, up to 8 channels */ |
| 2421 | const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = { |
| 2422 | { .channels = 1, |
Takashi Iwai | 5efbc26 | 2012-09-13 14:48:46 +0200 | [diff] [blame] | 2423 | .map = { SNDRV_CHMAP_MONO } }, |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2424 | { .channels = 2, |
| 2425 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 2426 | { .channels = 4, |
| 2427 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2428 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, |
| 2429 | { .channels = 6, |
| 2430 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2431 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, |
| 2432 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } }, |
| 2433 | { .channels = 8, |
| 2434 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2435 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, |
| 2436 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, |
| 2437 | SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, |
| 2438 | { } |
| 2439 | }; |
| 2440 | EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps); |
| 2441 | |
| 2442 | /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */ |
| 2443 | const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = { |
| 2444 | { .channels = 1, |
Takashi Iwai | 5efbc26 | 2012-09-13 14:48:46 +0200 | [diff] [blame] | 2445 | .map = { SNDRV_CHMAP_MONO } }, |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2446 | { .channels = 2, |
| 2447 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 2448 | { .channels = 4, |
| 2449 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2450 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, |
| 2451 | { .channels = 6, |
| 2452 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2453 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, |
| 2454 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, |
| 2455 | { .channels = 8, |
| 2456 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 2457 | SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, |
| 2458 | SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, |
| 2459 | SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, |
| 2460 | { } |
| 2461 | }; |
| 2462 | EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps); |
| 2463 | |
| 2464 | static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch) |
| 2465 | { |
| 2466 | if (ch > info->max_channels) |
| 2467 | return false; |
| 2468 | return !info->channel_mask || (info->channel_mask & (1U << ch)); |
| 2469 | } |
| 2470 | |
| 2471 | static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol, |
| 2472 | struct snd_ctl_elem_info *uinfo) |
| 2473 | { |
| 2474 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 2475 | |
| 2476 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2477 | uinfo->count = 0; |
| 2478 | uinfo->count = info->max_channels; |
| 2479 | uinfo->value.integer.min = 0; |
| 2480 | uinfo->value.integer.max = SNDRV_CHMAP_LAST; |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | /* get callback for channel map ctl element |
| 2485 | * stores the channel position firstly matching with the current channels |
| 2486 | */ |
| 2487 | static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol, |
| 2488 | struct snd_ctl_elem_value *ucontrol) |
| 2489 | { |
| 2490 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 2491 | unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); |
| 2492 | struct snd_pcm_substream *substream; |
| 2493 | const struct snd_pcm_chmap_elem *map; |
| 2494 | |
| 2495 | if (snd_BUG_ON(!info->chmap)) |
| 2496 | return -EINVAL; |
| 2497 | substream = snd_pcm_chmap_substream(info, idx); |
| 2498 | if (!substream) |
| 2499 | return -ENODEV; |
| 2500 | memset(ucontrol->value.integer.value, 0, |
| 2501 | sizeof(ucontrol->value.integer.value)); |
| 2502 | if (!substream->runtime) |
| 2503 | return 0; /* no channels set */ |
| 2504 | for (map = info->chmap; map->channels; map++) { |
| 2505 | int i; |
| 2506 | if (map->channels == substream->runtime->channels && |
| 2507 | valid_chmap_channels(info, map->channels)) { |
| 2508 | for (i = 0; i < map->channels; i++) |
| 2509 | ucontrol->value.integer.value[i] = map->map[i]; |
| 2510 | return 0; |
| 2511 | } |
| 2512 | } |
| 2513 | return -EINVAL; |
| 2514 | } |
| 2515 | |
| 2516 | /* tlv callback for channel map ctl element |
| 2517 | * expands the pre-defined channel maps in a form of TLV |
| 2518 | */ |
| 2519 | static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2520 | unsigned int size, unsigned int __user *tlv) |
| 2521 | { |
| 2522 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 2523 | const struct snd_pcm_chmap_elem *map; |
| 2524 | unsigned int __user *dst; |
| 2525 | int c, count = 0; |
| 2526 | |
| 2527 | if (snd_BUG_ON(!info->chmap)) |
| 2528 | return -EINVAL; |
| 2529 | if (size < 8) |
| 2530 | return -ENOMEM; |
| 2531 | if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv)) |
| 2532 | return -EFAULT; |
| 2533 | size -= 8; |
| 2534 | dst = tlv + 2; |
| 2535 | for (map = info->chmap; map->channels; map++) { |
| 2536 | int chs_bytes = map->channels * 4; |
| 2537 | if (!valid_chmap_channels(info, map->channels)) |
| 2538 | continue; |
| 2539 | if (size < 8) |
| 2540 | return -ENOMEM; |
| 2541 | if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) || |
| 2542 | put_user(chs_bytes, dst + 1)) |
| 2543 | return -EFAULT; |
| 2544 | dst += 2; |
| 2545 | size -= 8; |
| 2546 | count += 8; |
| 2547 | if (size < chs_bytes) |
| 2548 | return -ENOMEM; |
| 2549 | size -= chs_bytes; |
| 2550 | count += chs_bytes; |
| 2551 | for (c = 0; c < map->channels; c++) { |
| 2552 | if (put_user(map->map[c], dst)) |
| 2553 | return -EFAULT; |
| 2554 | dst++; |
| 2555 | } |
| 2556 | } |
| 2557 | if (put_user(count, tlv + 1)) |
| 2558 | return -EFAULT; |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
| 2562 | static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol) |
| 2563 | { |
| 2564 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 2565 | info->pcm->streams[info->stream].chmap_kctl = NULL; |
| 2566 | kfree(info); |
| 2567 | } |
| 2568 | |
| 2569 | /** |
| 2570 | * snd_pcm_add_chmap_ctls - create channel-mapping control elements |
| 2571 | * @pcm: the assigned PCM instance |
| 2572 | * @stream: stream direction |
| 2573 | * @chmap: channel map elements (for query) |
| 2574 | * @max_channels: the max number of channels for the stream |
| 2575 | * @private_value: the value passed to each kcontrol's private_value field |
| 2576 | * @info_ret: store struct snd_pcm_chmap instance if non-NULL |
| 2577 | * |
| 2578 | * Create channel-mapping control elements assigned to the given PCM stream(s). |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 2579 | * Return: Zero if successful, or a negative error value. |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2580 | */ |
| 2581 | int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream, |
| 2582 | const struct snd_pcm_chmap_elem *chmap, |
| 2583 | int max_channels, |
| 2584 | unsigned long private_value, |
| 2585 | struct snd_pcm_chmap **info_ret) |
| 2586 | { |
| 2587 | struct snd_pcm_chmap *info; |
| 2588 | struct snd_kcontrol_new knew = { |
| 2589 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 2590 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2591 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 2592 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, |
| 2593 | .info = pcm_chmap_ctl_info, |
| 2594 | .get = pcm_chmap_ctl_get, |
| 2595 | .tlv.c = pcm_chmap_ctl_tlv, |
| 2596 | }; |
| 2597 | int err; |
| 2598 | |
Takashi Iwai | 8d879be | 2016-05-10 16:07:40 +0200 | [diff] [blame] | 2599 | if (WARN_ON(pcm->streams[stream].chmap_kctl)) |
| 2600 | return -EBUSY; |
Takashi Iwai | 2d3391e | 2012-07-27 18:27:00 +0200 | [diff] [blame] | 2601 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 2602 | if (!info) |
| 2603 | return -ENOMEM; |
| 2604 | info->pcm = pcm; |
| 2605 | info->stream = stream; |
| 2606 | info->chmap = chmap; |
| 2607 | info->max_channels = max_channels; |
| 2608 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 2609 | knew.name = "Playback Channel Map"; |
| 2610 | else |
| 2611 | knew.name = "Capture Channel Map"; |
| 2612 | knew.device = pcm->device; |
| 2613 | knew.count = pcm->streams[stream].substream_count; |
| 2614 | knew.private_value = private_value; |
| 2615 | info->kctl = snd_ctl_new1(&knew, info); |
| 2616 | if (!info->kctl) { |
| 2617 | kfree(info); |
| 2618 | return -ENOMEM; |
| 2619 | } |
| 2620 | info->kctl->private_free = pcm_chmap_ctl_private_free; |
| 2621 | err = snd_ctl_add(pcm->card, info->kctl); |
| 2622 | if (err < 0) |
| 2623 | return err; |
| 2624 | pcm->streams[stream].chmap_kctl = info->kctl; |
| 2625 | if (info_ret) |
| 2626 | *info_ret = info; |
| 2627 | return 0; |
| 2628 | } |
| 2629 | EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls); |