blob: e73b6e4135f62d90f6c912c3a913fd52231cb74b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/time.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020026#include <linux/math64.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090036#include "pcm_local.h"
37
Takashi Iwaif5914902014-11-04 12:45:59 +010038#ifdef CONFIG_SND_PCM_XRUN_DEBUG
39#define CREATE_TRACE_POINTS
40#include "pcm_trace.h"
41#else
42#define trace_hwptr(substream, pos, in_interrupt)
43#define trace_xrun(substream)
44#define trace_hw_ptr_error(substream, reason)
Takashi Sakamotofccf5382017-06-12 09:41:45 +090045#define trace_applptr(substream, prev, curr)
Takashi Iwaif5914902014-11-04 12:45:59 +010046#endif
47
Takashi Iwaia9cd29e2017-05-24 18:18:15 +020048static int fill_silence_frames(struct snd_pcm_substream *substream,
49 snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * fill ring buffer with silence
53 * runtime->silence_start: starting pointer to silence area
54 * runtime->silence_filled: size filled with silence
55 * runtime->silence_threshold: threshold from application
56 * runtime->silence_size: maximal size from application
57 *
58 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
59 */
Takashi Iwai877211f2005-11-17 13:59:38 +010060void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Takashi Iwai877211f2005-11-17 13:59:38 +010062 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 snd_pcm_uframes_t frames, ofs, transfer;
Takashi Iwai29d1a872017-05-10 20:02:35 +020064 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (runtime->silence_size < runtime->boundary) {
67 snd_pcm_sframes_t noise_dist, n;
68 if (runtime->silence_start != runtime->control->appl_ptr) {
69 n = runtime->control->appl_ptr - runtime->silence_start;
70 if (n < 0)
71 n += runtime->boundary;
72 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
73 runtime->silence_filled -= n;
74 else
75 runtime->silence_filled = 0;
76 runtime->silence_start = runtime->control->appl_ptr;
77 }
Takashi Iwai235475c2005-12-07 15:28:07 +010078 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
81 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
82 return;
83 frames = runtime->silence_threshold - noise_dist;
84 if (frames > runtime->silence_size)
85 frames = runtime->silence_size;
86 } else {
87 if (new_hw_ptr == ULONG_MAX) { /* initialization */
88 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020089 if (avail > runtime->buffer_size)
90 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 runtime->silence_filled = avail > 0 ? avail : 0;
92 runtime->silence_start = (runtime->status->hw_ptr +
93 runtime->silence_filled) %
94 runtime->boundary;
95 } else {
96 ofs = runtime->status->hw_ptr;
97 frames = new_hw_ptr - ofs;
98 if ((snd_pcm_sframes_t)frames < 0)
99 frames += runtime->boundary;
100 runtime->silence_filled -= frames;
101 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
102 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200103 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200105 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 frames = runtime->buffer_size - runtime->silence_filled;
109 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200110 if (snd_BUG_ON(frames > runtime->buffer_size))
111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (frames == 0)
113 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200114 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 while (frames > 0) {
116 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
Takashi Iwaia9cd29e2017-05-24 18:18:15 +0200117 err = fill_silence_frames(substream, ofs, transfer);
118 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 runtime->silence_filled += transfer;
120 frames -= transfer;
121 ofs = 0;
122 }
123}
124
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200125#ifdef CONFIG_SND_DEBUG
126void snd_pcm_debug_name(struct snd_pcm_substream *substream,
Takashi Iwaic0070112009-06-08 15:58:48 +0200127 char *name, size_t len)
128{
129 snprintf(name, len, "pcmC%dD%d%c:%d",
130 substream->pcm->card->number,
131 substream->pcm->device,
132 substream->stream ? 'c' : 'p',
133 substream->number);
134}
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200135EXPORT_SYMBOL(snd_pcm_debug_name);
136#endif
Takashi Iwaic0070112009-06-08 15:58:48 +0200137
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100138#define XRUN_DEBUG_BASIC (1<<0)
139#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
140#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100141
142#ifdef CONFIG_SND_PCM_XRUN_DEBUG
143
144#define xrun_debug(substream, mask) \
145 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200146#else
147#define xrun_debug(substream, mask) 0
148#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100149
150#define dump_stack_on_xrun(substream) do { \
151 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
152 dump_stack(); \
153 } while (0)
154
Takashi Iwai877211f2005-11-17 13:59:38 +0100155static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200157 struct snd_pcm_runtime *runtime = substream->runtime;
158
Takashi Iwaif5914902014-11-04 12:45:59 +0100159 trace_xrun(substream);
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200160 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
161 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100163 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200164 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200165 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100166 pcm_warn(substream->pcm, "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100167 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jarkko Nikula0f170142010-03-26 16:07:25 +0200171#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwaif5914902014-11-04 12:45:59 +0100172#define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100173 do { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100174 trace_hw_ptr_error(substream, reason); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100175 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100176 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
177 (in_interrupt) ? 'Q' : 'P', ##args); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100178 dump_stack_on_xrun(substream); \
179 } \
180 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100182#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
183
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100184#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100185
186#endif
187
Jaroslav Kysela12509322010-01-07 15:36:31 +0100188int snd_pcm_update_state(struct snd_pcm_substream *substream,
189 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 snd_pcm_uframes_t avail;
192
193 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
194 avail = snd_pcm_playback_avail(runtime);
195 else
196 avail = snd_pcm_capture_avail(runtime);
197 if (avail > runtime->avail_max)
198 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200199 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
200 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200202 return -EPIPE;
203 }
204 } else {
205 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200207 return -EPIPE;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
David Dillow5daeba32010-06-27 00:13:20 +0200210 if (runtime->twake) {
211 if (avail >= runtime->twake)
212 wake_up(&runtime->tsleep);
213 } else if (avail >= runtime->control->avail_min)
214 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600218static void update_audio_tstamp(struct snd_pcm_substream *substream,
219 struct timespec *curr_tstamp,
220 struct timespec *audio_tstamp)
221{
222 struct snd_pcm_runtime *runtime = substream->runtime;
223 u64 audio_frames, audio_nsecs;
224 struct timespec driver_tstamp;
225
226 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
227 return;
228
229 if (!(substream->ops->get_time_info) ||
230 (runtime->audio_tstamp_report.actual_type ==
231 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
232
233 /*
234 * provide audio timestamp derived from pointer position
235 * add delay only if requested
236 */
237
238 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
239
240 if (runtime->audio_tstamp_config.report_delay) {
241 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
242 audio_frames -= runtime->delay;
243 else
244 audio_frames += runtime->delay;
245 }
246 audio_nsecs = div_u64(audio_frames * 1000000000LL,
247 runtime->rate);
248 *audio_tstamp = ns_to_timespec(audio_nsecs);
249 }
250 runtime->status->audio_tstamp = *audio_tstamp;
251 runtime->status->tstamp = *curr_tstamp;
252
253 /*
254 * re-take a driver timestamp to let apps detect if the reference tstamp
255 * read by low-level hardware was provided with a delay
256 */
257 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
258 runtime->driver_tstamp = driver_tstamp;
259}
260
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100261static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
262 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Takashi Iwai877211f2005-11-17 13:59:38 +0100264 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100266 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200267 snd_pcm_sframes_t hdelta, delta;
268 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500269 unsigned long curr_jiffies;
270 struct timespec curr_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500271 struct timespec audio_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500272 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200274 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500275
276 /*
277 * group pointer, time and jiffies reads to allow for more
278 * accurate correlations/corrections.
279 * The values are stored at the end of this routine after
280 * corrections for hw_ptr position
281 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100282 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500283 curr_jiffies = jiffies;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500284 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600285 if ((substream->ops->get_time_info) &&
286 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
287 substream->ops->get_time_info(substream, &curr_tstamp,
288 &audio_tstamp,
289 &runtime->audio_tstamp_config,
290 &runtime->audio_tstamp_report);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500291
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600292 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
293 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
294 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
295 } else
296 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500297 }
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (pos == SNDRV_PCM_POS_XRUN) {
300 xrun(substream);
301 return -EPIPE;
302 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100303 if (pos >= runtime->buffer_size) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100304 if (printk_ratelimit()) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100305 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200306 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100307 pcm_err(substream->pcm,
Takashi Iwai0ab1ace2016-03-10 20:56:20 +0100308 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
Takashi Iwai09e56df2014-02-04 18:19:48 +0100309 name, pos, runtime->buffer_size,
310 runtime->period_size);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100311 }
312 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200313 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100314 pos -= pos % runtime->min_align;
Takashi Iwaif5914902014-11-04 12:45:59 +0100315 trace_hwptr(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100316 hw_base = runtime->hw_ptr_base;
317 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100318 if (in_interrupt) {
319 /* we know that one period was processed */
320 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100321 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100322 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200323 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500324 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Koro Chen13a98832015-05-13 22:39:03 +0800325 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200326 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500327 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200328 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500329 crossed_boundary++;
330 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200331 new_hw_ptr = hw_base + pos;
332 goto __delta;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100336 /* new_hw_ptr might be lower than old_hw_ptr in case when */
337 /* pointer crosses the end of the ring buffer */
338 if (new_hw_ptr < old_hw_ptr) {
339 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500340 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100341 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500342 crossed_boundary++;
343 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100344 new_hw_ptr = hw_base + pos;
345 }
346 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200347 delta = new_hw_ptr - old_hw_ptr;
348 if (delta < 0)
349 delta += runtime->boundary;
Clemens Ladischab69a492010-11-15 10:46:23 +0100350
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100351 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200352 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100353 /*
354 * Without regular period interrupts, we have to check
355 * the elapsed time to detect xruns.
356 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500357 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Clemens Ladisch47228e42010-11-18 09:53:07 +0100358 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
359 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100360 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200361 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
362 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100363 delta += runtime->buffer_size;
364 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500365 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100366 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500367 crossed_boundary++;
368 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100369 new_hw_ptr = hw_base + pos;
370 hdelta -= runtime->hw_ptr_buffer_jiffies;
371 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100372 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100373 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100374
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100375 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100376 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100377 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
378 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
379 substream->stream, (long)pos,
380 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100381 return 0;
382 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200383
384 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100385 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200386 goto no_jiffies_check;
387
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200388 /* Skip the jiffies check for hardwares with BATCH flag.
389 * Such hardware usually just increases the position at each IRQ,
390 * thus it can't give any strange position.
391 */
392 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
393 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100394 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200395 if (hdelta < runtime->delay)
396 goto no_jiffies_check;
397 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500398 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200399 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
400 delta = jdelta /
401 (((runtime->period_size * HZ) / runtime->rate)
402 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100403 /* move new_hw_ptr according jiffies not pos variable */
404 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100405 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100406 /* use loop to avoid checks for delta overflows */
407 /* the delta value is small or zero in most cases */
408 while (delta > 0) {
409 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500410 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100411 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500412 crossed_boundary--;
413 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100414 delta--;
415 }
416 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100417 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
418 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200419 (long)pos, (long)hdelta,
420 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100421 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100422 (unsigned long)old_hw_ptr,
423 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100424 /* reset values to proper state */
425 delta = 0;
426 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200427 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200428 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200429 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100430 hw_ptr_error(substream, in_interrupt,
431 "Lost interrupts?",
432 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100433 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100434 (long)new_hw_ptr,
435 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100436 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100437
Clemens Ladischab69a492010-11-15 10:46:23 +0100438 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600439 if (runtime->status->hw_ptr == new_hw_ptr) {
440 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100441 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600442 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
445 runtime->silence_size > 0)
446 snd_pcm_playback_silence(substream, new_hw_ptr);
447
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100448 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200449 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
450 if (delta < 0)
451 delta += runtime->boundary;
452 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
453 runtime->hw_ptr_interrupt += delta;
454 if (runtime->hw_ptr_interrupt >= runtime->boundary)
455 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100456 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100457 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500459 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500460 if (crossed_boundary) {
461 snd_BUG_ON(crossed_boundary != 1);
462 runtime->hw_ptr_wrap += runtime->boundary;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600465 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500466
Jaroslav Kysela12509322010-01-07 15:36:31 +0100467 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100471int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100473 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/**
477 * snd_pcm_set_ops - set the PCM operators
478 * @pcm: the pcm instance
479 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
480 * @ops: the operator table
481 *
482 * Sets the given PCM operators to the pcm instance.
483 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200484void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
485 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwai877211f2005-11-17 13:59:38 +0100487 struct snd_pcm_str *stream = &pcm->streams[direction];
488 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 for (substream = stream->substream; substream != NULL; substream = substream->next)
491 substream->ops = ops;
492}
493
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200494EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496/**
497 * snd_pcm_sync - set the PCM sync id
498 * @substream: the pcm substream
499 *
500 * Sets the PCM sync identifier for the card.
501 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100502void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Takashi Iwai877211f2005-11-17 13:59:38 +0100504 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 runtime->sync.id32[0] = substream->pcm->card->number;
507 runtime->sync.id32[1] = -1;
508 runtime->sync.id32[2] = -1;
509 runtime->sync.id32[3] = -1;
510}
511
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200512EXPORT_SYMBOL(snd_pcm_set_sync);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * Standard ioctl routine
516 */
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static inline unsigned int div32(unsigned int a, unsigned int b,
519 unsigned int *r)
520{
521 if (b == 0) {
522 *r = 0;
523 return UINT_MAX;
524 }
525 *r = a % b;
526 return a / b;
527}
528
529static inline unsigned int div_down(unsigned int a, unsigned int b)
530{
531 if (b == 0)
532 return UINT_MAX;
533 return a / b;
534}
535
536static inline unsigned int div_up(unsigned int a, unsigned int b)
537{
538 unsigned int r;
539 unsigned int q;
540 if (b == 0)
541 return UINT_MAX;
542 q = div32(a, b, &r);
543 if (r)
544 ++q;
545 return q;
546}
547
548static inline unsigned int mul(unsigned int a, unsigned int b)
549{
550 if (a == 0)
551 return 0;
552 if (div_down(UINT_MAX, a) < b)
553 return UINT_MAX;
554 return a * b;
555}
556
557static inline unsigned int muldiv32(unsigned int a, unsigned int b,
558 unsigned int c, unsigned int *r)
559{
560 u_int64_t n = (u_int64_t) a * b;
561 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200562 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *r = 0;
564 return UINT_MAX;
565 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200566 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (n >= UINT_MAX) {
568 *r = 0;
569 return UINT_MAX;
570 }
571 return n;
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/**
575 * snd_interval_refine - refine the interval value of configurator
576 * @i: the interval value to refine
577 * @v: the interval value to refer to
578 *
579 * Refines the interval value with the reference value.
580 * The interval is changed to the range satisfying both intervals.
581 * The interval status (min, max, integer, etc.) are evaluated.
582 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100583 * Return: Positive if the value is changed, zero if it's not changed, or a
584 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100586int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200589 if (snd_BUG_ON(snd_interval_empty(i)))
590 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (i->min < v->min) {
592 i->min = v->min;
593 i->openmin = v->openmin;
594 changed = 1;
595 } else if (i->min == v->min && !i->openmin && v->openmin) {
596 i->openmin = 1;
597 changed = 1;
598 }
599 if (i->max > v->max) {
600 i->max = v->max;
601 i->openmax = v->openmax;
602 changed = 1;
603 } else if (i->max == v->max && !i->openmax && v->openmax) {
604 i->openmax = 1;
605 changed = 1;
606 }
607 if (!i->integer && v->integer) {
608 i->integer = 1;
609 changed = 1;
610 }
611 if (i->integer) {
612 if (i->openmin) {
613 i->min++;
614 i->openmin = 0;
615 }
616 if (i->openmax) {
617 i->max--;
618 i->openmax = 0;
619 }
620 } else if (!i->openmin && !i->openmax && i->min == i->max)
621 i->integer = 1;
622 if (snd_interval_checkempty(i)) {
623 snd_interval_none(i);
624 return -EINVAL;
625 }
626 return changed;
627}
628
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200629EXPORT_SYMBOL(snd_interval_refine);
630
Takashi Iwai877211f2005-11-17 13:59:38 +0100631static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200633 if (snd_BUG_ON(snd_interval_empty(i)))
634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (snd_interval_single(i))
636 return 0;
637 i->max = i->min;
638 i->openmax = i->openmin;
639 if (i->openmax)
640 i->max++;
641 return 1;
642}
643
Takashi Iwai877211f2005-11-17 13:59:38 +0100644static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200646 if (snd_BUG_ON(snd_interval_empty(i)))
647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (snd_interval_single(i))
649 return 0;
650 i->min = i->max;
651 i->openmin = i->openmax;
652 if (i->openmin)
653 i->min--;
654 return 1;
655}
656
Takashi Iwai877211f2005-11-17 13:59:38 +0100657void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 if (a->empty || b->empty) {
660 snd_interval_none(c);
661 return;
662 }
663 c->empty = 0;
664 c->min = mul(a->min, b->min);
665 c->openmin = (a->openmin || b->openmin);
666 c->max = mul(a->max, b->max);
667 c->openmax = (a->openmax || b->openmax);
668 c->integer = (a->integer && b->integer);
669}
670
671/**
672 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200673 * @a: dividend
674 * @b: divisor
675 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *
677 * c = a / b
678 *
679 * Returns non-zero if the value is changed, zero if not changed.
680 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100681void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 unsigned int r;
684 if (a->empty || b->empty) {
685 snd_interval_none(c);
686 return;
687 }
688 c->empty = 0;
689 c->min = div32(a->min, b->max, &r);
690 c->openmin = (r || a->openmin || b->openmax);
691 if (b->min > 0) {
692 c->max = div32(a->max, b->min, &r);
693 if (r) {
694 c->max++;
695 c->openmax = 1;
696 } else
697 c->openmax = (a->openmax || b->openmin);
698 } else {
699 c->max = UINT_MAX;
700 c->openmax = 0;
701 }
702 c->integer = 0;
703}
704
705/**
706 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200707 * @a: dividend 1
708 * @b: dividend 2
709 * @k: divisor (as integer)
710 * @c: result
711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * c = a * b / k
713 *
714 * Returns non-zero if the value is changed, zero if not changed.
715 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100716void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
717 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 unsigned int r;
720 if (a->empty || b->empty) {
721 snd_interval_none(c);
722 return;
723 }
724 c->empty = 0;
725 c->min = muldiv32(a->min, b->min, k, &r);
726 c->openmin = (r || a->openmin || b->openmin);
727 c->max = muldiv32(a->max, b->max, k, &r);
728 if (r) {
729 c->max++;
730 c->openmax = 1;
731 } else
732 c->openmax = (a->openmax || b->openmax);
733 c->integer = 0;
734}
735
736/**
737 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200738 * @a: dividend 1
739 * @k: dividend 2 (as integer)
740 * @b: divisor
741 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
743 * c = a * k / b
744 *
745 * Returns non-zero if the value is changed, zero if not changed.
746 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100747void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
748 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 unsigned int r;
751 if (a->empty || b->empty) {
752 snd_interval_none(c);
753 return;
754 }
755 c->empty = 0;
756 c->min = muldiv32(a->min, k, b->max, &r);
757 c->openmin = (r || a->openmin || b->openmax);
758 if (b->min > 0) {
759 c->max = muldiv32(a->max, k, b->min, &r);
760 if (r) {
761 c->max++;
762 c->openmax = 1;
763 } else
764 c->openmax = (a->openmax || b->openmin);
765 } else {
766 c->max = UINT_MAX;
767 c->openmax = 0;
768 }
769 c->integer = 0;
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/* ---- */
773
774
775/**
776 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200777 * @i: interval to refine
778 * @rats_count: number of ratnum_t
779 * @rats: ratnum_t array
780 * @nump: pointer to store the resultant numerator
781 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100783 * Return: Positive if the value is changed, zero if it's not changed, or a
784 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100786int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100787 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100788 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100790 unsigned int best_num, best_den;
791 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100793 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100795 unsigned int result_num, result_den;
796 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 best_num = best_den = best_diff = 0;
799 for (k = 0; k < rats_count; ++k) {
800 unsigned int num = rats[k].num;
801 unsigned int den;
802 unsigned int q = i->min;
803 int diff;
804 if (q == 0)
805 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100806 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (den < rats[k].den_min)
808 continue;
809 if (den > rats[k].den_max)
810 den = rats[k].den_max;
811 else {
812 unsigned int r;
813 r = (den - rats[k].den_min) % rats[k].den_step;
814 if (r != 0)
815 den -= r;
816 }
817 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100818 if (diff < 0)
819 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (best_num == 0 ||
821 diff * best_den < best_diff * den) {
822 best_diff = diff;
823 best_den = den;
824 best_num = num;
825 }
826 }
827 if (best_den == 0) {
828 i->empty = 1;
829 return -EINVAL;
830 }
831 t.min = div_down(best_num, best_den);
832 t.openmin = !!(best_num % best_den);
833
Krzysztof Helt8374e242009-12-21 17:07:08 +0100834 result_num = best_num;
835 result_diff = best_diff;
836 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 best_num = best_den = best_diff = 0;
838 for (k = 0; k < rats_count; ++k) {
839 unsigned int num = rats[k].num;
840 unsigned int den;
841 unsigned int q = i->max;
842 int diff;
843 if (q == 0) {
844 i->empty = 1;
845 return -EINVAL;
846 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100847 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (den > rats[k].den_max)
849 continue;
850 if (den < rats[k].den_min)
851 den = rats[k].den_min;
852 else {
853 unsigned int r;
854 r = (den - rats[k].den_min) % rats[k].den_step;
855 if (r != 0)
856 den += rats[k].den_step - r;
857 }
858 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100859 if (diff < 0)
860 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (best_num == 0 ||
862 diff * best_den < best_diff * den) {
863 best_diff = diff;
864 best_den = den;
865 best_num = num;
866 }
867 }
868 if (best_den == 0) {
869 i->empty = 1;
870 return -EINVAL;
871 }
872 t.max = div_up(best_num, best_den);
873 t.openmax = !!(best_num % best_den);
874 t.integer = 0;
875 err = snd_interval_refine(i, &t);
876 if (err < 0)
877 return err;
878
879 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100880 if (best_diff * result_den < result_diff * best_den) {
881 result_num = best_num;
882 result_den = best_den;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100885 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100887 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 return err;
890}
891
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200892EXPORT_SYMBOL(snd_interval_ratnum);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/**
895 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200896 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100897 * @rats_count: number of struct ratden
898 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200899 * @nump: pointer to store the resultant numerator
900 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100902 * Return: Positive if the value is changed, zero if it's not changed, or a
903 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100905static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100906 unsigned int rats_count,
907 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned int *nump, unsigned int *denp)
909{
910 unsigned int best_num, best_diff, best_den;
911 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100912 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int err;
914
915 best_num = best_den = best_diff = 0;
916 for (k = 0; k < rats_count; ++k) {
917 unsigned int num;
918 unsigned int den = rats[k].den;
919 unsigned int q = i->min;
920 int diff;
921 num = mul(q, den);
922 if (num > rats[k].num_max)
923 continue;
924 if (num < rats[k].num_min)
925 num = rats[k].num_max;
926 else {
927 unsigned int r;
928 r = (num - rats[k].num_min) % rats[k].num_step;
929 if (r != 0)
930 num += rats[k].num_step - r;
931 }
932 diff = num - q * den;
933 if (best_num == 0 ||
934 diff * best_den < best_diff * den) {
935 best_diff = diff;
936 best_den = den;
937 best_num = num;
938 }
939 }
940 if (best_den == 0) {
941 i->empty = 1;
942 return -EINVAL;
943 }
944 t.min = div_down(best_num, best_den);
945 t.openmin = !!(best_num % best_den);
946
947 best_num = best_den = best_diff = 0;
948 for (k = 0; k < rats_count; ++k) {
949 unsigned int num;
950 unsigned int den = rats[k].den;
951 unsigned int q = i->max;
952 int diff;
953 num = mul(q, den);
954 if (num < rats[k].num_min)
955 continue;
956 if (num > rats[k].num_max)
957 num = rats[k].num_max;
958 else {
959 unsigned int r;
960 r = (num - rats[k].num_min) % rats[k].num_step;
961 if (r != 0)
962 num -= r;
963 }
964 diff = q * den - num;
965 if (best_num == 0 ||
966 diff * best_den < best_diff * den) {
967 best_diff = diff;
968 best_den = den;
969 best_num = num;
970 }
971 }
972 if (best_den == 0) {
973 i->empty = 1;
974 return -EINVAL;
975 }
976 t.max = div_up(best_num, best_den);
977 t.openmax = !!(best_num % best_den);
978 t.integer = 0;
979 err = snd_interval_refine(i, &t);
980 if (err < 0)
981 return err;
982
983 if (snd_interval_single(i)) {
984 if (nump)
985 *nump = best_num;
986 if (denp)
987 *denp = best_den;
988 }
989 return err;
990}
991
992/**
993 * snd_interval_list - refine the interval value from the list
994 * @i: the interval value to refine
995 * @count: the number of elements in the list
996 * @list: the value list
997 * @mask: the bit-mask to evaluate
998 *
999 * Refines the interval value from the list.
1000 * When mask is non-zero, only the elements corresponding to bit 1 are
1001 * evaluated.
1002 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001003 * Return: Positive if the value is changed, zero if it's not changed, or a
1004 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Mark Brown4af87a92012-03-14 19:48:43 +00001006int snd_interval_list(struct snd_interval *i, unsigned int count,
1007 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001010 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001011
1012 if (!count) {
1013 i->empty = 1;
1014 return -EINVAL;
1015 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001016 snd_interval_any(&list_range);
1017 list_range.min = UINT_MAX;
1018 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 for (k = 0; k < count; k++) {
1020 if (mask && !(mask & (1 << k)))
1021 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001022 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001024 list_range.min = min(list_range.min, list[k]);
1025 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001027 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001030EXPORT_SYMBOL(snd_interval_list);
1031
Peter Rosinf66f8982015-01-28 15:16:06 +01001032/**
1033 * snd_interval_ranges - refine the interval value from the list of ranges
1034 * @i: the interval value to refine
1035 * @count: the number of elements in the list of ranges
1036 * @ranges: the ranges list
1037 * @mask: the bit-mask to evaluate
1038 *
1039 * Refines the interval value from the list of ranges.
1040 * When mask is non-zero, only the elements corresponding to bit 1 are
1041 * evaluated.
1042 *
1043 * Return: Positive if the value is changed, zero if it's not changed, or a
1044 * negative error code.
1045 */
1046int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1047 const struct snd_interval *ranges, unsigned int mask)
1048{
1049 unsigned int k;
1050 struct snd_interval range_union;
1051 struct snd_interval range;
1052
1053 if (!count) {
1054 snd_interval_none(i);
1055 return -EINVAL;
1056 }
1057 snd_interval_any(&range_union);
1058 range_union.min = UINT_MAX;
1059 range_union.max = 0;
1060 for (k = 0; k < count; k++) {
1061 if (mask && !(mask & (1 << k)))
1062 continue;
1063 snd_interval_copy(&range, &ranges[k]);
1064 if (snd_interval_refine(&range, i) < 0)
1065 continue;
1066 if (snd_interval_empty(&range))
1067 continue;
1068
1069 if (range.min < range_union.min) {
1070 range_union.min = range.min;
1071 range_union.openmin = 1;
1072 }
1073 if (range.min == range_union.min && !range.openmin)
1074 range_union.openmin = 0;
1075 if (range.max > range_union.max) {
1076 range_union.max = range.max;
1077 range_union.openmax = 1;
1078 }
1079 if (range.max == range_union.max && !range.openmax)
1080 range_union.openmax = 0;
1081 }
1082 return snd_interval_refine(i, &range_union);
1083}
1084EXPORT_SYMBOL(snd_interval_ranges);
1085
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001086static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 unsigned int n;
1089 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001090 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (n != 0 || i->openmin) {
1092 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001093 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 changed = 1;
1095 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001096 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (n != 0 || i->openmax) {
1098 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001099 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 changed = 1;
1101 }
1102 if (snd_interval_checkempty(i)) {
1103 i->empty = 1;
1104 return -EINVAL;
1105 }
1106 return changed;
1107}
1108
1109/* Info constraints helpers */
1110
1111/**
1112 * snd_pcm_hw_rule_add - add the hw-constraint rule
1113 * @runtime: the pcm runtime instance
1114 * @cond: condition bits
1115 * @var: the variable to evaluate
1116 * @func: the evaluation function
1117 * @private: the private data pointer passed to function
1118 * @dep: the dependent variables
1119 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001120 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001122int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int var,
1124 snd_pcm_hw_rule_func_t func, void *private,
1125 int dep, ...)
1126{
Takashi Iwai877211f2005-11-17 13:59:38 +01001127 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1128 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 unsigned int k;
1130 va_list args;
1131 va_start(args, dep);
1132 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001133 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unsigned int new_rules = constrs->rules_all + 16;
1135 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001136 if (!new) {
1137 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (constrs->rules) {
1141 memcpy(new, constrs->rules,
1142 constrs->rules_num * sizeof(*c));
1143 kfree(constrs->rules);
1144 }
1145 constrs->rules = new;
1146 constrs->rules_all = new_rules;
1147 }
1148 c = &constrs->rules[constrs->rules_num];
1149 c->cond = cond;
1150 c->func = func;
1151 c->var = var;
1152 c->private = private;
1153 k = 0;
1154 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001155 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1156 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001157 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 c->deps[k++] = dep;
1160 if (dep < 0)
1161 break;
1162 dep = va_arg(args, int);
1163 }
1164 constrs->rules_num++;
1165 va_end(args);
1166 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001169EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001172 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001173 * @runtime: PCM runtime instance
1174 * @var: hw_params variable to apply the mask
1175 * @mask: the bitmap mask
1176 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001177 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001178 *
1179 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001181int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 u_int32_t mask)
1183{
Takashi Iwai877211f2005-11-17 13:59:38 +01001184 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1185 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *maskp->bits &= mask;
1187 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1188 if (*maskp->bits == 0)
1189 return -EINVAL;
1190 return 0;
1191}
1192
1193/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001194 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001195 * @runtime: PCM runtime instance
1196 * @var: hw_params variable to apply the mask
1197 * @mask: the 64bit bitmap mask
1198 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001199 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001200 *
1201 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001203int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 u_int64_t mask)
1205{
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1207 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 maskp->bits[0] &= (u_int32_t)mask;
1209 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1210 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1211 if (! maskp->bits[0] && ! maskp->bits[1])
1212 return -EINVAL;
1213 return 0;
1214}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001215EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001218 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001219 * @runtime: PCM runtime instance
1220 * @var: hw_params variable to apply the integer constraint
1221 *
1222 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001223 *
1224 * Return: Positive if the value is changed, zero if it's not changed, or a
1225 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001227int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Takashi Iwai877211f2005-11-17 13:59:38 +01001229 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return snd_interval_setinteger(constrs_interval(constrs, var));
1231}
1232
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001233EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001236 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001237 * @runtime: PCM runtime instance
1238 * @var: hw_params variable to apply the range
1239 * @min: the minimal value
1240 * @max: the maximal value
1241 *
1242 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001243 *
1244 * Return: Positive if the value is changed, zero if it's not changed, or a
1245 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001247int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 unsigned int min, unsigned int max)
1249{
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1251 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 t.min = min;
1253 t.max = max;
1254 t.openmin = t.openmax = 0;
1255 t.integer = 0;
1256 return snd_interval_refine(constrs_interval(constrs, var), &t);
1257}
1258
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001259EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1260
Takashi Iwai877211f2005-11-17 13:59:38 +01001261static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1262 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Takashi Iwai877211f2005-11-17 13:59:38 +01001264 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1266}
1267
1268
1269/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001270 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001271 * @runtime: PCM runtime instance
1272 * @cond: condition bits
1273 * @var: hw_params variable to apply the list constraint
1274 * @l: list
1275 *
1276 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001277 *
1278 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001280int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned int cond,
1282 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001283 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001286 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 var, -1);
1288}
1289
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001290EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1291
Peter Rosinf66f8982015-01-28 15:16:06 +01001292static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1293 struct snd_pcm_hw_rule *rule)
1294{
1295 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1296 return snd_interval_ranges(hw_param_interval(params, rule->var),
1297 r->count, r->ranges, r->mask);
1298}
1299
1300
1301/**
1302 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1303 * @runtime: PCM runtime instance
1304 * @cond: condition bits
1305 * @var: hw_params variable to apply the list of range constraints
1306 * @r: ranges
1307 *
1308 * Apply the list of range constraints to an interval parameter.
1309 *
1310 * Return: Zero if successful, or a negative error code on failure.
1311 */
1312int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1313 unsigned int cond,
1314 snd_pcm_hw_param_t var,
1315 const struct snd_pcm_hw_constraint_ranges *r)
1316{
1317 return snd_pcm_hw_rule_add(runtime, cond, var,
1318 snd_pcm_hw_rule_ranges, (void *)r,
1319 var, -1);
1320}
1321EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1322
Takashi Iwai877211f2005-11-17 13:59:38 +01001323static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1324 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001326 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 unsigned int num = 0, den = 0;
1328 int err;
1329 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1330 r->nrats, r->rats, &num, &den);
1331 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1332 params->rate_num = num;
1333 params->rate_den = den;
1334 }
1335 return err;
1336}
1337
1338/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001339 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001340 * @runtime: PCM runtime instance
1341 * @cond: condition bits
1342 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001343 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001344 *
1345 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001347int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned int cond,
1349 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001350 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001353 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 var, -1);
1355}
1356
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001357EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1358
Takashi Iwai877211f2005-11-17 13:59:38 +01001359static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1360 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001362 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 unsigned int num = 0, den = 0;
1364 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1365 r->nrats, r->rats, &num, &den);
1366 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1367 params->rate_num = num;
1368 params->rate_den = den;
1369 }
1370 return err;
1371}
1372
1373/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001374 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001375 * @runtime: PCM runtime instance
1376 * @cond: condition bits
1377 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001378 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001379 *
1380 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001382int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 unsigned int cond,
1384 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001385 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001388 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 var, -1);
1390}
1391
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001392EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1393
Takashi Iwai877211f2005-11-17 13:59:38 +01001394static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1395 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 unsigned int l = (unsigned long) rule->private;
1398 int width = l & 0xffff;
1399 unsigned int msbits = l >> 16;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09001400 const struct snd_interval *i =
1401 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001402
1403 if (!snd_interval_single(i))
1404 return 0;
1405
1406 if ((snd_interval_value(i) == width) ||
1407 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001408 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411}
1412
1413/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001414 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001415 * @runtime: PCM runtime instance
1416 * @cond: condition bits
1417 * @width: sample bits width
1418 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001419 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001420 * This constraint will set the number of most significant bits (msbits) if a
1421 * sample format with the specified width has been select. If width is set to 0
1422 * the msbits will be set for any sample format with a width larger than the
1423 * specified msbits.
1424 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001425 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001427int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 unsigned int cond,
1429 unsigned int width,
1430 unsigned int msbits)
1431{
1432 unsigned long l = (msbits << 16) | width;
1433 return snd_pcm_hw_rule_add(runtime, cond, -1,
1434 snd_pcm_hw_rule_msbits,
1435 (void*) l,
1436 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1437}
1438
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001439EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1440
Takashi Iwai877211f2005-11-17 13:59:38 +01001441static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1442 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001445 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001449 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001450 * @runtime: PCM runtime instance
1451 * @cond: condition bits
1452 * @var: hw_params variable to apply the step constraint
1453 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001454 *
1455 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001457int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 unsigned int cond,
1459 snd_pcm_hw_param_t var,
1460 unsigned long step)
1461{
1462 return snd_pcm_hw_rule_add(runtime, cond, var,
1463 snd_pcm_hw_rule_step, (void *) step,
1464 var, -1);
1465}
1466
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001467EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1468
Takashi Iwai877211f2005-11-17 13:59:38 +01001469static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001471 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1473 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1474 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1475 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1476 };
1477 return snd_interval_list(hw_param_interval(params, rule->var),
1478 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1479}
1480
1481/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001482 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001483 * @runtime: PCM runtime instance
1484 * @cond: condition bits
1485 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001486 *
1487 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001489int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 unsigned int cond,
1491 snd_pcm_hw_param_t var)
1492{
1493 return snd_pcm_hw_rule_add(runtime, cond, var,
1494 snd_pcm_hw_rule_pow2, NULL,
1495 var, -1);
1496}
1497
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001498EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1499
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001500static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1501 struct snd_pcm_hw_rule *rule)
1502{
1503 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1504 struct snd_interval *rate;
1505
1506 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1507 return snd_interval_list(rate, 1, &base_rate, 0);
1508}
1509
1510/**
1511 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1512 * @runtime: PCM runtime instance
1513 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001514 *
1515 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001516 */
1517int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1518 unsigned int base_rate)
1519{
1520 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1521 SNDRV_PCM_HW_PARAM_RATE,
1522 snd_pcm_hw_rule_noresample_func,
1523 (void *)(uintptr_t)base_rate,
1524 SNDRV_PCM_HW_PARAM_RATE, -1);
1525}
1526EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1527
Takashi Iwai877211f2005-11-17 13:59:38 +01001528static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001529 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 if (hw_is_mask(var)) {
1532 snd_mask_any(hw_param_mask(params, var));
1533 params->cmask |= 1 << var;
1534 params->rmask |= 1 << var;
1535 return;
1536 }
1537 if (hw_is_interval(var)) {
1538 snd_interval_any(hw_param_interval(params, var));
1539 params->cmask |= 1 << var;
1540 params->rmask |= 1 << var;
1541 return;
1542 }
1543 snd_BUG();
1544}
1545
Takashi Iwai877211f2005-11-17 13:59:38 +01001546void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 unsigned int k;
1549 memset(params, 0, sizeof(*params));
1550 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1551 _snd_pcm_hw_param_any(params, k);
1552 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1553 _snd_pcm_hw_param_any(params, k);
1554 params->info = ~0U;
1555}
1556
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001557EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001560 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001561 * @params: the hw_params instance
1562 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001563 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001565 * Return: The value for field @var if it's fixed in configuration space
1566 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001568int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1569 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001572 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!snd_mask_single(mask))
1574 return -EINVAL;
1575 if (dir)
1576 *dir = 0;
1577 return snd_mask_value(mask);
1578 }
1579 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001580 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (!snd_interval_single(i))
1582 return -EINVAL;
1583 if (dir)
1584 *dir = i->openmin;
1585 return snd_interval_value(i);
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EINVAL;
1588}
1589
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001590EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Takashi Iwai877211f2005-11-17 13:59:38 +01001592void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 snd_pcm_hw_param_t var)
1594{
1595 if (hw_is_mask(var)) {
1596 snd_mask_none(hw_param_mask(params, var));
1597 params->cmask |= 1 << var;
1598 params->rmask |= 1 << var;
1599 } else if (hw_is_interval(var)) {
1600 snd_interval_none(hw_param_interval(params, var));
1601 params->cmask |= 1 << var;
1602 params->rmask |= 1 << var;
1603 } else {
1604 snd_BUG();
1605 }
1606}
1607
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001608EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwai877211f2005-11-17 13:59:38 +01001610static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001611 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 int changed;
1614 if (hw_is_mask(var))
1615 changed = snd_mask_refine_first(hw_param_mask(params, var));
1616 else if (hw_is_interval(var))
1617 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001618 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (changed) {
1621 params->cmask |= 1 << var;
1622 params->rmask |= 1 << var;
1623 }
1624 return changed;
1625}
1626
1627
1628/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001629 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001630 * @pcm: PCM instance
1631 * @params: the hw_params instance
1632 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001633 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001635 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001637 *
1638 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001640int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1641 struct snd_pcm_hw_params *params,
1642 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 int changed = _snd_pcm_hw_param_first(params, var);
1645 if (changed < 0)
1646 return changed;
1647 if (params->rmask) {
1648 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001649 if (snd_BUG_ON(err < 0))
1650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652 return snd_pcm_hw_param_value(params, var, dir);
1653}
1654
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001655EXPORT_SYMBOL(snd_pcm_hw_param_first);
1656
Takashi Iwai877211f2005-11-17 13:59:38 +01001657static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001658 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 int changed;
1661 if (hw_is_mask(var))
1662 changed = snd_mask_refine_last(hw_param_mask(params, var));
1663 else if (hw_is_interval(var))
1664 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001665 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (changed) {
1668 params->cmask |= 1 << var;
1669 params->rmask |= 1 << var;
1670 }
1671 return changed;
1672}
1673
1674
1675/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001676 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001677 * @pcm: PCM instance
1678 * @params: the hw_params instance
1679 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001680 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001682 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001684 *
1685 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001687int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1688 struct snd_pcm_hw_params *params,
1689 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 int changed = _snd_pcm_hw_param_last(params, var);
1692 if (changed < 0)
1693 return changed;
1694 if (params->rmask) {
1695 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001696 if (snd_BUG_ON(err < 0))
1697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 return snd_pcm_hw_param_value(params, var, dir);
1700}
1701
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001702EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Takashi Iwai877211f2005-11-17 13:59:38 +01001704static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 void *arg)
1706{
Takashi Iwai877211f2005-11-17 13:59:38 +01001707 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 unsigned long flags;
1709 snd_pcm_stream_lock_irqsave(substream, flags);
1710 if (snd_pcm_running(substream) &&
1711 snd_pcm_update_hw_ptr(substream) >= 0)
1712 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001713 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001715 runtime->hw_ptr_wrap = 0;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 snd_pcm_stream_unlock_irqrestore(substream, flags);
1718 return 0;
1719}
1720
Takashi Iwai877211f2005-11-17 13:59:38 +01001721static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 void *arg)
1723{
Takashi Iwai877211f2005-11-17 13:59:38 +01001724 struct snd_pcm_channel_info *info = arg;
1725 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int width;
1727 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1728 info->offset = -1;
1729 return 0;
1730 }
1731 width = snd_pcm_format_physical_width(runtime->format);
1732 if (width < 0)
1733 return width;
1734 info->offset = 0;
1735 switch (runtime->access) {
1736 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1737 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1738 info->first = info->channel * width;
1739 info->step = runtime->channels * width;
1740 break;
1741 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1742 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1743 {
1744 size_t size = runtime->dma_bytes / runtime->channels;
1745 info->first = info->channel * size * 8;
1746 info->step = width;
1747 break;
1748 }
1749 default:
1750 snd_BUG();
1751 break;
1752 }
1753 return 0;
1754}
1755
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001756static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1757 void *arg)
1758{
1759 struct snd_pcm_hw_params *params = arg;
1760 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001761 int channels;
1762 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001763
1764 params->fifo_size = substream->runtime->hw.fifo_size;
1765 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1766 format = params_format(params);
1767 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001768 frame_size = snd_pcm_format_size(format, channels);
1769 if (frame_size > 0)
1770 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001771 }
1772 return 0;
1773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/**
1776 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1777 * @substream: the pcm substream instance
1778 * @cmd: ioctl command
1779 * @arg: ioctl argument
1780 *
1781 * Processes the generic ioctl commands for PCM.
1782 * Can be passed as the ioctl callback for PCM ops.
1783 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001784 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001786int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 unsigned int cmd, void *arg)
1788{
1789 switch (cmd) {
1790 case SNDRV_PCM_IOCTL1_INFO:
1791 return 0;
1792 case SNDRV_PCM_IOCTL1_RESET:
1793 return snd_pcm_lib_ioctl_reset(substream, arg);
1794 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1795 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001796 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1797 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 return -ENXIO;
1800}
1801
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001802EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/**
1805 * snd_pcm_period_elapsed - update the pcm status for the next period
1806 * @substream: the pcm substream instance
1807 *
1808 * This function is called from the interrupt handler when the
1809 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001810 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 *
1812 * Even if more than one periods have elapsed since the last call, you
1813 * have to call this only once.
1814 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001815void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Takashi Iwai877211f2005-11-17 13:59:38 +01001817 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 unsigned long flags;
1819
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001820 if (PCM_RUNTIME_CHECK(substream))
1821 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 snd_pcm_stream_lock_irqsave(substream, flags);
1825 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001826 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto _end;
1828
Jie Yang90bbaf62015-10-16 17:57:46 +08001829#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (substream->timer_running)
1831 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001835 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001838EXPORT_SYMBOL(snd_pcm_period_elapsed);
1839
Takashi Iwai13075512008-01-08 18:08:14 +01001840/*
1841 * Wait until avail_min data becomes available
1842 * Returns a negative error code if any error occurs during operation.
1843 * The available space is stored on availp. When err = 0 and avail = 0
1844 * on the capture stream, it indicates the stream is in DRAINING state.
1845 */
David Dillow5daeba32010-06-27 00:13:20 +02001846static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001847 snd_pcm_uframes_t *availp)
1848{
1849 struct snd_pcm_runtime *runtime = substream->runtime;
1850 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1851 wait_queue_t wait;
1852 int err = 0;
1853 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001854 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001855
Arjan van de Ven763437a2011-09-15 08:49:25 +02001856 init_waitqueue_entry(&wait, current);
1857 set_current_state(TASK_INTERRUPTIBLE);
1858 add_wait_queue(&runtime->tsleep, &wait);
1859
Takashi Iwaif2b36142011-05-26 08:09:38 +02001860 if (runtime->no_period_wakeup)
1861 wait_time = MAX_SCHEDULE_TIMEOUT;
1862 else {
1863 wait_time = 10;
1864 if (runtime->rate) {
1865 long t = runtime->period_size * 2 / runtime->rate;
1866 wait_time = max(t, wait_time);
1867 }
1868 wait_time = msecs_to_jiffies(wait_time * 1000);
1869 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001870
Takashi Iwai13075512008-01-08 18:08:14 +01001871 for (;;) {
1872 if (signal_pending(current)) {
1873 err = -ERESTARTSYS;
1874 break;
1875 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001876
1877 /*
1878 * We need to check if space became available already
1879 * (and thus the wakeup happened already) first to close
1880 * the race of space already having become available.
1881 * This check must happen after been added to the waitqueue
1882 * and having current state be INTERRUPTIBLE.
1883 */
1884 if (is_playback)
1885 avail = snd_pcm_playback_avail(runtime);
1886 else
1887 avail = snd_pcm_capture_avail(runtime);
1888 if (avail >= runtime->twake)
1889 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001890 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001891
1892 tout = schedule_timeout(wait_time);
1893
Takashi Iwai13075512008-01-08 18:08:14 +01001894 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001895 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001896 switch (runtime->status->state) {
1897 case SNDRV_PCM_STATE_SUSPENDED:
1898 err = -ESTRPIPE;
1899 goto _endloop;
1900 case SNDRV_PCM_STATE_XRUN:
1901 err = -EPIPE;
1902 goto _endloop;
1903 case SNDRV_PCM_STATE_DRAINING:
1904 if (is_playback)
1905 err = -EPIPE;
1906 else
1907 avail = 0; /* indicate draining */
1908 goto _endloop;
1909 case SNDRV_PCM_STATE_OPEN:
1910 case SNDRV_PCM_STATE_SETUP:
1911 case SNDRV_PCM_STATE_DISCONNECTED:
1912 err = -EBADFD;
1913 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001914 case SNDRV_PCM_STATE_PAUSED:
1915 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001916 }
1917 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001918 pcm_dbg(substream->pcm,
1919 "%s write error (DMA or IRQ trouble?)\n",
1920 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001921 err = -EIO;
1922 break;
1923 }
Takashi Iwai13075512008-01-08 18:08:14 +01001924 }
1925 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001926 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001927 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001928 *availp = avail;
1929 return err;
1930}
1931
Takashi Iwai9f600632017-05-24 18:15:26 +02001932typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
1933 int channel, unsigned long hwoff,
1934 void *buf, unsigned long bytes);
Takashi Iwaibdc4acf2017-05-24 17:59:17 +02001935
Takashi Iwai9f600632017-05-24 18:15:26 +02001936typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1937 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
1938
1939/* calculate the target DMA-buffer position to be written/read */
1940static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
1941 int channel, unsigned long hwoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Takashi Iwai9f600632017-05-24 18:15:26 +02001943 return runtime->dma_area + hwoff +
1944 channel * (runtime->dma_bytes / runtime->channels);
1945}
1946
Takashi Iwai5c7264c2017-05-24 22:36:23 +02001947/* default copy_user ops for write; used for both interleaved and non- modes */
1948static int default_write_copy(struct snd_pcm_substream *substream,
1949 int channel, unsigned long hwoff,
1950 void *buf, unsigned long bytes)
Takashi Iwai9f600632017-05-24 18:15:26 +02001951{
1952 if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
Takashi Iwai5c7264c2017-05-24 22:36:23 +02001953 (void __user *)buf, bytes))
Takashi Iwai9f600632017-05-24 18:15:26 +02001954 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
Takashi Iwai9f600632017-05-24 18:15:26 +02001957
Takashi Iwai68541212017-05-24 18:23:20 +02001958/* default copy_kernel ops for write */
1959static int default_write_copy_kernel(struct snd_pcm_substream *substream,
1960 int channel, unsigned long hwoff,
1961 void *buf, unsigned long bytes)
1962{
1963 memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
1964 return 0;
1965}
1966
Takashi Iwai9f600632017-05-24 18:15:26 +02001967/* fill silence instead of copy data; called as a transfer helper
1968 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
1969 * a NULL buffer is passed
1970 */
1971static int fill_silence(struct snd_pcm_substream *substream, int channel,
1972 unsigned long hwoff, void *buf, unsigned long bytes)
Takashi Iwaibdc4acf2017-05-24 17:59:17 +02001973{
1974 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibdc4acf2017-05-24 17:59:17 +02001975
Takashi Iwai9f600632017-05-24 18:15:26 +02001976 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1977 return 0;
1978 if (substream->ops->fill_silence)
1979 return substream->ops->fill_silence(substream, channel,
1980 hwoff, bytes);
1981
1982 snd_pcm_format_set_silence(runtime->format,
1983 get_dma_ptr(runtime, channel, hwoff),
1984 bytes_to_samples(runtime, bytes));
1985 return 0;
1986}
1987
Takashi Iwai5c7264c2017-05-24 22:36:23 +02001988/* default copy_user ops for read; used for both interleaved and non- modes */
1989static int default_read_copy(struct snd_pcm_substream *substream,
1990 int channel, unsigned long hwoff,
1991 void *buf, unsigned long bytes)
1992{
1993 if (copy_to_user((void __user *)buf,
1994 get_dma_ptr(substream->runtime, channel, hwoff),
1995 bytes))
1996 return -EFAULT;
1997 return 0;
1998}
1999
Takashi Iwai68541212017-05-24 18:23:20 +02002000/* default copy_kernel ops for read */
2001static int default_read_copy_kernel(struct snd_pcm_substream *substream,
2002 int channel, unsigned long hwoff,
2003 void *buf, unsigned long bytes)
2004{
2005 memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
2006 return 0;
2007}
2008
Takashi Iwai9f600632017-05-24 18:15:26 +02002009/* call transfer function with the converted pointers and sizes;
2010 * for interleaved mode, it's one shot for all samples
2011 */
2012static int interleaved_copy(struct snd_pcm_substream *substream,
2013 snd_pcm_uframes_t hwoff, void *data,
2014 snd_pcm_uframes_t off,
2015 snd_pcm_uframes_t frames,
2016 pcm_transfer_f transfer)
2017{
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019
2020 /* convert to bytes */
2021 hwoff = frames_to_bytes(runtime, hwoff);
2022 off = frames_to_bytes(runtime, off);
2023 frames = frames_to_bytes(runtime, frames);
2024 return transfer(substream, 0, hwoff, data + off, frames);
2025}
2026
2027/* call transfer function with the converted pointers and sizes for each
2028 * non-interleaved channel; when buffer is NULL, silencing instead of copying
2029 */
2030static int noninterleaved_copy(struct snd_pcm_substream *substream,
2031 snd_pcm_uframes_t hwoff, void *data,
2032 snd_pcm_uframes_t off,
2033 snd_pcm_uframes_t frames,
2034 pcm_transfer_f transfer)
2035{
2036 struct snd_pcm_runtime *runtime = substream->runtime;
2037 int channels = runtime->channels;
2038 void **bufs = data;
2039 int c, err;
2040
2041 /* convert to bytes; note that it's not frames_to_bytes() here.
2042 * in non-interleaved mode, we copy for each channel, thus
2043 * each copy is n_samples bytes x channels = whole frames.
2044 */
2045 off = samples_to_bytes(runtime, off);
2046 frames = samples_to_bytes(runtime, frames);
2047 hwoff = samples_to_bytes(runtime, hwoff);
2048 for (c = 0; c < channels; ++c, ++bufs) {
2049 if (!data || !*bufs)
2050 err = fill_silence(substream, c, hwoff, NULL, frames);
2051 else
2052 err = transfer(substream, c, hwoff, *bufs + off,
2053 frames);
2054 if (err < 0)
2055 return err;
Takashi Iwaibdc4acf2017-05-24 17:59:17 +02002056 }
2057 return 0;
2058}
2059
Takashi Iwaia9cd29e2017-05-24 18:18:15 +02002060/* fill silence on the given buffer position;
2061 * called from snd_pcm_playback_silence()
2062 */
2063static int fill_silence_frames(struct snd_pcm_substream *substream,
2064 snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2065{
2066 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2067 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2068 return interleaved_copy(substream, off, NULL, 0, frames,
2069 fill_silence);
2070 else
2071 return noninterleaved_copy(substream, off, NULL, 0, frames,
2072 fill_silence);
2073}
2074
Takashi Iwaibdc4acf2017-05-24 17:59:17 +02002075/* sanity-check for read/write methods */
2076static int pcm_sanity_check(struct snd_pcm_substream *substream)
2077{
2078 struct snd_pcm_runtime *runtime;
2079 if (PCM_RUNTIME_CHECK(substream))
2080 return -ENXIO;
2081 runtime = substream->runtime;
2082 if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
2083 return -EINVAL;
2084 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2085 return -EBADFD;
2086 return 0;
2087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwai6ba63922017-05-24 17:51:30 +02002089static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
2090{
2091 switch (runtime->status->state) {
2092 case SNDRV_PCM_STATE_PREPARED:
2093 case SNDRV_PCM_STATE_RUNNING:
2094 case SNDRV_PCM_STATE_PAUSED:
2095 return 0;
2096 case SNDRV_PCM_STATE_XRUN:
2097 return -EPIPE;
2098 case SNDRV_PCM_STATE_SUSPENDED:
2099 return -ESTRPIPE;
2100 default:
2101 return -EBADFD;
2102 }
2103}
2104
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002105/* update to the given appl_ptr and call ack callback if needed;
2106 * when an error is returned, take back to the original value
2107 */
2108int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
2109 snd_pcm_uframes_t appl_ptr)
2110{
2111 struct snd_pcm_runtime *runtime = substream->runtime;
2112 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2113 int ret;
2114
2115 runtime->control->appl_ptr = appl_ptr;
2116 if (substream->ops->ack) {
2117 ret = substream->ops->ack(substream);
2118 if (ret < 0) {
2119 runtime->control->appl_ptr = old_appl_ptr;
2120 return ret;
2121 }
2122 }
Takashi Sakamotofccf5382017-06-12 09:41:45 +09002123
2124 trace_applptr(substream, old_appl_ptr, appl_ptr);
2125
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002126 return 0;
2127}
2128
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002129/* the common loop for read/write data */
2130snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2131 void *data, bool interleaved,
Takashi Iwai68541212017-05-24 18:23:20 +02002132 snd_pcm_uframes_t size, bool in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Takashi Iwai877211f2005-11-17 13:59:38 +01002134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 snd_pcm_uframes_t xfer = 0;
2136 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002137 snd_pcm_uframes_t avail;
Takashi Iwai9f600632017-05-24 18:15:26 +02002138 pcm_copy_f writer;
2139 pcm_transfer_f transfer;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002140 bool nonblock;
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002141 bool is_playback;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002142 int err;
2143
2144 err = pcm_sanity_check(substream);
2145 if (err < 0)
2146 return err;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002147
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002148 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002149 if (interleaved) {
2150 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2151 runtime->channels > 1)
2152 return -EINVAL;
Takashi Iwai9f600632017-05-24 18:15:26 +02002153 writer = interleaved_copy;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002154 } else {
2155 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2156 return -EINVAL;
Takashi Iwai9f600632017-05-24 18:15:26 +02002157 writer = noninterleaved_copy;
2158 }
2159
2160 if (!data) {
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002161 if (is_playback)
2162 transfer = fill_silence;
2163 else
2164 return -EINVAL;
Takashi Iwai68541212017-05-24 18:23:20 +02002165 } else if (in_kernel) {
2166 if (substream->ops->copy_kernel)
2167 transfer = substream->ops->copy_kernel;
2168 else
2169 transfer = is_playback ?
2170 default_write_copy_kernel : default_read_copy_kernel;
Takashi Iwai9f600632017-05-24 18:15:26 +02002171 } else {
2172 if (substream->ops->copy_user)
2173 transfer = (pcm_transfer_f)substream->ops->copy_user;
2174 else
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002175 transfer = is_playback ?
2176 default_write_copy : default_read_copy;
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 if (size == 0)
2180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Takashi Iwaic48f12ee2017-05-21 09:35:21 +02002182 nonblock = !!(substream->f_flags & O_NONBLOCK);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 snd_pcm_stream_lock_irq(substream);
Takashi Iwai6ba63922017-05-24 17:51:30 +02002185 err = pcm_accessible_state(runtime);
2186 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002189 if (!is_playback &&
2190 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2191 size >= runtime->start_threshold) {
2192 err = snd_pcm_start(substream);
2193 if (err < 0)
2194 goto _end_unlock;
2195 }
2196
David Dillow5daeba32010-06-27 00:13:20 +02002197 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002198 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2199 snd_pcm_update_hw_ptr(substream);
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002200 if (is_playback)
2201 avail = snd_pcm_playback_avail(runtime);
2202 else
2203 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 while (size > 0) {
2205 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002207 if (!avail) {
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002208 if (!is_playback &&
2209 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2210 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2211 goto _end_unlock;
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (nonblock) {
2214 err = -EAGAIN;
2215 goto _end_unlock;
2216 }
David Dillow5daeba32010-06-27 00:13:20 +02002217 runtime->twake = min_t(snd_pcm_uframes_t, size,
2218 runtime->control->avail_min ? : 1);
2219 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002220 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto _end_unlock;
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002222 if (!avail)
2223 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 frames = size > avail ? avail : size;
2226 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2227 if (frames > cont)
2228 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002229 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002230 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002231 snd_pcm_stream_unlock_irq(substream);
2232 return -EINVAL;
2233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 appl_ptr = runtime->control->appl_ptr;
2235 appl_ofs = appl_ptr % runtime->buffer_size;
2236 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9f600632017-05-24 18:15:26 +02002237 err = writer(substream, appl_ofs, data, offset, frames,
2238 transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002240 if (err < 0)
2241 goto _end_unlock;
Takashi Iwai6ba63922017-05-24 17:51:30 +02002242 err = pcm_accessible_state(runtime);
2243 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 appl_ptr += frames;
2246 if (appl_ptr >= runtime->boundary)
2247 appl_ptr -= runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002248 err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2249 if (err < 0)
2250 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 offset += frames;
2253 size -= frames;
2254 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002255 avail -= frames;
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002256 if (is_playback &&
2257 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2259 err = snd_pcm_start(substream);
2260 if (err < 0)
2261 goto _end_unlock;
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002265 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002266 if (xfer > 0 && err >= 0)
2267 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2270}
Takashi Iwai5c7264c2017-05-24 22:36:23 +02002271EXPORT_SYMBOL(__snd_pcm_lib_xfer);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002272
2273/*
2274 * standard channel mapping helpers
2275 */
2276
2277/* default channel maps for multi-channel playbacks, up to 8 channels */
2278const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2279 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002280 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002281 { .channels = 2,
2282 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2283 { .channels = 4,
2284 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2285 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2286 { .channels = 6,
2287 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2288 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2289 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2290 { .channels = 8,
2291 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2292 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2293 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2294 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2295 { }
2296};
2297EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2298
2299/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2300const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2301 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002302 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002303 { .channels = 2,
2304 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2305 { .channels = 4,
2306 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2307 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2308 { .channels = 6,
2309 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2310 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2311 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2312 { .channels = 8,
2313 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2314 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2315 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2316 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2317 { }
2318};
2319EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2320
2321static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2322{
2323 if (ch > info->max_channels)
2324 return false;
2325 return !info->channel_mask || (info->channel_mask & (1U << ch));
2326}
2327
2328static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_info *uinfo)
2330{
2331 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2332
2333 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2334 uinfo->count = 0;
2335 uinfo->count = info->max_channels;
2336 uinfo->value.integer.min = 0;
2337 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2338 return 0;
2339}
2340
2341/* get callback for channel map ctl element
2342 * stores the channel position firstly matching with the current channels
2343 */
2344static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
2346{
2347 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2348 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2349 struct snd_pcm_substream *substream;
2350 const struct snd_pcm_chmap_elem *map;
2351
2352 if (snd_BUG_ON(!info->chmap))
2353 return -EINVAL;
2354 substream = snd_pcm_chmap_substream(info, idx);
2355 if (!substream)
2356 return -ENODEV;
2357 memset(ucontrol->value.integer.value, 0,
2358 sizeof(ucontrol->value.integer.value));
2359 if (!substream->runtime)
2360 return 0; /* no channels set */
2361 for (map = info->chmap; map->channels; map++) {
2362 int i;
2363 if (map->channels == substream->runtime->channels &&
2364 valid_chmap_channels(info, map->channels)) {
2365 for (i = 0; i < map->channels; i++)
2366 ucontrol->value.integer.value[i] = map->map[i];
2367 return 0;
2368 }
2369 }
2370 return -EINVAL;
2371}
2372
2373/* tlv callback for channel map ctl element
2374 * expands the pre-defined channel maps in a form of TLV
2375 */
2376static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2377 unsigned int size, unsigned int __user *tlv)
2378{
2379 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2380 const struct snd_pcm_chmap_elem *map;
2381 unsigned int __user *dst;
2382 int c, count = 0;
2383
2384 if (snd_BUG_ON(!info->chmap))
2385 return -EINVAL;
2386 if (size < 8)
2387 return -ENOMEM;
2388 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2389 return -EFAULT;
2390 size -= 8;
2391 dst = tlv + 2;
2392 for (map = info->chmap; map->channels; map++) {
2393 int chs_bytes = map->channels * 4;
2394 if (!valid_chmap_channels(info, map->channels))
2395 continue;
2396 if (size < 8)
2397 return -ENOMEM;
2398 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2399 put_user(chs_bytes, dst + 1))
2400 return -EFAULT;
2401 dst += 2;
2402 size -= 8;
2403 count += 8;
2404 if (size < chs_bytes)
2405 return -ENOMEM;
2406 size -= chs_bytes;
2407 count += chs_bytes;
2408 for (c = 0; c < map->channels; c++) {
2409 if (put_user(map->map[c], dst))
2410 return -EFAULT;
2411 dst++;
2412 }
2413 }
2414 if (put_user(count, tlv + 1))
2415 return -EFAULT;
2416 return 0;
2417}
2418
2419static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2420{
2421 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2422 info->pcm->streams[info->stream].chmap_kctl = NULL;
2423 kfree(info);
2424}
2425
2426/**
2427 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2428 * @pcm: the assigned PCM instance
2429 * @stream: stream direction
2430 * @chmap: channel map elements (for query)
2431 * @max_channels: the max number of channels for the stream
2432 * @private_value: the value passed to each kcontrol's private_value field
2433 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2434 *
2435 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002436 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002437 */
2438int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2439 const struct snd_pcm_chmap_elem *chmap,
2440 int max_channels,
2441 unsigned long private_value,
2442 struct snd_pcm_chmap **info_ret)
2443{
2444 struct snd_pcm_chmap *info;
2445 struct snd_kcontrol_new knew = {
2446 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2447 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002448 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2449 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2450 .info = pcm_chmap_ctl_info,
2451 .get = pcm_chmap_ctl_get,
2452 .tlv.c = pcm_chmap_ctl_tlv,
2453 };
2454 int err;
2455
Takashi Iwai8d879be2016-05-10 16:07:40 +02002456 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2457 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002458 info = kzalloc(sizeof(*info), GFP_KERNEL);
2459 if (!info)
2460 return -ENOMEM;
2461 info->pcm = pcm;
2462 info->stream = stream;
2463 info->chmap = chmap;
2464 info->max_channels = max_channels;
2465 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2466 knew.name = "Playback Channel Map";
2467 else
2468 knew.name = "Capture Channel Map";
2469 knew.device = pcm->device;
2470 knew.count = pcm->streams[stream].substream_count;
2471 knew.private_value = private_value;
2472 info->kctl = snd_ctl_new1(&knew, info);
2473 if (!info->kctl) {
2474 kfree(info);
2475 return -ENOMEM;
2476 }
2477 info->kctl->private_free = pcm_chmap_ctl_private_free;
2478 err = snd_ctl_add(pcm->card, info->kctl);
2479 if (err < 0)
2480 return err;
2481 pcm->streams[stream].chmap_kctl = info->kctl;
2482 if (info_ret)
2483 *info_ret = info;
2484 return 0;
2485}
2486EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);