xref: /openbmc/linux/sound/core/pcm_lib.c (revision e14ebde5)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Digital Audio (PCM) abstract layer
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  *                   Abramo Bagnara <abramo@alsa-project.org>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/slab.h>
9174cd4b1SIngo Molnar #include <linux/sched/signal.h>
101da177e4SLinus Torvalds #include <linux/time.h>
113f7440a6STakashi Iwai #include <linux/math64.h>
12d81a6d71SPaul Gortmaker #include <linux/export.h>
131da177e4SLinus Torvalds #include <sound/core.h>
141da177e4SLinus Torvalds #include <sound/control.h>
152d3391ecSTakashi Iwai #include <sound/tlv.h>
161da177e4SLinus Torvalds #include <sound/info.h>
171da177e4SLinus Torvalds #include <sound/pcm.h>
181da177e4SLinus Torvalds #include <sound/pcm_params.h>
191da177e4SLinus Torvalds #include <sound/timer.h>
201da177e4SLinus Torvalds 
212c4842d3STakashi Sakamoto #include "pcm_local.h"
222c4842d3STakashi Sakamoto 
23f5914908STakashi Iwai #ifdef CONFIG_SND_PCM_XRUN_DEBUG
24f5914908STakashi Iwai #define CREATE_TRACE_POINTS
25f5914908STakashi Iwai #include "pcm_trace.h"
26f5914908STakashi Iwai #else
27f5914908STakashi Iwai #define trace_hwptr(substream, pos, in_interrupt)
28f5914908STakashi Iwai #define trace_xrun(substream)
29f5914908STakashi Iwai #define trace_hw_ptr_error(substream, reason)
30fccf5388STakashi Sakamoto #define trace_applptr(substream, prev, curr)
31f5914908STakashi Iwai #endif
32f5914908STakashi Iwai 
33a9cd29e7STakashi Iwai static int fill_silence_frames(struct snd_pcm_substream *substream,
34a9cd29e7STakashi Iwai 			       snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
35a9cd29e7STakashi Iwai 
366d8d56dbSJaroslav Kysela 
update_silence_vars(struct snd_pcm_runtime * runtime,snd_pcm_uframes_t ptr,snd_pcm_uframes_t new_ptr)376d8d56dbSJaroslav Kysela static inline void update_silence_vars(struct snd_pcm_runtime *runtime,
386d8d56dbSJaroslav Kysela 				       snd_pcm_uframes_t ptr,
396d8d56dbSJaroslav Kysela 				       snd_pcm_uframes_t new_ptr)
406d8d56dbSJaroslav Kysela {
416d8d56dbSJaroslav Kysela 	snd_pcm_sframes_t delta;
426d8d56dbSJaroslav Kysela 
436d8d56dbSJaroslav Kysela 	delta = new_ptr - ptr;
446d8d56dbSJaroslav Kysela 	if (delta == 0)
456d8d56dbSJaroslav Kysela 		return;
466d8d56dbSJaroslav Kysela 	if (delta < 0)
476d8d56dbSJaroslav Kysela 		delta += runtime->boundary;
486d8d56dbSJaroslav Kysela 	if ((snd_pcm_uframes_t)delta < runtime->silence_filled)
496d8d56dbSJaroslav Kysela 		runtime->silence_filled -= delta;
506d8d56dbSJaroslav Kysela 	else
516d8d56dbSJaroslav Kysela 		runtime->silence_filled = 0;
526d8d56dbSJaroslav Kysela 	runtime->silence_start = new_ptr;
536d8d56dbSJaroslav Kysela }
546d8d56dbSJaroslav Kysela 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * fill ring buffer with silence
571da177e4SLinus Torvalds  * runtime->silence_start: starting pointer to silence area
581da177e4SLinus Torvalds  * runtime->silence_filled: size filled with silence
591da177e4SLinus Torvalds  * runtime->silence_threshold: threshold from application
601da177e4SLinus Torvalds  * runtime->silence_size: maximal size from application
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
631da177e4SLinus Torvalds  */
snd_pcm_playback_silence(struct snd_pcm_substream * substream,snd_pcm_uframes_t new_hw_ptr)64d7f5dd97SJaroslav Kysela void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
651da177e4SLinus Torvalds {
66877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
67d7f5dd97SJaroslav Kysela 	snd_pcm_uframes_t frames, ofs, transfer;
6829d1a873STakashi Iwai 	int err;
691da177e4SLinus Torvalds 
70d7f5dd97SJaroslav Kysela 	if (runtime->silence_size < runtime->boundary) {
716d8d56dbSJaroslav Kysela 		snd_pcm_sframes_t noise_dist;
72d7f5dd97SJaroslav Kysela 		snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
736d8d56dbSJaroslav Kysela 		update_silence_vars(runtime, runtime->silence_start, appl_ptr);
742fbaa44aSJaroslav Kysela 		/* initialization outside pointer updates */
752fbaa44aSJaroslav Kysela 		if (new_hw_ptr == ULONG_MAX)
762fbaa44aSJaroslav Kysela 			new_hw_ptr = runtime->status->hw_ptr;
772fbaa44aSJaroslav Kysela 		/* get hw_avail with the boundary crossing */
782fbaa44aSJaroslav Kysela 		noise_dist = appl_ptr - new_hw_ptr;
792fbaa44aSJaroslav Kysela 		if (noise_dist < 0)
802fbaa44aSJaroslav Kysela 			noise_dist += runtime->boundary;
812fbaa44aSJaroslav Kysela 		/* total noise distance */
822fbaa44aSJaroslav Kysela 		noise_dist += runtime->silence_filled;
83d7f5dd97SJaroslav Kysela 		if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
84d7f5dd97SJaroslav Kysela 			return;
85d7f5dd97SJaroslav Kysela 		frames = runtime->silence_threshold - noise_dist;
861da177e4SLinus Torvalds 		if (frames > runtime->silence_size)
871da177e4SLinus Torvalds 			frames = runtime->silence_size;
881da177e4SLinus Torvalds 	} else {
89d7f5dd97SJaroslav Kysela 		/*
90d7f5dd97SJaroslav Kysela 		 * This filling mode aims at free-running mode (used for example by dmix),
91d7f5dd97SJaroslav Kysela 		 * which doesn't update the application pointer.
92d7f5dd97SJaroslav Kysela 		 */
936ffa6f39SOswald Buddenhagen 		snd_pcm_uframes_t hw_ptr = runtime->status->hw_ptr;
946ffa6f39SOswald Buddenhagen 		if (new_hw_ptr == ULONG_MAX) {
956ffa6f39SOswald Buddenhagen 			/*
966ffa6f39SOswald Buddenhagen 			 * Initialization, fill the whole unused buffer with silence.
976ffa6f39SOswald Buddenhagen 			 *
986ffa6f39SOswald Buddenhagen 			 * Usually, this is entered while stopped, before data is queued,
996ffa6f39SOswald Buddenhagen 			 * so both pointers are expected to be zero.
1006ffa6f39SOswald Buddenhagen 			 */
1016ffa6f39SOswald Buddenhagen 			snd_pcm_sframes_t avail = runtime->control->appl_ptr - hw_ptr;
1026ffa6f39SOswald Buddenhagen 			if (avail < 0)
1036ffa6f39SOswald Buddenhagen 				avail += runtime->boundary;
1046ffa6f39SOswald Buddenhagen 			/*
1056ffa6f39SOswald Buddenhagen 			 * In free-running mode, appl_ptr will be zero even while running,
1066ffa6f39SOswald Buddenhagen 			 * so we end up with a huge number. There is no useful way to
1076ffa6f39SOswald Buddenhagen 			 * handle this, so we just clear the whole buffer.
1086ffa6f39SOswald Buddenhagen 			 */
1096ffa6f39SOswald Buddenhagen 			runtime->silence_filled = avail > runtime->buffer_size ? 0 : avail;
1106ffa6f39SOswald Buddenhagen 			runtime->silence_start = hw_ptr;
111d7f5dd97SJaroslav Kysela 		} else {
1126ffa6f39SOswald Buddenhagen 			/* Silence the just played area immediately */
1136ffa6f39SOswald Buddenhagen 			update_silence_vars(runtime, hw_ptr, new_hw_ptr);
114d7f5dd97SJaroslav Kysela 		}
1156ffa6f39SOswald Buddenhagen 		/*
1166ffa6f39SOswald Buddenhagen 		 * In this mode, silence_filled actually includes the valid
1176ffa6f39SOswald Buddenhagen 		 * sample data from the user.
1186ffa6f39SOswald Buddenhagen 		 */
119d7f5dd97SJaroslav Kysela 		frames = runtime->buffer_size - runtime->silence_filled;
120d7f5dd97SJaroslav Kysela 	}
1217eaa943cSTakashi Iwai 	if (snd_BUG_ON(frames > runtime->buffer_size))
1227eaa943cSTakashi Iwai 		return;
123d7f5dd97SJaroslav Kysela 	if (frames == 0)
124d7f5dd97SJaroslav Kysela 		return;
125781b4da6SJaroslav Kysela 	ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
126ee2dd703SOswald Buddenhagen 	do {
1271da177e4SLinus Torvalds 		transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
128a9cd29e7STakashi Iwai 		err = fill_silence_frames(substream, ofs, transfer);
1297eaa943cSTakashi Iwai 		snd_BUG_ON(err < 0);
1301da177e4SLinus Torvalds 		runtime->silence_filled += transfer;
1311da177e4SLinus Torvalds 		frames -= transfer;
1321da177e4SLinus Torvalds 		ofs = 0;
133ee2dd703SOswald Buddenhagen 	} while (frames > 0);
134a25684a9STakashi Iwai 	snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
137acb03d44SEliot Blennerhassett #ifdef CONFIG_SND_DEBUG
snd_pcm_debug_name(struct snd_pcm_substream * substream,char * name,size_t len)138acb03d44SEliot Blennerhassett void snd_pcm_debug_name(struct snd_pcm_substream *substream,
139c0070110STakashi Iwai 			   char *name, size_t len)
1401da177e4SLinus Torvalds {
141c0070110STakashi Iwai 	snprintf(name, len, "pcmC%dD%d%c:%d",
1421da177e4SLinus Torvalds 		 substream->pcm->card->number,
1431da177e4SLinus Torvalds 		 substream->pcm->device,
144c0070110STakashi Iwai 		 substream->stream ? 'c' : 'p',
145c0070110STakashi Iwai 		 substream->number);
146c0070110STakashi Iwai }
147acb03d44SEliot Blennerhassett EXPORT_SYMBOL(snd_pcm_debug_name);
148acb03d44SEliot Blennerhassett #endif
149c0070110STakashi Iwai 
1504d96eb25SJaroslav Kysela #define XRUN_DEBUG_BASIC	(1<<0)
1514d96eb25SJaroslav Kysela #define XRUN_DEBUG_STACK	(1<<1)	/* dump also stack */
1524d96eb25SJaroslav Kysela #define XRUN_DEBUG_JIFFIESCHECK	(1<<2)	/* do jiffies check */
1534d96eb25SJaroslav Kysela 
1544d96eb25SJaroslav Kysela #ifdef CONFIG_SND_PCM_XRUN_DEBUG
1554d96eb25SJaroslav Kysela 
1564d96eb25SJaroslav Kysela #define xrun_debug(substream, mask) \
1574d96eb25SJaroslav Kysela 			((substream)->pstr->xrun_debug & (mask))
1580f17014bSJarkko Nikula #else
1590f17014bSJarkko Nikula #define xrun_debug(substream, mask)	0
1600f17014bSJarkko Nikula #endif
1614d96eb25SJaroslav Kysela 
1624d96eb25SJaroslav Kysela #define dump_stack_on_xrun(substream) do {			\
1634d96eb25SJaroslav Kysela 		if (xrun_debug(substream, XRUN_DEBUG_STACK))	\
1644d96eb25SJaroslav Kysela 			dump_stack();				\
1654d96eb25SJaroslav Kysela 	} while (0)
1664d96eb25SJaroslav Kysela 
1679cd641edSTakashi Iwai /* call with stream lock held */
__snd_pcm_xrun(struct snd_pcm_substream * substream)1689cd641edSTakashi Iwai void __snd_pcm_xrun(struct snd_pcm_substream *substream)
1691da177e4SLinus Torvalds {
17013f040f9SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
17113f040f9SJaroslav Kysela 
172f5914908STakashi Iwai 	trace_xrun(substream);
173fcae40c9SBaolin Wang 	if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
174fcae40c9SBaolin Wang 		struct timespec64 tstamp;
175fcae40c9SBaolin Wang 
176fcae40c9SBaolin Wang 		snd_pcm_gettime(runtime, &tstamp);
17780fe7430SArnd Bergmann 		runtime->status->tstamp.tv_sec = tstamp.tv_sec;
17880fe7430SArnd Bergmann 		runtime->status->tstamp.tv_nsec = tstamp.tv_nsec;
179fcae40c9SBaolin Wang 	}
1801da177e4SLinus Torvalds 	snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
181741b20cfSJaroslav Kysela 	if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
182c0070110STakashi Iwai 		char name[16];
183acb03d44SEliot Blennerhassett 		snd_pcm_debug_name(substream, name, sizeof(name));
18409e56df8STakashi Iwai 		pcm_warn(substream->pcm, "XRUN: %s\n", name);
185ed3da3d9STakashi Iwai 		dump_stack_on_xrun(substream);
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1890f17014bSJarkko Nikula #ifdef CONFIG_SND_PCM_XRUN_DEBUG
190f5914908STakashi Iwai #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...)	\
1914d96eb25SJaroslav Kysela 	do {								\
192f5914908STakashi Iwai 		trace_hw_ptr_error(substream, reason);	\
1934d96eb25SJaroslav Kysela 		if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {		\
194f5914908STakashi Iwai 			pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
195f5914908STakashi Iwai 					   (in_interrupt) ? 'Q' : 'P', ##args);	\
1964d96eb25SJaroslav Kysela 			dump_stack_on_xrun(substream);			\
1974d96eb25SJaroslav Kysela 		}							\
1984d96eb25SJaroslav Kysela 	} while (0)
1994d96eb25SJaroslav Kysela 
2004d96eb25SJaroslav Kysela #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
2014d96eb25SJaroslav Kysela 
2024d96eb25SJaroslav Kysela #define hw_ptr_error(substream, fmt, args...) do { } while (0)
2034d96eb25SJaroslav Kysela 
2044d96eb25SJaroslav Kysela #endif
2054d96eb25SJaroslav Kysela 
snd_pcm_update_state(struct snd_pcm_substream * substream,struct snd_pcm_runtime * runtime)2061250932eSJaroslav Kysela int snd_pcm_update_state(struct snd_pcm_substream *substream,
207877211f5STakashi Iwai 			 struct snd_pcm_runtime *runtime)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	snd_pcm_uframes_t avail;
2101da177e4SLinus Torvalds 
211763e5067STakashi Iwai 	avail = snd_pcm_avail(substream);
2121da177e4SLinus Torvalds 	if (avail > runtime->avail_max)
2131da177e4SLinus Torvalds 		runtime->avail_max = avail;
214f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_DRAINING) {
2154cdc115fSTakashi Iwai 		if (avail >= runtime->buffer_size) {
2161da177e4SLinus Torvalds 			snd_pcm_drain_done(substream);
2174cdc115fSTakashi Iwai 			return -EPIPE;
2184cdc115fSTakashi Iwai 		}
2194cdc115fSTakashi Iwai 	} else {
2204cdc115fSTakashi Iwai 		if (avail >= runtime->stop_threshold) {
2219cd641edSTakashi Iwai 			__snd_pcm_xrun(substream);
2221da177e4SLinus Torvalds 			return -EPIPE;
2231da177e4SLinus Torvalds 		}
2244cdc115fSTakashi Iwai 	}
2255daeba34SDavid Dillow 	if (runtime->twake) {
2265daeba34SDavid Dillow 		if (avail >= runtime->twake)
2275daeba34SDavid Dillow 			wake_up(&runtime->tsleep);
2285daeba34SDavid Dillow 	} else if (avail >= runtime->control->avail_min)
2295daeba34SDavid Dillow 		wake_up(&runtime->sleep);
2301da177e4SLinus Torvalds 	return 0;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
update_audio_tstamp(struct snd_pcm_substream * substream,struct timespec64 * curr_tstamp,struct timespec64 * audio_tstamp)2333179f620SPierre-Louis Bossart static void update_audio_tstamp(struct snd_pcm_substream *substream,
234fcae40c9SBaolin Wang 				struct timespec64 *curr_tstamp,
235fcae40c9SBaolin Wang 				struct timespec64 *audio_tstamp)
2363179f620SPierre-Louis Bossart {
2373179f620SPierre-Louis Bossart 	struct snd_pcm_runtime *runtime = substream->runtime;
2383179f620SPierre-Louis Bossart 	u64 audio_frames, audio_nsecs;
239fcae40c9SBaolin Wang 	struct timespec64 driver_tstamp;
2403179f620SPierre-Louis Bossart 
2413179f620SPierre-Louis Bossart 	if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
2423179f620SPierre-Louis Bossart 		return;
2433179f620SPierre-Louis Bossart 
2443179f620SPierre-Louis Bossart 	if (!(substream->ops->get_time_info) ||
2453179f620SPierre-Louis Bossart 		(runtime->audio_tstamp_report.actual_type ==
2463179f620SPierre-Louis Bossart 			SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
2473179f620SPierre-Louis Bossart 
2483179f620SPierre-Louis Bossart 		/*
2493179f620SPierre-Louis Bossart 		 * provide audio timestamp derived from pointer position
2503179f620SPierre-Louis Bossart 		 * add delay only if requested
2513179f620SPierre-Louis Bossart 		 */
2523179f620SPierre-Louis Bossart 
2533179f620SPierre-Louis Bossart 		audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
2543179f620SPierre-Louis Bossart 
2553179f620SPierre-Louis Bossart 		if (runtime->audio_tstamp_config.report_delay) {
2563179f620SPierre-Louis Bossart 			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2573179f620SPierre-Louis Bossart 				audio_frames -=  runtime->delay;
2583179f620SPierre-Louis Bossart 			else
2593179f620SPierre-Louis Bossart 				audio_frames +=  runtime->delay;
2603179f620SPierre-Louis Bossart 		}
2613179f620SPierre-Louis Bossart 		audio_nsecs = div_u64(audio_frames * 1000000000LL,
2623179f620SPierre-Louis Bossart 				runtime->rate);
263fcae40c9SBaolin Wang 		*audio_tstamp = ns_to_timespec64(audio_nsecs);
2643179f620SPierre-Louis Bossart 	}
265fcae40c9SBaolin Wang 
266fcae40c9SBaolin Wang 	if (runtime->status->audio_tstamp.tv_sec != audio_tstamp->tv_sec ||
267fcae40c9SBaolin Wang 	    runtime->status->audio_tstamp.tv_nsec != audio_tstamp->tv_nsec) {
26880fe7430SArnd Bergmann 		runtime->status->audio_tstamp.tv_sec = audio_tstamp->tv_sec;
26980fe7430SArnd Bergmann 		runtime->status->audio_tstamp.tv_nsec = audio_tstamp->tv_nsec;
27080fe7430SArnd Bergmann 		runtime->status->tstamp.tv_sec = curr_tstamp->tv_sec;
27180fe7430SArnd Bergmann 		runtime->status->tstamp.tv_nsec = curr_tstamp->tv_nsec;
27220e3f985SHenrik Eriksson 	}
2733179f620SPierre-Louis Bossart 
274fcae40c9SBaolin Wang 
2753179f620SPierre-Louis Bossart 	/*
2763179f620SPierre-Louis Bossart 	 * re-take a driver timestamp to let apps detect if the reference tstamp
2773179f620SPierre-Louis Bossart 	 * read by low-level hardware was provided with a delay
2783179f620SPierre-Louis Bossart 	 */
279fcae40c9SBaolin Wang 	snd_pcm_gettime(substream->runtime, &driver_tstamp);
2803179f620SPierre-Louis Bossart 	runtime->driver_tstamp = driver_tstamp;
2813179f620SPierre-Louis Bossart }
2823179f620SPierre-Louis Bossart 
snd_pcm_update_hw_ptr0(struct snd_pcm_substream * substream,unsigned int in_interrupt)283f240406bSJaroslav Kysela static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
284f240406bSJaroslav Kysela 				  unsigned int in_interrupt)
2851da177e4SLinus Torvalds {
286877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
2871da177e4SLinus Torvalds 	snd_pcm_uframes_t pos;
288f240406bSJaroslav Kysela 	snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
289bbf6ad13SJaroslav Kysela 	snd_pcm_sframes_t hdelta, delta;
290bbf6ad13SJaroslav Kysela 	unsigned long jdelta;
2913509a03fSPierre-Louis Bossart 	unsigned long curr_jiffies;
292fcae40c9SBaolin Wang 	struct timespec64 curr_tstamp;
293fcae40c9SBaolin Wang 	struct timespec64 audio_tstamp;
2940e8014d7SPierre-Louis Bossart 	int crossed_boundary = 0;
2951da177e4SLinus Torvalds 
296bbf6ad13SJaroslav Kysela 	old_hw_ptr = runtime->status->hw_ptr;
2973509a03fSPierre-Louis Bossart 
2983509a03fSPierre-Louis Bossart 	/*
2993509a03fSPierre-Louis Bossart 	 * group pointer, time and jiffies reads to allow for more
3003509a03fSPierre-Louis Bossart 	 * accurate correlations/corrections.
3013509a03fSPierre-Louis Bossart 	 * The values are stored at the end of this routine after
3023509a03fSPierre-Louis Bossart 	 * corrections for hw_ptr position
3033509a03fSPierre-Louis Bossart 	 */
304f240406bSJaroslav Kysela 	pos = substream->ops->pointer(substream);
3053509a03fSPierre-Louis Bossart 	curr_jiffies = jiffies;
3064eeaaeaeSPierre-Louis Bossart 	if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
3073179f620SPierre-Louis Bossart 		if ((substream->ops->get_time_info) &&
3083179f620SPierre-Louis Bossart 			(runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
3093179f620SPierre-Louis Bossart 			substream->ops->get_time_info(substream, &curr_tstamp,
3103179f620SPierre-Louis Bossart 						&audio_tstamp,
3113179f620SPierre-Louis Bossart 						&runtime->audio_tstamp_config,
3123179f620SPierre-Louis Bossart 						&runtime->audio_tstamp_report);
3133509a03fSPierre-Louis Bossart 
3143179f620SPierre-Louis Bossart 			/* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
3153179f620SPierre-Louis Bossart 			if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
316fcae40c9SBaolin Wang 				snd_pcm_gettime(runtime, &curr_tstamp);
3173179f620SPierre-Louis Bossart 		} else
318fcae40c9SBaolin Wang 			snd_pcm_gettime(runtime, &curr_tstamp);
3194eeaaeaeSPierre-Louis Bossart 	}
3204eeaaeaeSPierre-Louis Bossart 
3211da177e4SLinus Torvalds 	if (pos == SNDRV_PCM_POS_XRUN) {
3229cd641edSTakashi Iwai 		__snd_pcm_xrun(substream);
3231da177e4SLinus Torvalds 		return -EPIPE;
3241da177e4SLinus Torvalds 	}
325f240406bSJaroslav Kysela 	if (pos >= runtime->buffer_size) {
32609e56df8STakashi Iwai 		if (printk_ratelimit()) {
327cedb8118STakashi Iwai 			char name[16];
328acb03d44SEliot Blennerhassett 			snd_pcm_debug_name(substream, name, sizeof(name));
32909e56df8STakashi Iwai 			pcm_err(substream->pcm,
3300ab1ace8STakashi Iwai 				"invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
331f240406bSJaroslav Kysela 				name, pos, runtime->buffer_size,
332f240406bSJaroslav Kysela 				runtime->period_size);
333cedb8118STakashi Iwai 		}
334f240406bSJaroslav Kysela 		pos = 0;
335f240406bSJaroslav Kysela 	}
336f240406bSJaroslav Kysela 	pos -= pos % runtime->min_align;
337f5914908STakashi Iwai 	trace_hwptr(substream, pos, in_interrupt);
338ed3da3d9STakashi Iwai 	hw_base = runtime->hw_ptr_base;
339ed3da3d9STakashi Iwai 	new_hw_ptr = hw_base + pos;
340f240406bSJaroslav Kysela 	if (in_interrupt) {
341f240406bSJaroslav Kysela 		/* we know that one period was processed */
342f240406bSJaroslav Kysela 		/* delta = "expected next hw_ptr" for in_interrupt != 0 */
343e7636925SJaroslav Kysela 		delta = runtime->hw_ptr_interrupt + runtime->period_size;
344f240406bSJaroslav Kysela 		if (delta > new_hw_ptr) {
345bd76af0fSJaroslav Kysela 			/* check for double acknowledged interrupts */
3463509a03fSPierre-Louis Bossart 			hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
34713a98839SKoro Chen 			if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
348f240406bSJaroslav Kysela 				hw_base += runtime->buffer_size;
3490e8014d7SPierre-Louis Bossart 				if (hw_base >= runtime->boundary) {
350f240406bSJaroslav Kysela 					hw_base = 0;
3510e8014d7SPierre-Louis Bossart 					crossed_boundary++;
3520e8014d7SPierre-Louis Bossart 				}
353f240406bSJaroslav Kysela 				new_hw_ptr = hw_base + pos;
354f240406bSJaroslav Kysela 				goto __delta;
355ded652f7STakashi Iwai 			}
356f240406bSJaroslav Kysela 		}
357bd76af0fSJaroslav Kysela 	}
358f240406bSJaroslav Kysela 	/* new_hw_ptr might be lower than old_hw_ptr in case when */
359f240406bSJaroslav Kysela 	/* pointer crosses the end of the ring buffer */
360f240406bSJaroslav Kysela 	if (new_hw_ptr < old_hw_ptr) {
361ed3da3d9STakashi Iwai 		hw_base += runtime->buffer_size;
3620e8014d7SPierre-Louis Bossart 		if (hw_base >= runtime->boundary) {
363ed3da3d9STakashi Iwai 			hw_base = 0;
3640e8014d7SPierre-Louis Bossart 			crossed_boundary++;
3650e8014d7SPierre-Louis Bossart 		}
366ed3da3d9STakashi Iwai 		new_hw_ptr = hw_base + pos;
3671da177e4SLinus Torvalds 	}
368f240406bSJaroslav Kysela       __delta:
369b406e610SClemens Ladisch 	delta = new_hw_ptr - old_hw_ptr;
370b406e610SClemens Ladisch 	if (delta < 0)
371b406e610SClemens Ladisch 		delta += runtime->boundary;
372ab69a490SClemens Ladisch 
37359ff878fSClemens Ladisch 	if (runtime->no_period_wakeup) {
37412ff414eSKelly Anderson 		snd_pcm_sframes_t xrun_threshold;
37559ff878fSClemens Ladisch 		/*
37659ff878fSClemens Ladisch 		 * Without regular period interrupts, we have to check
37759ff878fSClemens Ladisch 		 * the elapsed time to detect xruns.
37859ff878fSClemens Ladisch 		 */
3793509a03fSPierre-Louis Bossart 		jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
38047228e48SClemens Ladisch 		if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
38147228e48SClemens Ladisch 			goto no_delta_check;
38259ff878fSClemens Ladisch 		hdelta = jdelta - delta * HZ / runtime->rate;
38312ff414eSKelly Anderson 		xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
38412ff414eSKelly Anderson 		while (hdelta > xrun_threshold) {
38559ff878fSClemens Ladisch 			delta += runtime->buffer_size;
38659ff878fSClemens Ladisch 			hw_base += runtime->buffer_size;
3870e8014d7SPierre-Louis Bossart 			if (hw_base >= runtime->boundary) {
38859ff878fSClemens Ladisch 				hw_base = 0;
3890e8014d7SPierre-Louis Bossart 				crossed_boundary++;
3900e8014d7SPierre-Louis Bossart 			}
39159ff878fSClemens Ladisch 			new_hw_ptr = hw_base + pos;
39259ff878fSClemens Ladisch 			hdelta -= runtime->hw_ptr_buffer_jiffies;
39359ff878fSClemens Ladisch 		}
394ab69a490SClemens Ladisch 		goto no_delta_check;
39559ff878fSClemens Ladisch 	}
396ab69a490SClemens Ladisch 
397f240406bSJaroslav Kysela 	/* something must be really wrong */
3987b3a177bSJaroslav Kysela 	if (delta >= runtime->buffer_size + runtime->period_size) {
399f5914908STakashi Iwai 		hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
400f5914908STakashi Iwai 			     "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
401f240406bSJaroslav Kysela 			     substream->stream, (long)pos,
402f240406bSJaroslav Kysela 			     (long)new_hw_ptr, (long)old_hw_ptr);
403f240406bSJaroslav Kysela 		return 0;
4041da177e4SLinus Torvalds 	}
405c87d9732STakashi Iwai 
406c87d9732STakashi Iwai 	/* Do jiffies check only in xrun_debug mode */
407741b20cfSJaroslav Kysela 	if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
408c87d9732STakashi Iwai 		goto no_jiffies_check;
409c87d9732STakashi Iwai 
4103e5b5016STakashi Iwai 	/* Skip the jiffies check for hardwares with BATCH flag.
4113e5b5016STakashi Iwai 	 * Such hardware usually just increases the position at each IRQ,
4123e5b5016STakashi Iwai 	 * thus it can't give any strange position.
4133e5b5016STakashi Iwai 	 */
4143e5b5016STakashi Iwai 	if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
4153e5b5016STakashi Iwai 		goto no_jiffies_check;
416f240406bSJaroslav Kysela 	hdelta = delta;
417a4444da3SJaroslav Kysela 	if (hdelta < runtime->delay)
418a4444da3SJaroslav Kysela 		goto no_jiffies_check;
419a4444da3SJaroslav Kysela 	hdelta -= runtime->delay;
4203509a03fSPierre-Louis Bossart 	jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
421bbf6ad13SJaroslav Kysela 	if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
422bbf6ad13SJaroslav Kysela 		delta = jdelta /
423bbf6ad13SJaroslav Kysela 			(((runtime->period_size * HZ) / runtime->rate)
424bbf6ad13SJaroslav Kysela 								+ HZ/100);
425f240406bSJaroslav Kysela 		/* move new_hw_ptr according jiffies not pos variable */
426f240406bSJaroslav Kysela 		new_hw_ptr = old_hw_ptr;
427ed69c6a8SJaroslav Kysela 		hw_base = delta;
428f240406bSJaroslav Kysela 		/* use loop to avoid checks for delta overflows */
429f240406bSJaroslav Kysela 		/* the delta value is small or zero in most cases */
430f240406bSJaroslav Kysela 		while (delta > 0) {
431f240406bSJaroslav Kysela 			new_hw_ptr += runtime->period_size;
4320e8014d7SPierre-Louis Bossart 			if (new_hw_ptr >= runtime->boundary) {
433f240406bSJaroslav Kysela 				new_hw_ptr -= runtime->boundary;
4340e8014d7SPierre-Louis Bossart 				crossed_boundary--;
4350e8014d7SPierre-Louis Bossart 			}
436f240406bSJaroslav Kysela 			delta--;
437f240406bSJaroslav Kysela 		}
438f240406bSJaroslav Kysela 		/* align hw_base to buffer_size */
439f5914908STakashi Iwai 		hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
440f5914908STakashi Iwai 			     "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
441bbf6ad13SJaroslav Kysela 			     (long)pos, (long)hdelta,
442bbf6ad13SJaroslav Kysela 			     (long)runtime->period_size, jdelta,
443ed69c6a8SJaroslav Kysela 			     ((hdelta * HZ) / runtime->rate), hw_base,
444f240406bSJaroslav Kysela 			     (unsigned long)old_hw_ptr,
445f240406bSJaroslav Kysela 			     (unsigned long)new_hw_ptr);
446ed69c6a8SJaroslav Kysela 		/* reset values to proper state */
447ed69c6a8SJaroslav Kysela 		delta = 0;
448ed69c6a8SJaroslav Kysela 		hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
449bbf6ad13SJaroslav Kysela 	}
4503e5b5016STakashi Iwai  no_jiffies_check:
451bbf6ad13SJaroslav Kysela 	if (delta > runtime->period_size + runtime->period_size / 2) {
452f5914908STakashi Iwai 		hw_ptr_error(substream, in_interrupt,
453f5914908STakashi Iwai 			     "Lost interrupts?",
454f5914908STakashi Iwai 			     "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
455ed3da3d9STakashi Iwai 			     substream->stream, (long)delta,
456f240406bSJaroslav Kysela 			     (long)new_hw_ptr,
457f240406bSJaroslav Kysela 			     (long)old_hw_ptr);
4581da177e4SLinus Torvalds 	}
459f240406bSJaroslav Kysela 
460ab69a490SClemens Ladisch  no_delta_check:
4613179f620SPierre-Louis Bossart 	if (runtime->status->hw_ptr == new_hw_ptr) {
462e7513c57SBrent Lu 		runtime->hw_ptr_jiffies = curr_jiffies;
4633179f620SPierre-Louis Bossart 		update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
464f240406bSJaroslav Kysela 		return 0;
4653179f620SPierre-Louis Bossart 	}
466ab1863fcSTakashi Iwai 
467d7f5dd97SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
468d7f5dd97SJaroslav Kysela 	    runtime->silence_size > 0)
469d7f5dd97SJaroslav Kysela 		snd_pcm_playback_silence(substream, new_hw_ptr);
470d7f5dd97SJaroslav Kysela 
471e7636925SJaroslav Kysela 	if (in_interrupt) {
472ead4046bSClemens Ladisch 		delta = new_hw_ptr - runtime->hw_ptr_interrupt;
473ead4046bSClemens Ladisch 		if (delta < 0)
474ead4046bSClemens Ladisch 			delta += runtime->boundary;
475ead4046bSClemens Ladisch 		delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
476ead4046bSClemens Ladisch 		runtime->hw_ptr_interrupt += delta;
477ead4046bSClemens Ladisch 		if (runtime->hw_ptr_interrupt >= runtime->boundary)
478ead4046bSClemens Ladisch 			runtime->hw_ptr_interrupt -= runtime->boundary;
479e7636925SJaroslav Kysela 	}
480ed3da3d9STakashi Iwai 	runtime->hw_ptr_base = hw_base;
4811da177e4SLinus Torvalds 	runtime->status->hw_ptr = new_hw_ptr;
4823509a03fSPierre-Louis Bossart 	runtime->hw_ptr_jiffies = curr_jiffies;
4830e8014d7SPierre-Louis Bossart 	if (crossed_boundary) {
4840e8014d7SPierre-Louis Bossart 		snd_BUG_ON(crossed_boundary != 1);
4850e8014d7SPierre-Louis Bossart 		runtime->hw_ptr_wrap += runtime->boundary;
4860e8014d7SPierre-Louis Bossart 	}
4871da177e4SLinus Torvalds 
4883179f620SPierre-Louis Bossart 	update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
4894eeaaeaeSPierre-Louis Bossart 
4901250932eSJaroslav Kysela 	return snd_pcm_update_state(substream, runtime);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /* CAUTION: call it with irq disabled */
snd_pcm_update_hw_ptr(struct snd_pcm_substream * substream)494877211f5STakashi Iwai int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
4951da177e4SLinus Torvalds {
496f240406bSJaroslav Kysela 	return snd_pcm_update_hw_ptr0(substream, 0);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /**
5001da177e4SLinus Torvalds  * snd_pcm_set_ops - set the PCM operators
5011da177e4SLinus Torvalds  * @pcm: the pcm instance
5021da177e4SLinus Torvalds  * @direction: stream direction, SNDRV_PCM_STREAM_XXX
5031da177e4SLinus Torvalds  * @ops: the operator table
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  * Sets the given PCM operators to the pcm instance.
5061da177e4SLinus Torvalds  */
snd_pcm_set_ops(struct snd_pcm * pcm,int direction,const struct snd_pcm_ops * ops)507e6c2e7ebSLars-Peter Clausen void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
508e6c2e7ebSLars-Peter Clausen 		     const struct snd_pcm_ops *ops)
5091da177e4SLinus Torvalds {
510877211f5STakashi Iwai 	struct snd_pcm_str *stream = &pcm->streams[direction];
511877211f5STakashi Iwai 	struct snd_pcm_substream *substream;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	for (substream = stream->substream; substream != NULL; substream = substream->next)
5141da177e4SLinus Torvalds 		substream->ops = ops;
5151da177e4SLinus Torvalds }
516e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_set_ops);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /**
519f7b6603cSMauro Carvalho Chehab  * snd_pcm_set_sync - set the PCM sync id
5201da177e4SLinus Torvalds  * @substream: the pcm substream
5211da177e4SLinus Torvalds  *
5221da177e4SLinus Torvalds  * Sets the PCM sync identifier for the card.
5231da177e4SLinus Torvalds  */
snd_pcm_set_sync(struct snd_pcm_substream * substream)524877211f5STakashi Iwai void snd_pcm_set_sync(struct snd_pcm_substream *substream)
5251da177e4SLinus Torvalds {
526877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	runtime->sync.id32[0] = substream->pcm->card->number;
5291da177e4SLinus Torvalds 	runtime->sync.id32[1] = -1;
5301da177e4SLinus Torvalds 	runtime->sync.id32[2] = -1;
5311da177e4SLinus Torvalds 	runtime->sync.id32[3] = -1;
5321da177e4SLinus Torvalds }
533e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_set_sync);
534e88e8ae6STakashi Iwai 
5351da177e4SLinus Torvalds /*
5361da177e4SLinus Torvalds  *  Standard ioctl routine
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds 
div32(unsigned int a,unsigned int b,unsigned int * r)5391da177e4SLinus Torvalds static inline unsigned int div32(unsigned int a, unsigned int b,
5401da177e4SLinus Torvalds 				 unsigned int *r)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	if (b == 0) {
5431da177e4SLinus Torvalds 		*r = 0;
5441da177e4SLinus Torvalds 		return UINT_MAX;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 	*r = a % b;
5471da177e4SLinus Torvalds 	return a / b;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
div_down(unsigned int a,unsigned int b)5501da177e4SLinus Torvalds static inline unsigned int div_down(unsigned int a, unsigned int b)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	if (b == 0)
5531da177e4SLinus Torvalds 		return UINT_MAX;
5541da177e4SLinus Torvalds 	return a / b;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
div_up(unsigned int a,unsigned int b)5571da177e4SLinus Torvalds static inline unsigned int div_up(unsigned int a, unsigned int b)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	unsigned int r;
5601da177e4SLinus Torvalds 	unsigned int q;
5611da177e4SLinus Torvalds 	if (b == 0)
5621da177e4SLinus Torvalds 		return UINT_MAX;
5631da177e4SLinus Torvalds 	q = div32(a, b, &r);
5641da177e4SLinus Torvalds 	if (r)
5651da177e4SLinus Torvalds 		++q;
5661da177e4SLinus Torvalds 	return q;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
mul(unsigned int a,unsigned int b)5691da177e4SLinus Torvalds static inline unsigned int mul(unsigned int a, unsigned int b)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	if (a == 0)
5721da177e4SLinus Torvalds 		return 0;
5731da177e4SLinus Torvalds 	if (div_down(UINT_MAX, a) < b)
5741da177e4SLinus Torvalds 		return UINT_MAX;
5751da177e4SLinus Torvalds 	return a * b;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
muldiv32(unsigned int a,unsigned int b,unsigned int c,unsigned int * r)5781da177e4SLinus Torvalds static inline unsigned int muldiv32(unsigned int a, unsigned int b,
5791da177e4SLinus Torvalds 				    unsigned int c, unsigned int *r)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	u_int64_t n = (u_int64_t) a * b;
5821da177e4SLinus Torvalds 	if (c == 0) {
5831da177e4SLinus Torvalds 		*r = 0;
5841da177e4SLinus Torvalds 		return UINT_MAX;
5851da177e4SLinus Torvalds 	}
5863f7440a6STakashi Iwai 	n = div_u64_rem(n, c, r);
5871da177e4SLinus Torvalds 	if (n >= UINT_MAX) {
5881da177e4SLinus Torvalds 		*r = 0;
5891da177e4SLinus Torvalds 		return UINT_MAX;
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 	return n;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /**
5951da177e4SLinus Torvalds  * snd_interval_refine - refine the interval value of configurator
5961da177e4SLinus Torvalds  * @i: the interval value to refine
5971da177e4SLinus Torvalds  * @v: the interval value to refer to
5981da177e4SLinus Torvalds  *
5991da177e4SLinus Torvalds  * Refines the interval value with the reference value.
6001da177e4SLinus Torvalds  * The interval is changed to the range satisfying both intervals.
6011da177e4SLinus Torvalds  * The interval status (min, max, integer, etc.) are evaluated.
6021da177e4SLinus Torvalds  *
603eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
604eb7c06e8SYacine Belkadi  * negative error code.
6051da177e4SLinus Torvalds  */
snd_interval_refine(struct snd_interval * i,const struct snd_interval * v)606877211f5STakashi Iwai int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	int changed = 0;
6097eaa943cSTakashi Iwai 	if (snd_BUG_ON(snd_interval_empty(i)))
6107eaa943cSTakashi Iwai 		return -EINVAL;
6111da177e4SLinus Torvalds 	if (i->min < v->min) {
6121da177e4SLinus Torvalds 		i->min = v->min;
6131da177e4SLinus Torvalds 		i->openmin = v->openmin;
6141da177e4SLinus Torvalds 		changed = 1;
6151da177e4SLinus Torvalds 	} else if (i->min == v->min && !i->openmin && v->openmin) {
6161da177e4SLinus Torvalds 		i->openmin = 1;
6171da177e4SLinus Torvalds 		changed = 1;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 	if (i->max > v->max) {
6201da177e4SLinus Torvalds 		i->max = v->max;
6211da177e4SLinus Torvalds 		i->openmax = v->openmax;
6221da177e4SLinus Torvalds 		changed = 1;
6231da177e4SLinus Torvalds 	} else if (i->max == v->max && !i->openmax && v->openmax) {
6241da177e4SLinus Torvalds 		i->openmax = 1;
6251da177e4SLinus Torvalds 		changed = 1;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	if (!i->integer && v->integer) {
6281da177e4SLinus Torvalds 		i->integer = 1;
6291da177e4SLinus Torvalds 		changed = 1;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 	if (i->integer) {
6321da177e4SLinus Torvalds 		if (i->openmin) {
6331da177e4SLinus Torvalds 			i->min++;
6341da177e4SLinus Torvalds 			i->openmin = 0;
6351da177e4SLinus Torvalds 		}
6361da177e4SLinus Torvalds 		if (i->openmax) {
6371da177e4SLinus Torvalds 			i->max--;
6381da177e4SLinus Torvalds 			i->openmax = 0;
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 	} else if (!i->openmin && !i->openmax && i->min == i->max)
6411da177e4SLinus Torvalds 		i->integer = 1;
6421da177e4SLinus Torvalds 	if (snd_interval_checkempty(i)) {
6431da177e4SLinus Torvalds 		snd_interval_none(i);
6441da177e4SLinus Torvalds 		return -EINVAL;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 	return changed;
6471da177e4SLinus Torvalds }
648e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_interval_refine);
649e88e8ae6STakashi Iwai 
snd_interval_refine_first(struct snd_interval * i)650877211f5STakashi Iwai static int snd_interval_refine_first(struct snd_interval *i)
6511da177e4SLinus Torvalds {
652ff2d6acdSTimo Wischer 	const unsigned int last_max = i->max;
653ff2d6acdSTimo Wischer 
6547eaa943cSTakashi Iwai 	if (snd_BUG_ON(snd_interval_empty(i)))
6557eaa943cSTakashi Iwai 		return -EINVAL;
6561da177e4SLinus Torvalds 	if (snd_interval_single(i))
6571da177e4SLinus Torvalds 		return 0;
6581da177e4SLinus Torvalds 	i->max = i->min;
659ff2d6acdSTimo Wischer 	if (i->openmin)
6601da177e4SLinus Torvalds 		i->max++;
661ff2d6acdSTimo Wischer 	/* only exclude max value if also excluded before refine */
662ff2d6acdSTimo Wischer 	i->openmax = (i->openmax && i->max >= last_max);
6631da177e4SLinus Torvalds 	return 1;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
snd_interval_refine_last(struct snd_interval * i)666877211f5STakashi Iwai static int snd_interval_refine_last(struct snd_interval *i)
6671da177e4SLinus Torvalds {
668ff2d6acdSTimo Wischer 	const unsigned int last_min = i->min;
669ff2d6acdSTimo Wischer 
6707eaa943cSTakashi Iwai 	if (snd_BUG_ON(snd_interval_empty(i)))
6717eaa943cSTakashi Iwai 		return -EINVAL;
6721da177e4SLinus Torvalds 	if (snd_interval_single(i))
6731da177e4SLinus Torvalds 		return 0;
6741da177e4SLinus Torvalds 	i->min = i->max;
675ff2d6acdSTimo Wischer 	if (i->openmax)
6761da177e4SLinus Torvalds 		i->min--;
677ff2d6acdSTimo Wischer 	/* only exclude min value if also excluded before refine */
678ff2d6acdSTimo Wischer 	i->openmin = (i->openmin && i->min <= last_min);
6791da177e4SLinus Torvalds 	return 1;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
snd_interval_mul(const struct snd_interval * a,const struct snd_interval * b,struct snd_interval * c)682877211f5STakashi Iwai void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	if (a->empty || b->empty) {
6851da177e4SLinus Torvalds 		snd_interval_none(c);
6861da177e4SLinus Torvalds 		return;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	c->empty = 0;
6891da177e4SLinus Torvalds 	c->min = mul(a->min, b->min);
6901da177e4SLinus Torvalds 	c->openmin = (a->openmin || b->openmin);
6911da177e4SLinus Torvalds 	c->max = mul(a->max,  b->max);
6921da177e4SLinus Torvalds 	c->openmax = (a->openmax || b->openmax);
6931da177e4SLinus Torvalds 	c->integer = (a->integer && b->integer);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds /**
6971da177e4SLinus Torvalds  * snd_interval_div - refine the interval value with division
698df8db936STakashi Iwai  * @a: dividend
699df8db936STakashi Iwai  * @b: divisor
700df8db936STakashi Iwai  * @c: quotient
7011da177e4SLinus Torvalds  *
7021da177e4SLinus Torvalds  * c = a / b
7031da177e4SLinus Torvalds  *
7041da177e4SLinus Torvalds  * Returns non-zero if the value is changed, zero if not changed.
7051da177e4SLinus Torvalds  */
snd_interval_div(const struct snd_interval * a,const struct snd_interval * b,struct snd_interval * c)706877211f5STakashi Iwai void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	unsigned int r;
7091da177e4SLinus Torvalds 	if (a->empty || b->empty) {
7101da177e4SLinus Torvalds 		snd_interval_none(c);
7111da177e4SLinus Torvalds 		return;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 	c->empty = 0;
7141da177e4SLinus Torvalds 	c->min = div32(a->min, b->max, &r);
7151da177e4SLinus Torvalds 	c->openmin = (r || a->openmin || b->openmax);
7161da177e4SLinus Torvalds 	if (b->min > 0) {
7171da177e4SLinus Torvalds 		c->max = div32(a->max, b->min, &r);
7181da177e4SLinus Torvalds 		if (r) {
7191da177e4SLinus Torvalds 			c->max++;
7201da177e4SLinus Torvalds 			c->openmax = 1;
7211da177e4SLinus Torvalds 		} else
7221da177e4SLinus Torvalds 			c->openmax = (a->openmax || b->openmin);
7231da177e4SLinus Torvalds 	} else {
7241da177e4SLinus Torvalds 		c->max = UINT_MAX;
7251da177e4SLinus Torvalds 		c->openmax = 0;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 	c->integer = 0;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds /**
7311da177e4SLinus Torvalds  * snd_interval_muldivk - refine the interval value
732df8db936STakashi Iwai  * @a: dividend 1
733df8db936STakashi Iwai  * @b: dividend 2
734df8db936STakashi Iwai  * @k: divisor (as integer)
735df8db936STakashi Iwai  * @c: result
7361da177e4SLinus Torvalds   *
7371da177e4SLinus Torvalds  * c = a * b / k
7381da177e4SLinus Torvalds  *
7391da177e4SLinus Torvalds  * Returns non-zero if the value is changed, zero if not changed.
7401da177e4SLinus Torvalds  */
snd_interval_muldivk(const struct snd_interval * a,const struct snd_interval * b,unsigned int k,struct snd_interval * c)741877211f5STakashi Iwai void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
742877211f5STakashi Iwai 		      unsigned int k, struct snd_interval *c)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	unsigned int r;
7451da177e4SLinus Torvalds 	if (a->empty || b->empty) {
7461da177e4SLinus Torvalds 		snd_interval_none(c);
7471da177e4SLinus Torvalds 		return;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	c->empty = 0;
7501da177e4SLinus Torvalds 	c->min = muldiv32(a->min, b->min, k, &r);
7511da177e4SLinus Torvalds 	c->openmin = (r || a->openmin || b->openmin);
7521da177e4SLinus Torvalds 	c->max = muldiv32(a->max, b->max, k, &r);
7531da177e4SLinus Torvalds 	if (r) {
7541da177e4SLinus Torvalds 		c->max++;
7551da177e4SLinus Torvalds 		c->openmax = 1;
7561da177e4SLinus Torvalds 	} else
7571da177e4SLinus Torvalds 		c->openmax = (a->openmax || b->openmax);
7581da177e4SLinus Torvalds 	c->integer = 0;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds /**
7621da177e4SLinus Torvalds  * snd_interval_mulkdiv - refine the interval value
763df8db936STakashi Iwai  * @a: dividend 1
764df8db936STakashi Iwai  * @k: dividend 2 (as integer)
765df8db936STakashi Iwai  * @b: divisor
766df8db936STakashi Iwai  * @c: result
7671da177e4SLinus Torvalds  *
7681da177e4SLinus Torvalds  * c = a * k / b
7691da177e4SLinus Torvalds  *
7701da177e4SLinus Torvalds  * Returns non-zero if the value is changed, zero if not changed.
7711da177e4SLinus Torvalds  */
snd_interval_mulkdiv(const struct snd_interval * a,unsigned int k,const struct snd_interval * b,struct snd_interval * c)772877211f5STakashi Iwai void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
773877211f5STakashi Iwai 		      const struct snd_interval *b, struct snd_interval *c)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	unsigned int r;
7761da177e4SLinus Torvalds 	if (a->empty || b->empty) {
7771da177e4SLinus Torvalds 		snd_interval_none(c);
7781da177e4SLinus Torvalds 		return;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	c->empty = 0;
7811da177e4SLinus Torvalds 	c->min = muldiv32(a->min, k, b->max, &r);
7821da177e4SLinus Torvalds 	c->openmin = (r || a->openmin || b->openmax);
7831da177e4SLinus Torvalds 	if (b->min > 0) {
7841da177e4SLinus Torvalds 		c->max = muldiv32(a->max, k, b->min, &r);
7851da177e4SLinus Torvalds 		if (r) {
7861da177e4SLinus Torvalds 			c->max++;
7871da177e4SLinus Torvalds 			c->openmax = 1;
7881da177e4SLinus Torvalds 		} else
7891da177e4SLinus Torvalds 			c->openmax = (a->openmax || b->openmin);
7901da177e4SLinus Torvalds 	} else {
7911da177e4SLinus Torvalds 		c->max = UINT_MAX;
7921da177e4SLinus Torvalds 		c->openmax = 0;
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 	c->integer = 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds /* ---- */
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /**
8011da177e4SLinus Torvalds  * snd_interval_ratnum - refine the interval value
802df8db936STakashi Iwai  * @i: interval to refine
803df8db936STakashi Iwai  * @rats_count: number of ratnum_t
804df8db936STakashi Iwai  * @rats: ratnum_t array
805df8db936STakashi Iwai  * @nump: pointer to store the resultant numerator
806df8db936STakashi Iwai  * @denp: pointer to store the resultant denominator
8071da177e4SLinus Torvalds  *
808eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
809eb7c06e8SYacine Belkadi  * negative error code.
8101da177e4SLinus Torvalds  */
snd_interval_ratnum(struct snd_interval * i,unsigned int rats_count,const struct snd_ratnum * rats,unsigned int * nump,unsigned int * denp)811877211f5STakashi Iwai int snd_interval_ratnum(struct snd_interval *i,
812e5e113cfSLars-Peter Clausen 			unsigned int rats_count, const struct snd_ratnum *rats,
8131da177e4SLinus Torvalds 			unsigned int *nump, unsigned int *denp)
8141da177e4SLinus Torvalds {
8158374e24cSKrzysztof Helt 	unsigned int best_num, best_den;
8168374e24cSKrzysztof Helt 	int best_diff;
8171da177e4SLinus Torvalds 	unsigned int k;
818877211f5STakashi Iwai 	struct snd_interval t;
8191da177e4SLinus Torvalds 	int err;
8208374e24cSKrzysztof Helt 	unsigned int result_num, result_den;
8218374e24cSKrzysztof Helt 	int result_diff;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	best_num = best_den = best_diff = 0;
8241da177e4SLinus Torvalds 	for (k = 0; k < rats_count; ++k) {
8251da177e4SLinus Torvalds 		unsigned int num = rats[k].num;
8261da177e4SLinus Torvalds 		unsigned int den;
8271da177e4SLinus Torvalds 		unsigned int q = i->min;
8281da177e4SLinus Torvalds 		int diff;
8291da177e4SLinus Torvalds 		if (q == 0)
8301da177e4SLinus Torvalds 			q = 1;
83140962d7cSKrzysztof Helt 		den = div_up(num, q);
8321da177e4SLinus Torvalds 		if (den < rats[k].den_min)
8331da177e4SLinus Torvalds 			continue;
8341da177e4SLinus Torvalds 		if (den > rats[k].den_max)
8351da177e4SLinus Torvalds 			den = rats[k].den_max;
8361da177e4SLinus Torvalds 		else {
8371da177e4SLinus Torvalds 			unsigned int r;
8381da177e4SLinus Torvalds 			r = (den - rats[k].den_min) % rats[k].den_step;
8391da177e4SLinus Torvalds 			if (r != 0)
8401da177e4SLinus Torvalds 				den -= r;
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 		diff = num - q * den;
8438374e24cSKrzysztof Helt 		if (diff < 0)
8448374e24cSKrzysztof Helt 			diff = -diff;
8451da177e4SLinus Torvalds 		if (best_num == 0 ||
8461da177e4SLinus Torvalds 		    diff * best_den < best_diff * den) {
8471da177e4SLinus Torvalds 			best_diff = diff;
8481da177e4SLinus Torvalds 			best_den = den;
8491da177e4SLinus Torvalds 			best_num = num;
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	if (best_den == 0) {
8531da177e4SLinus Torvalds 		i->empty = 1;
8541da177e4SLinus Torvalds 		return -EINVAL;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 	t.min = div_down(best_num, best_den);
8571da177e4SLinus Torvalds 	t.openmin = !!(best_num % best_den);
8581da177e4SLinus Torvalds 
8598374e24cSKrzysztof Helt 	result_num = best_num;
8608374e24cSKrzysztof Helt 	result_diff = best_diff;
8618374e24cSKrzysztof Helt 	result_den = best_den;
8621da177e4SLinus Torvalds 	best_num = best_den = best_diff = 0;
8631da177e4SLinus Torvalds 	for (k = 0; k < rats_count; ++k) {
8641da177e4SLinus Torvalds 		unsigned int num = rats[k].num;
8651da177e4SLinus Torvalds 		unsigned int den;
8661da177e4SLinus Torvalds 		unsigned int q = i->max;
8671da177e4SLinus Torvalds 		int diff;
8681da177e4SLinus Torvalds 		if (q == 0) {
8691da177e4SLinus Torvalds 			i->empty = 1;
8701da177e4SLinus Torvalds 			return -EINVAL;
8711da177e4SLinus Torvalds 		}
87240962d7cSKrzysztof Helt 		den = div_down(num, q);
8731da177e4SLinus Torvalds 		if (den > rats[k].den_max)
8741da177e4SLinus Torvalds 			continue;
8751da177e4SLinus Torvalds 		if (den < rats[k].den_min)
8761da177e4SLinus Torvalds 			den = rats[k].den_min;
8771da177e4SLinus Torvalds 		else {
8781da177e4SLinus Torvalds 			unsigned int r;
8791da177e4SLinus Torvalds 			r = (den - rats[k].den_min) % rats[k].den_step;
8801da177e4SLinus Torvalds 			if (r != 0)
8811da177e4SLinus Torvalds 				den += rats[k].den_step - r;
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 		diff = q * den - num;
8848374e24cSKrzysztof Helt 		if (diff < 0)
8858374e24cSKrzysztof Helt 			diff = -diff;
8861da177e4SLinus Torvalds 		if (best_num == 0 ||
8871da177e4SLinus Torvalds 		    diff * best_den < best_diff * den) {
8881da177e4SLinus Torvalds 			best_diff = diff;
8891da177e4SLinus Torvalds 			best_den = den;
8901da177e4SLinus Torvalds 			best_num = num;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 	if (best_den == 0) {
8941da177e4SLinus Torvalds 		i->empty = 1;
8951da177e4SLinus Torvalds 		return -EINVAL;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 	t.max = div_up(best_num, best_den);
8981da177e4SLinus Torvalds 	t.openmax = !!(best_num % best_den);
8991da177e4SLinus Torvalds 	t.integer = 0;
9001da177e4SLinus Torvalds 	err = snd_interval_refine(i, &t);
9011da177e4SLinus Torvalds 	if (err < 0)
9021da177e4SLinus Torvalds 		return err;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	if (snd_interval_single(i)) {
9058374e24cSKrzysztof Helt 		if (best_diff * result_den < result_diff * best_den) {
9068374e24cSKrzysztof Helt 			result_num = best_num;
9078374e24cSKrzysztof Helt 			result_den = best_den;
9088374e24cSKrzysztof Helt 		}
9091da177e4SLinus Torvalds 		if (nump)
9108374e24cSKrzysztof Helt 			*nump = result_num;
9111da177e4SLinus Torvalds 		if (denp)
9128374e24cSKrzysztof Helt 			*denp = result_den;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 	return err;
9151da177e4SLinus Torvalds }
916e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_interval_ratnum);
917e88e8ae6STakashi Iwai 
9181da177e4SLinus Torvalds /**
9191da177e4SLinus Torvalds  * snd_interval_ratden - refine the interval value
920df8db936STakashi Iwai  * @i: interval to refine
921877211f5STakashi Iwai  * @rats_count: number of struct ratden
922877211f5STakashi Iwai  * @rats: struct ratden array
923df8db936STakashi Iwai  * @nump: pointer to store the resultant numerator
924df8db936STakashi Iwai  * @denp: pointer to store the resultant denominator
9251da177e4SLinus Torvalds  *
926eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
927eb7c06e8SYacine Belkadi  * negative error code.
9281da177e4SLinus Torvalds  */
snd_interval_ratden(struct snd_interval * i,unsigned int rats_count,const struct snd_ratden * rats,unsigned int * nump,unsigned int * denp)929877211f5STakashi Iwai static int snd_interval_ratden(struct snd_interval *i,
930e5e113cfSLars-Peter Clausen 			       unsigned int rats_count,
931e5e113cfSLars-Peter Clausen 			       const struct snd_ratden *rats,
9321da177e4SLinus Torvalds 			       unsigned int *nump, unsigned int *denp)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	unsigned int best_num, best_diff, best_den;
9351da177e4SLinus Torvalds 	unsigned int k;
936877211f5STakashi Iwai 	struct snd_interval t;
9371da177e4SLinus Torvalds 	int err;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	best_num = best_den = best_diff = 0;
9401da177e4SLinus Torvalds 	for (k = 0; k < rats_count; ++k) {
9411da177e4SLinus Torvalds 		unsigned int num;
9421da177e4SLinus Torvalds 		unsigned int den = rats[k].den;
9431da177e4SLinus Torvalds 		unsigned int q = i->min;
9441da177e4SLinus Torvalds 		int diff;
9451da177e4SLinus Torvalds 		num = mul(q, den);
9461da177e4SLinus Torvalds 		if (num > rats[k].num_max)
9471da177e4SLinus Torvalds 			continue;
9481da177e4SLinus Torvalds 		if (num < rats[k].num_min)
9491da177e4SLinus Torvalds 			num = rats[k].num_max;
9501da177e4SLinus Torvalds 		else {
9511da177e4SLinus Torvalds 			unsigned int r;
9521da177e4SLinus Torvalds 			r = (num - rats[k].num_min) % rats[k].num_step;
9531da177e4SLinus Torvalds 			if (r != 0)
9541da177e4SLinus Torvalds 				num += rats[k].num_step - r;
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 		diff = num - q * den;
9571da177e4SLinus Torvalds 		if (best_num == 0 ||
9581da177e4SLinus Torvalds 		    diff * best_den < best_diff * den) {
9591da177e4SLinus Torvalds 			best_diff = diff;
9601da177e4SLinus Torvalds 			best_den = den;
9611da177e4SLinus Torvalds 			best_num = num;
9621da177e4SLinus Torvalds 		}
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 	if (best_den == 0) {
9651da177e4SLinus Torvalds 		i->empty = 1;
9661da177e4SLinus Torvalds 		return -EINVAL;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 	t.min = div_down(best_num, best_den);
9691da177e4SLinus Torvalds 	t.openmin = !!(best_num % best_den);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	best_num = best_den = best_diff = 0;
9721da177e4SLinus Torvalds 	for (k = 0; k < rats_count; ++k) {
9731da177e4SLinus Torvalds 		unsigned int num;
9741da177e4SLinus Torvalds 		unsigned int den = rats[k].den;
9751da177e4SLinus Torvalds 		unsigned int q = i->max;
9761da177e4SLinus Torvalds 		int diff;
9771da177e4SLinus Torvalds 		num = mul(q, den);
9781da177e4SLinus Torvalds 		if (num < rats[k].num_min)
9791da177e4SLinus Torvalds 			continue;
9801da177e4SLinus Torvalds 		if (num > rats[k].num_max)
9811da177e4SLinus Torvalds 			num = rats[k].num_max;
9821da177e4SLinus Torvalds 		else {
9831da177e4SLinus Torvalds 			unsigned int r;
9841da177e4SLinus Torvalds 			r = (num - rats[k].num_min) % rats[k].num_step;
9851da177e4SLinus Torvalds 			if (r != 0)
9861da177e4SLinus Torvalds 				num -= r;
9871da177e4SLinus Torvalds 		}
9881da177e4SLinus Torvalds 		diff = q * den - num;
9891da177e4SLinus Torvalds 		if (best_num == 0 ||
9901da177e4SLinus Torvalds 		    diff * best_den < best_diff * den) {
9911da177e4SLinus Torvalds 			best_diff = diff;
9921da177e4SLinus Torvalds 			best_den = den;
9931da177e4SLinus Torvalds 			best_num = num;
9941da177e4SLinus Torvalds 		}
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	if (best_den == 0) {
9971da177e4SLinus Torvalds 		i->empty = 1;
9981da177e4SLinus Torvalds 		return -EINVAL;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 	t.max = div_up(best_num, best_den);
10011da177e4SLinus Torvalds 	t.openmax = !!(best_num % best_den);
10021da177e4SLinus Torvalds 	t.integer = 0;
10031da177e4SLinus Torvalds 	err = snd_interval_refine(i, &t);
10041da177e4SLinus Torvalds 	if (err < 0)
10051da177e4SLinus Torvalds 		return err;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	if (snd_interval_single(i)) {
10081da177e4SLinus Torvalds 		if (nump)
10091da177e4SLinus Torvalds 			*nump = best_num;
10101da177e4SLinus Torvalds 		if (denp)
10111da177e4SLinus Torvalds 			*denp = best_den;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 	return err;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds /**
10171da177e4SLinus Torvalds  * snd_interval_list - refine the interval value from the list
10181da177e4SLinus Torvalds  * @i: the interval value to refine
10191da177e4SLinus Torvalds  * @count: the number of elements in the list
10201da177e4SLinus Torvalds  * @list: the value list
10211da177e4SLinus Torvalds  * @mask: the bit-mask to evaluate
10221da177e4SLinus Torvalds  *
10231da177e4SLinus Torvalds  * Refines the interval value from the list.
10241da177e4SLinus Torvalds  * When mask is non-zero, only the elements corresponding to bit 1 are
10251da177e4SLinus Torvalds  * evaluated.
10261da177e4SLinus Torvalds  *
1027eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
1028eb7c06e8SYacine Belkadi  * negative error code.
10291da177e4SLinus Torvalds  */
snd_interval_list(struct snd_interval * i,unsigned int count,const unsigned int * list,unsigned int mask)10304af87a93SMark Brown int snd_interval_list(struct snd_interval *i, unsigned int count,
10314af87a93SMark Brown 		      const unsigned int *list, unsigned int mask)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds         unsigned int k;
1034b1ddaf68SClemens Ladisch 	struct snd_interval list_range;
10350981a260STakashi Iwai 
10360981a260STakashi Iwai 	if (!count) {
10370981a260STakashi Iwai 		i->empty = 1;
10380981a260STakashi Iwai 		return -EINVAL;
10390981a260STakashi Iwai 	}
1040b1ddaf68SClemens Ladisch 	snd_interval_any(&list_range);
1041b1ddaf68SClemens Ladisch 	list_range.min = UINT_MAX;
1042b1ddaf68SClemens Ladisch 	list_range.max = 0;
10431da177e4SLinus Torvalds         for (k = 0; k < count; k++) {
10441da177e4SLinus Torvalds 		if (mask && !(mask & (1 << k)))
10451da177e4SLinus Torvalds 			continue;
1046b1ddaf68SClemens Ladisch 		if (!snd_interval_test(i, list[k]))
10471da177e4SLinus Torvalds 			continue;
1048b1ddaf68SClemens Ladisch 		list_range.min = min(list_range.min, list[k]);
1049b1ddaf68SClemens Ladisch 		list_range.max = max(list_range.max, list[k]);
10501da177e4SLinus Torvalds         }
1051b1ddaf68SClemens Ladisch 	return snd_interval_refine(i, &list_range);
10521da177e4SLinus Torvalds }
1053e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_interval_list);
1054e88e8ae6STakashi Iwai 
1055f66f898eSPeter Rosin /**
1056f66f898eSPeter Rosin  * snd_interval_ranges - refine the interval value from the list of ranges
1057f66f898eSPeter Rosin  * @i: the interval value to refine
1058f66f898eSPeter Rosin  * @count: the number of elements in the list of ranges
1059f66f898eSPeter Rosin  * @ranges: the ranges list
1060f66f898eSPeter Rosin  * @mask: the bit-mask to evaluate
1061f66f898eSPeter Rosin  *
1062f66f898eSPeter Rosin  * Refines the interval value from the list of ranges.
1063f66f898eSPeter Rosin  * When mask is non-zero, only the elements corresponding to bit 1 are
1064f66f898eSPeter Rosin  * evaluated.
1065f66f898eSPeter Rosin  *
1066f66f898eSPeter Rosin  * Return: Positive if the value is changed, zero if it's not changed, or a
1067f66f898eSPeter Rosin  * negative error code.
1068f66f898eSPeter Rosin  */
snd_interval_ranges(struct snd_interval * i,unsigned int count,const struct snd_interval * ranges,unsigned int mask)1069f66f898eSPeter Rosin int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1070f66f898eSPeter Rosin 			const struct snd_interval *ranges, unsigned int mask)
1071f66f898eSPeter Rosin {
1072f66f898eSPeter Rosin 	unsigned int k;
1073f66f898eSPeter Rosin 	struct snd_interval range_union;
1074f66f898eSPeter Rosin 	struct snd_interval range;
1075f66f898eSPeter Rosin 
1076f66f898eSPeter Rosin 	if (!count) {
1077f66f898eSPeter Rosin 		snd_interval_none(i);
1078f66f898eSPeter Rosin 		return -EINVAL;
1079f66f898eSPeter Rosin 	}
1080f66f898eSPeter Rosin 	snd_interval_any(&range_union);
1081f66f898eSPeter Rosin 	range_union.min = UINT_MAX;
1082f66f898eSPeter Rosin 	range_union.max = 0;
1083f66f898eSPeter Rosin 	for (k = 0; k < count; k++) {
1084f66f898eSPeter Rosin 		if (mask && !(mask & (1 << k)))
1085f66f898eSPeter Rosin 			continue;
1086f66f898eSPeter Rosin 		snd_interval_copy(&range, &ranges[k]);
1087f66f898eSPeter Rosin 		if (snd_interval_refine(&range, i) < 0)
1088f66f898eSPeter Rosin 			continue;
1089f66f898eSPeter Rosin 		if (snd_interval_empty(&range))
1090f66f898eSPeter Rosin 			continue;
1091f66f898eSPeter Rosin 
1092f66f898eSPeter Rosin 		if (range.min < range_union.min) {
1093f66f898eSPeter Rosin 			range_union.min = range.min;
1094f66f898eSPeter Rosin 			range_union.openmin = 1;
1095f66f898eSPeter Rosin 		}
1096f66f898eSPeter Rosin 		if (range.min == range_union.min && !range.openmin)
1097f66f898eSPeter Rosin 			range_union.openmin = 0;
1098f66f898eSPeter Rosin 		if (range.max > range_union.max) {
1099f66f898eSPeter Rosin 			range_union.max = range.max;
1100f66f898eSPeter Rosin 			range_union.openmax = 1;
1101f66f898eSPeter Rosin 		}
1102f66f898eSPeter Rosin 		if (range.max == range_union.max && !range.openmax)
1103f66f898eSPeter Rosin 			range_union.openmax = 0;
1104f66f898eSPeter Rosin 	}
1105f66f898eSPeter Rosin 	return snd_interval_refine(i, &range_union);
1106f66f898eSPeter Rosin }
1107f66f898eSPeter Rosin EXPORT_SYMBOL(snd_interval_ranges);
1108f66f898eSPeter Rosin 
snd_interval_step(struct snd_interval * i,unsigned int step)11090f519b62SClemens Ladisch static int snd_interval_step(struct snd_interval *i, unsigned int step)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	unsigned int n;
11121da177e4SLinus Torvalds 	int changed = 0;
11130f519b62SClemens Ladisch 	n = i->min % step;
11141da177e4SLinus Torvalds 	if (n != 0 || i->openmin) {
11151da177e4SLinus Torvalds 		i->min += step - n;
1116df1e4719SClemens Ladisch 		i->openmin = 0;
11171da177e4SLinus Torvalds 		changed = 1;
11181da177e4SLinus Torvalds 	}
11190f519b62SClemens Ladisch 	n = i->max % step;
11201da177e4SLinus Torvalds 	if (n != 0 || i->openmax) {
11211da177e4SLinus Torvalds 		i->max -= n;
1122df1e4719SClemens Ladisch 		i->openmax = 0;
11231da177e4SLinus Torvalds 		changed = 1;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 	if (snd_interval_checkempty(i)) {
11261da177e4SLinus Torvalds 		i->empty = 1;
11271da177e4SLinus Torvalds 		return -EINVAL;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 	return changed;
11301da177e4SLinus Torvalds }
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds /* Info constraints helpers */
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /**
11351da177e4SLinus Torvalds  * snd_pcm_hw_rule_add - add the hw-constraint rule
11361da177e4SLinus Torvalds  * @runtime: the pcm runtime instance
11371da177e4SLinus Torvalds  * @cond: condition bits
11381da177e4SLinus Torvalds  * @var: the variable to evaluate
11391da177e4SLinus Torvalds  * @func: the evaluation function
11401da177e4SLinus Torvalds  * @private: the private data pointer passed to function
11411da177e4SLinus Torvalds  * @dep: the dependent variables
11421da177e4SLinus Torvalds  *
1143eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
11441da177e4SLinus Torvalds  */
snd_pcm_hw_rule_add(struct snd_pcm_runtime * runtime,unsigned int cond,int var,snd_pcm_hw_rule_func_t func,void * private,int dep,...)1145877211f5STakashi Iwai int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
11461da177e4SLinus Torvalds 			int var,
11471da177e4SLinus Torvalds 			snd_pcm_hw_rule_func_t func, void *private,
11481da177e4SLinus Torvalds 			int dep, ...)
11491da177e4SLinus Torvalds {
1150877211f5STakashi Iwai 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1151877211f5STakashi Iwai 	struct snd_pcm_hw_rule *c;
11521da177e4SLinus Torvalds 	unsigned int k;
11531da177e4SLinus Torvalds 	va_list args;
11541da177e4SLinus Torvalds 	va_start(args, dep);
11551da177e4SLinus Torvalds 	if (constrs->rules_num >= constrs->rules_all) {
1156877211f5STakashi Iwai 		struct snd_pcm_hw_rule *new;
11571da177e4SLinus Torvalds 		unsigned int new_rules = constrs->rules_all + 16;
115864f0bd11SBartosz Golaszewski 		new = krealloc_array(constrs->rules, new_rules,
115964f0bd11SBartosz Golaszewski 				     sizeof(*c), GFP_KERNEL);
116087a1c8aaSJesper Juhl 		if (!new) {
116187a1c8aaSJesper Juhl 			va_end(args);
11621da177e4SLinus Torvalds 			return -ENOMEM;
116387a1c8aaSJesper Juhl 		}
11641da177e4SLinus Torvalds 		constrs->rules = new;
11651da177e4SLinus Torvalds 		constrs->rules_all = new_rules;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 	c = &constrs->rules[constrs->rules_num];
11681da177e4SLinus Torvalds 	c->cond = cond;
11691da177e4SLinus Torvalds 	c->func = func;
11701da177e4SLinus Torvalds 	c->var = var;
11711da177e4SLinus Torvalds 	c->private = private;
11721da177e4SLinus Torvalds 	k = 0;
11731da177e4SLinus Torvalds 	while (1) {
117487a1c8aaSJesper Juhl 		if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
117587a1c8aaSJesper Juhl 			va_end(args);
11767eaa943cSTakashi Iwai 			return -EINVAL;
117787a1c8aaSJesper Juhl 		}
11781da177e4SLinus Torvalds 		c->deps[k++] = dep;
11791da177e4SLinus Torvalds 		if (dep < 0)
11801da177e4SLinus Torvalds 			break;
11811da177e4SLinus Torvalds 		dep = va_arg(args, int);
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	constrs->rules_num++;
11841da177e4SLinus Torvalds 	va_end(args);
11851da177e4SLinus Torvalds 	return 0;
11861da177e4SLinus Torvalds }
1187e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1188e88e8ae6STakashi Iwai 
11891da177e4SLinus Torvalds /**
11901c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1191df8db936STakashi Iwai  * @runtime: PCM runtime instance
1192df8db936STakashi Iwai  * @var: hw_params variable to apply the mask
1193df8db936STakashi Iwai  * @mask: the bitmap mask
1194df8db936STakashi Iwai  *
11951c85cc64SRandy Dunlap  * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1196eb7c06e8SYacine Belkadi  *
1197eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
11981da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_mask(struct snd_pcm_runtime * runtime,snd_pcm_hw_param_t var,u_int32_t mask)1199877211f5STakashi Iwai int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
12001da177e4SLinus Torvalds 			       u_int32_t mask)
12011da177e4SLinus Torvalds {
1202877211f5STakashi Iwai 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1203877211f5STakashi Iwai 	struct snd_mask *maskp = constrs_mask(constrs, var);
12041da177e4SLinus Torvalds 	*maskp->bits &= mask;
12051da177e4SLinus Torvalds 	memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
12061da177e4SLinus Torvalds 	if (*maskp->bits == 0)
12071da177e4SLinus Torvalds 		return -EINVAL;
12081da177e4SLinus Torvalds 	return 0;
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /**
12121c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1213df8db936STakashi Iwai  * @runtime: PCM runtime instance
1214df8db936STakashi Iwai  * @var: hw_params variable to apply the mask
1215df8db936STakashi Iwai  * @mask: the 64bit bitmap mask
1216df8db936STakashi Iwai  *
12171c85cc64SRandy Dunlap  * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1218eb7c06e8SYacine Belkadi  *
1219eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
12201da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime * runtime,snd_pcm_hw_param_t var,u_int64_t mask)1221877211f5STakashi Iwai int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
12221da177e4SLinus Torvalds 				 u_int64_t mask)
12231da177e4SLinus Torvalds {
1224877211f5STakashi Iwai 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1225877211f5STakashi Iwai 	struct snd_mask *maskp = constrs_mask(constrs, var);
12261da177e4SLinus Torvalds 	maskp->bits[0] &= (u_int32_t)mask;
12271da177e4SLinus Torvalds 	maskp->bits[1] &= (u_int32_t)(mask >> 32);
12281da177e4SLinus Torvalds 	memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
12291da177e4SLinus Torvalds 	if (! maskp->bits[0] && ! maskp->bits[1])
12301da177e4SLinus Torvalds 		return -EINVAL;
12311da177e4SLinus Torvalds 	return 0;
12321da177e4SLinus Torvalds }
123363a5d4c6SMark Brown EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds /**
12361c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1237df8db936STakashi Iwai  * @runtime: PCM runtime instance
1238df8db936STakashi Iwai  * @var: hw_params variable to apply the integer constraint
1239df8db936STakashi Iwai  *
1240df8db936STakashi Iwai  * Apply the constraint of integer to an interval parameter.
1241eb7c06e8SYacine Belkadi  *
1242eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
1243eb7c06e8SYacine Belkadi  * negative error code.
12441da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_integer(struct snd_pcm_runtime * runtime,snd_pcm_hw_param_t var)1245877211f5STakashi Iwai int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
12461da177e4SLinus Torvalds {
1247877211f5STakashi Iwai 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
12481da177e4SLinus Torvalds 	return snd_interval_setinteger(constrs_interval(constrs, var));
12491da177e4SLinus Torvalds }
1250e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1251e88e8ae6STakashi Iwai 
12521da177e4SLinus Torvalds /**
12531c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1254df8db936STakashi Iwai  * @runtime: PCM runtime instance
1255df8db936STakashi Iwai  * @var: hw_params variable to apply the range
1256df8db936STakashi Iwai  * @min: the minimal value
1257df8db936STakashi Iwai  * @max: the maximal value
1258df8db936STakashi Iwai  *
1259df8db936STakashi Iwai  * Apply the min/max range constraint to an interval parameter.
1260eb7c06e8SYacine Belkadi  *
1261eb7c06e8SYacine Belkadi  * Return: Positive if the value is changed, zero if it's not changed, or a
1262eb7c06e8SYacine Belkadi  * negative error code.
12631da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime * runtime,snd_pcm_hw_param_t var,unsigned int min,unsigned int max)1264877211f5STakashi Iwai int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
12651da177e4SLinus Torvalds 				 unsigned int min, unsigned int max)
12661da177e4SLinus Torvalds {
1267877211f5STakashi Iwai 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1268877211f5STakashi Iwai 	struct snd_interval t;
12691da177e4SLinus Torvalds 	t.min = min;
12701da177e4SLinus Torvalds 	t.max = max;
12711da177e4SLinus Torvalds 	t.openmin = t.openmax = 0;
12721da177e4SLinus Torvalds 	t.integer = 0;
12731da177e4SLinus Torvalds 	return snd_interval_refine(constrs_interval(constrs, var), &t);
12741da177e4SLinus Torvalds }
1275e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1276e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_list(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1277877211f5STakashi Iwai static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1278877211f5STakashi Iwai 				struct snd_pcm_hw_rule *rule)
12791da177e4SLinus Torvalds {
1280877211f5STakashi Iwai 	struct snd_pcm_hw_constraint_list *list = rule->private;
12811da177e4SLinus Torvalds 	return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds /**
12861c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1287df8db936STakashi Iwai  * @runtime: PCM runtime instance
1288df8db936STakashi Iwai  * @cond: condition bits
1289df8db936STakashi Iwai  * @var: hw_params variable to apply the list constraint
1290df8db936STakashi Iwai  * @l: list
1291df8db936STakashi Iwai  *
1292df8db936STakashi Iwai  * Apply the list of constraints to an interval parameter.
1293eb7c06e8SYacine Belkadi  *
1294eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
12951da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_list(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var,const struct snd_pcm_hw_constraint_list * l)1296877211f5STakashi Iwai int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
12971da177e4SLinus Torvalds 			       unsigned int cond,
12981da177e4SLinus Torvalds 			       snd_pcm_hw_param_t var,
12991464189fSMark Brown 			       const struct snd_pcm_hw_constraint_list *l)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, var,
13021464189fSMark Brown 				   snd_pcm_hw_rule_list, (void *)l,
13031da177e4SLinus Torvalds 				   var, -1);
13041da177e4SLinus Torvalds }
1305e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1306e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1307f66f898eSPeter Rosin static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1308f66f898eSPeter Rosin 				  struct snd_pcm_hw_rule *rule)
1309f66f898eSPeter Rosin {
1310f66f898eSPeter Rosin 	struct snd_pcm_hw_constraint_ranges *r = rule->private;
1311f66f898eSPeter Rosin 	return snd_interval_ranges(hw_param_interval(params, rule->var),
1312f66f898eSPeter Rosin 				   r->count, r->ranges, r->mask);
1313f66f898eSPeter Rosin }
1314f66f898eSPeter Rosin 
1315f66f898eSPeter Rosin 
1316f66f898eSPeter Rosin /**
1317f66f898eSPeter Rosin  * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1318f66f898eSPeter Rosin  * @runtime: PCM runtime instance
1319f66f898eSPeter Rosin  * @cond: condition bits
1320f66f898eSPeter Rosin  * @var: hw_params variable to apply the list of range constraints
1321f66f898eSPeter Rosin  * @r: ranges
1322f66f898eSPeter Rosin  *
1323f66f898eSPeter Rosin  * Apply the list of range constraints to an interval parameter.
1324f66f898eSPeter Rosin  *
1325f66f898eSPeter Rosin  * Return: Zero if successful, or a negative error code on failure.
1326f66f898eSPeter Rosin  */
snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var,const struct snd_pcm_hw_constraint_ranges * r)1327f66f898eSPeter Rosin int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1328f66f898eSPeter Rosin 				 unsigned int cond,
1329f66f898eSPeter Rosin 				 snd_pcm_hw_param_t var,
1330f66f898eSPeter Rosin 				 const struct snd_pcm_hw_constraint_ranges *r)
1331f66f898eSPeter Rosin {
1332f66f898eSPeter Rosin 	return snd_pcm_hw_rule_add(runtime, cond, var,
1333f66f898eSPeter Rosin 				   snd_pcm_hw_rule_ranges, (void *)r,
1334f66f898eSPeter Rosin 				   var, -1);
1335f66f898eSPeter Rosin }
1336f66f898eSPeter Rosin EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1337f66f898eSPeter Rosin 
snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1338877211f5STakashi Iwai static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1339877211f5STakashi Iwai 				   struct snd_pcm_hw_rule *rule)
13401da177e4SLinus Torvalds {
1341e5e113cfSLars-Peter Clausen 	const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
13421da177e4SLinus Torvalds 	unsigned int num = 0, den = 0;
13431da177e4SLinus Torvalds 	int err;
13441da177e4SLinus Torvalds 	err = snd_interval_ratnum(hw_param_interval(params, rule->var),
13451da177e4SLinus Torvalds 				  r->nrats, r->rats, &num, &den);
13461da177e4SLinus Torvalds 	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
13471da177e4SLinus Torvalds 		params->rate_num = num;
13481da177e4SLinus Torvalds 		params->rate_den = den;
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 	return err;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds /**
13541c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1355df8db936STakashi Iwai  * @runtime: PCM runtime instance
1356df8db936STakashi Iwai  * @cond: condition bits
1357df8db936STakashi Iwai  * @var: hw_params variable to apply the ratnums constraint
1358877211f5STakashi Iwai  * @r: struct snd_ratnums constriants
1359eb7c06e8SYacine Belkadi  *
1360eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
13611da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var,const struct snd_pcm_hw_constraint_ratnums * r)1362877211f5STakashi Iwai int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
13631da177e4SLinus Torvalds 				  unsigned int cond,
13641da177e4SLinus Torvalds 				  snd_pcm_hw_param_t var,
1365e5e113cfSLars-Peter Clausen 				  const struct snd_pcm_hw_constraint_ratnums *r)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, var,
1368e5e113cfSLars-Peter Clausen 				   snd_pcm_hw_rule_ratnums, (void *)r,
13691da177e4SLinus Torvalds 				   var, -1);
13701da177e4SLinus Torvalds }
1371e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1372e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1373877211f5STakashi Iwai static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1374877211f5STakashi Iwai 				   struct snd_pcm_hw_rule *rule)
13751da177e4SLinus Torvalds {
1376e5e113cfSLars-Peter Clausen 	const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
13771da177e4SLinus Torvalds 	unsigned int num = 0, den = 0;
13781da177e4SLinus Torvalds 	int err = snd_interval_ratden(hw_param_interval(params, rule->var),
13791da177e4SLinus Torvalds 				  r->nrats, r->rats, &num, &den);
13801da177e4SLinus Torvalds 	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
13811da177e4SLinus Torvalds 		params->rate_num = num;
13821da177e4SLinus Torvalds 		params->rate_den = den;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	return err;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds /**
13881c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1389df8db936STakashi Iwai  * @runtime: PCM runtime instance
1390df8db936STakashi Iwai  * @cond: condition bits
1391df8db936STakashi Iwai  * @var: hw_params variable to apply the ratdens constraint
1392877211f5STakashi Iwai  * @r: struct snd_ratdens constriants
1393eb7c06e8SYacine Belkadi  *
1394eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
13951da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var,const struct snd_pcm_hw_constraint_ratdens * r)1396877211f5STakashi Iwai int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
13971da177e4SLinus Torvalds 				  unsigned int cond,
13981da177e4SLinus Torvalds 				  snd_pcm_hw_param_t var,
1399e5e113cfSLars-Peter Clausen 				  const struct snd_pcm_hw_constraint_ratdens *r)
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, var,
1402e5e113cfSLars-Peter Clausen 				   snd_pcm_hw_rule_ratdens, (void *)r,
14031da177e4SLinus Torvalds 				   var, -1);
14041da177e4SLinus Torvalds }
1405e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1406e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1407877211f5STakashi Iwai static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1408877211f5STakashi Iwai 				  struct snd_pcm_hw_rule *rule)
14091da177e4SLinus Torvalds {
14101da177e4SLinus Torvalds 	unsigned int l = (unsigned long) rule->private;
14111da177e4SLinus Torvalds 	int width = l & 0xffff;
14121da177e4SLinus Torvalds 	unsigned int msbits = l >> 16;
1413b55f9fdcSTakashi Sakamoto 	const struct snd_interval *i =
1414b55f9fdcSTakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
14158ef9df55SLars-Peter Clausen 
14168ef9df55SLars-Peter Clausen 	if (!snd_interval_single(i))
14178ef9df55SLars-Peter Clausen 		return 0;
14188ef9df55SLars-Peter Clausen 
14198ef9df55SLars-Peter Clausen 	if ((snd_interval_value(i) == width) ||
14208ef9df55SLars-Peter Clausen 	    (width == 0 && snd_interval_value(i) > msbits))
142119f52faeSLars-Peter Clausen 		params->msbits = min_not_zero(params->msbits, msbits);
14228ef9df55SLars-Peter Clausen 
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds /**
14271c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1428df8db936STakashi Iwai  * @runtime: PCM runtime instance
1429df8db936STakashi Iwai  * @cond: condition bits
1430df8db936STakashi Iwai  * @width: sample bits width
1431df8db936STakashi Iwai  * @msbits: msbits width
1432eb7c06e8SYacine Belkadi  *
14338ef9df55SLars-Peter Clausen  * This constraint will set the number of most significant bits (msbits) if a
14348ef9df55SLars-Peter Clausen  * sample format with the specified width has been select. If width is set to 0
14358ef9df55SLars-Peter Clausen  * the msbits will be set for any sample format with a width larger than the
14368ef9df55SLars-Peter Clausen  * specified msbits.
14378ef9df55SLars-Peter Clausen  *
1438eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
14391da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime * runtime,unsigned int cond,unsigned int width,unsigned int msbits)1440877211f5STakashi Iwai int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
14411da177e4SLinus Torvalds 				 unsigned int cond,
14421da177e4SLinus Torvalds 				 unsigned int width,
14431da177e4SLinus Torvalds 				 unsigned int msbits)
14441da177e4SLinus Torvalds {
14451da177e4SLinus Torvalds 	unsigned long l = (msbits << 16) | width;
14461da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, -1,
14471da177e4SLinus Torvalds 				    snd_pcm_hw_rule_msbits,
14481da177e4SLinus Torvalds 				    (void*) l,
14491da177e4SLinus Torvalds 				    SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
14501da177e4SLinus Torvalds }
1451e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1452e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_step(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1453877211f5STakashi Iwai static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1454877211f5STakashi Iwai 				struct snd_pcm_hw_rule *rule)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	unsigned long step = (unsigned long) rule->private;
14570f519b62SClemens Ladisch 	return snd_interval_step(hw_param_interval(params, rule->var), step);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds /**
14611c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_step - add a hw constraint step rule
1462df8db936STakashi Iwai  * @runtime: PCM runtime instance
1463df8db936STakashi Iwai  * @cond: condition bits
1464df8db936STakashi Iwai  * @var: hw_params variable to apply the step constraint
1465df8db936STakashi Iwai  * @step: step size
1466eb7c06e8SYacine Belkadi  *
1467eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
14681da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_step(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var,unsigned long step)1469877211f5STakashi Iwai int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
14701da177e4SLinus Torvalds 			       unsigned int cond,
14711da177e4SLinus Torvalds 			       snd_pcm_hw_param_t var,
14721da177e4SLinus Torvalds 			       unsigned long step)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, var,
14751da177e4SLinus Torvalds 				   snd_pcm_hw_rule_step, (void *) step,
14761da177e4SLinus Torvalds 				   var, -1);
14771da177e4SLinus Torvalds }
1478e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1479e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1480877211f5STakashi Iwai static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
14811da177e4SLinus Torvalds {
1482d03af9b8STakashi Iwai 	static const unsigned int pow2_sizes[] = {
14831da177e4SLinus Torvalds 		1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
14841da177e4SLinus Torvalds 		1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
14851da177e4SLinus Torvalds 		1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
14861da177e4SLinus Torvalds 		1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
14871da177e4SLinus Torvalds 	};
14881da177e4SLinus Torvalds 	return snd_interval_list(hw_param_interval(params, rule->var),
14891da177e4SLinus Torvalds 				 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds /**
14931c85cc64SRandy Dunlap  * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1494df8db936STakashi Iwai  * @runtime: PCM runtime instance
1495df8db936STakashi Iwai  * @cond: condition bits
1496df8db936STakashi Iwai  * @var: hw_params variable to apply the power-of-2 constraint
1497eb7c06e8SYacine Belkadi  *
1498eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
14991da177e4SLinus Torvalds  */
snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime * runtime,unsigned int cond,snd_pcm_hw_param_t var)1500877211f5STakashi Iwai int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
15011da177e4SLinus Torvalds 			       unsigned int cond,
15021da177e4SLinus Torvalds 			       snd_pcm_hw_param_t var)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds 	return snd_pcm_hw_rule_add(runtime, cond, var,
15051da177e4SLinus Torvalds 				   snd_pcm_hw_rule_pow2, NULL,
15061da177e4SLinus Torvalds 				   var, -1);
15071da177e4SLinus Torvalds }
1508e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1509e88e8ae6STakashi Iwai 
snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1510d5b702a6SClemens Ladisch static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1511d5b702a6SClemens Ladisch 					   struct snd_pcm_hw_rule *rule)
1512d5b702a6SClemens Ladisch {
1513d5b702a6SClemens Ladisch 	unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1514d5b702a6SClemens Ladisch 	struct snd_interval *rate;
1515d5b702a6SClemens Ladisch 
1516d5b702a6SClemens Ladisch 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1517d5b702a6SClemens Ladisch 	return snd_interval_list(rate, 1, &base_rate, 0);
1518d5b702a6SClemens Ladisch }
1519d5b702a6SClemens Ladisch 
1520d5b702a6SClemens Ladisch /**
1521d5b702a6SClemens Ladisch  * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1522d5b702a6SClemens Ladisch  * @runtime: PCM runtime instance
1523d5b702a6SClemens Ladisch  * @base_rate: the rate at which the hardware does not resample
1524eb7c06e8SYacine Belkadi  *
1525eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
1526d5b702a6SClemens Ladisch  */
snd_pcm_hw_rule_noresample(struct snd_pcm_runtime * runtime,unsigned int base_rate)1527d5b702a6SClemens Ladisch int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1528d5b702a6SClemens Ladisch 			       unsigned int base_rate)
1529d5b702a6SClemens Ladisch {
1530d5b702a6SClemens Ladisch 	return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1531d5b702a6SClemens Ladisch 				   SNDRV_PCM_HW_PARAM_RATE,
1532d5b702a6SClemens Ladisch 				   snd_pcm_hw_rule_noresample_func,
1533d5b702a6SClemens Ladisch 				   (void *)(uintptr_t)base_rate,
1534d5b702a6SClemens Ladisch 				   SNDRV_PCM_HW_PARAM_RATE, -1);
1535d5b702a6SClemens Ladisch }
1536d5b702a6SClemens Ladisch EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1537d5b702a6SClemens Ladisch 
_snd_pcm_hw_param_any(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1538877211f5STakashi Iwai static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1539123992f7SAdrian Bunk 				  snd_pcm_hw_param_t var)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	if (hw_is_mask(var)) {
15421da177e4SLinus Torvalds 		snd_mask_any(hw_param_mask(params, var));
15431da177e4SLinus Torvalds 		params->cmask |= 1 << var;
15441da177e4SLinus Torvalds 		params->rmask |= 1 << var;
15451da177e4SLinus Torvalds 		return;
15461da177e4SLinus Torvalds 	}
15471da177e4SLinus Torvalds 	if (hw_is_interval(var)) {
15481da177e4SLinus Torvalds 		snd_interval_any(hw_param_interval(params, var));
15491da177e4SLinus Torvalds 		params->cmask |= 1 << var;
15501da177e4SLinus Torvalds 		params->rmask |= 1 << var;
15511da177e4SLinus Torvalds 		return;
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 	snd_BUG();
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds 
_snd_pcm_hw_params_any(struct snd_pcm_hw_params * params)1556877211f5STakashi Iwai void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
15571da177e4SLinus Torvalds {
15581da177e4SLinus Torvalds 	unsigned int k;
15591da177e4SLinus Torvalds 	memset(params, 0, sizeof(*params));
15601da177e4SLinus Torvalds 	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
15611da177e4SLinus Torvalds 		_snd_pcm_hw_param_any(params, k);
15621da177e4SLinus Torvalds 	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
15631da177e4SLinus Torvalds 		_snd_pcm_hw_param_any(params, k);
15641da177e4SLinus Torvalds 	params->info = ~0U;
15651da177e4SLinus Torvalds }
1566e88e8ae6STakashi Iwai EXPORT_SYMBOL(_snd_pcm_hw_params_any);
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds /**
15691c85cc64SRandy Dunlap  * snd_pcm_hw_param_value - return @params field @var value
1570df8db936STakashi Iwai  * @params: the hw_params instance
1571df8db936STakashi Iwai  * @var: parameter to retrieve
15721c85cc64SRandy Dunlap  * @dir: pointer to the direction (-1,0,1) or %NULL
15731da177e4SLinus Torvalds  *
1574eb7c06e8SYacine Belkadi  * Return: The value for field @var if it's fixed in configuration space
1575eb7c06e8SYacine Belkadi  * defined by @params. -%EINVAL otherwise.
15761da177e4SLinus Torvalds  */
snd_pcm_hw_param_value(const struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,int * dir)1577e88e8ae6STakashi Iwai int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
15781da177e4SLinus Torvalds 			   snd_pcm_hw_param_t var, int *dir)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	if (hw_is_mask(var)) {
1581877211f5STakashi Iwai 		const struct snd_mask *mask = hw_param_mask_c(params, var);
15821da177e4SLinus Torvalds 		if (!snd_mask_single(mask))
15831da177e4SLinus Torvalds 			return -EINVAL;
15841da177e4SLinus Torvalds 		if (dir)
15851da177e4SLinus Torvalds 			*dir = 0;
15861da177e4SLinus Torvalds 		return snd_mask_value(mask);
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 	if (hw_is_interval(var)) {
1589877211f5STakashi Iwai 		const struct snd_interval *i = hw_param_interval_c(params, var);
15901da177e4SLinus Torvalds 		if (!snd_interval_single(i))
15911da177e4SLinus Torvalds 			return -EINVAL;
15921da177e4SLinus Torvalds 		if (dir)
15931da177e4SLinus Torvalds 			*dir = i->openmin;
15941da177e4SLinus Torvalds 		return snd_interval_value(i);
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds 	return -EINVAL;
15971da177e4SLinus Torvalds }
1598e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_param_value);
15991da177e4SLinus Torvalds 
_snd_pcm_hw_param_setempty(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1600877211f5STakashi Iwai void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
16011da177e4SLinus Torvalds 				snd_pcm_hw_param_t var)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds 	if (hw_is_mask(var)) {
16041da177e4SLinus Torvalds 		snd_mask_none(hw_param_mask(params, var));
16051da177e4SLinus Torvalds 		params->cmask |= 1 << var;
16061da177e4SLinus Torvalds 		params->rmask |= 1 << var;
16071da177e4SLinus Torvalds 	} else if (hw_is_interval(var)) {
16081da177e4SLinus Torvalds 		snd_interval_none(hw_param_interval(params, var));
16091da177e4SLinus Torvalds 		params->cmask |= 1 << var;
16101da177e4SLinus Torvalds 		params->rmask |= 1 << var;
16111da177e4SLinus Torvalds 	} else {
16121da177e4SLinus Torvalds 		snd_BUG();
16131da177e4SLinus Torvalds 	}
16141da177e4SLinus Torvalds }
1615e88e8ae6STakashi Iwai EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
16161da177e4SLinus Torvalds 
_snd_pcm_hw_param_first(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1617877211f5STakashi Iwai static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
16181da177e4SLinus Torvalds 				   snd_pcm_hw_param_t var)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	int changed;
16211da177e4SLinus Torvalds 	if (hw_is_mask(var))
16221da177e4SLinus Torvalds 		changed = snd_mask_refine_first(hw_param_mask(params, var));
16231da177e4SLinus Torvalds 	else if (hw_is_interval(var))
16241da177e4SLinus Torvalds 		changed = snd_interval_refine_first(hw_param_interval(params, var));
16252f4ca8e5STakashi Iwai 	else
16261da177e4SLinus Torvalds 		return -EINVAL;
16277a0a8716STakashi Iwai 	if (changed > 0) {
16281da177e4SLinus Torvalds 		params->cmask |= 1 << var;
16291da177e4SLinus Torvalds 		params->rmask |= 1 << var;
16301da177e4SLinus Torvalds 	}
16311da177e4SLinus Torvalds 	return changed;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds /**
16361c85cc64SRandy Dunlap  * snd_pcm_hw_param_first - refine config space and return minimum value
1637df8db936STakashi Iwai  * @pcm: PCM instance
1638df8db936STakashi Iwai  * @params: the hw_params instance
1639df8db936STakashi Iwai  * @var: parameter to retrieve
16401c85cc64SRandy Dunlap  * @dir: pointer to the direction (-1,0,1) or %NULL
16411da177e4SLinus Torvalds  *
16421c85cc64SRandy Dunlap  * Inside configuration space defined by @params remove from @var all
16431da177e4SLinus Torvalds  * values > minimum. Reduce configuration space accordingly.
1644eb7c06e8SYacine Belkadi  *
1645eb7c06e8SYacine Belkadi  * Return: The minimum, or a negative error code on failure.
16461da177e4SLinus Torvalds  */
snd_pcm_hw_param_first(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,int * dir)1647e88e8ae6STakashi Iwai int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1648877211f5STakashi Iwai 			   struct snd_pcm_hw_params *params,
16491da177e4SLinus Torvalds 			   snd_pcm_hw_param_t var, int *dir)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	int changed = _snd_pcm_hw_param_first(params, var);
16521da177e4SLinus Torvalds 	if (changed < 0)
16531da177e4SLinus Torvalds 		return changed;
16541da177e4SLinus Torvalds 	if (params->rmask) {
16551da177e4SLinus Torvalds 		int err = snd_pcm_hw_refine(pcm, params);
1656fe08f34dSTakashi Iwai 		if (err < 0)
16577eaa943cSTakashi Iwai 			return err;
16581da177e4SLinus Torvalds 	}
16591da177e4SLinus Torvalds 	return snd_pcm_hw_param_value(params, var, dir);
16601da177e4SLinus Torvalds }
1661e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_param_first);
1662e88e8ae6STakashi Iwai 
_snd_pcm_hw_param_last(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1663877211f5STakashi Iwai static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
16641da177e4SLinus Torvalds 				  snd_pcm_hw_param_t var)
16651da177e4SLinus Torvalds {
16661da177e4SLinus Torvalds 	int changed;
16671da177e4SLinus Torvalds 	if (hw_is_mask(var))
16681da177e4SLinus Torvalds 		changed = snd_mask_refine_last(hw_param_mask(params, var));
16691da177e4SLinus Torvalds 	else if (hw_is_interval(var))
16701da177e4SLinus Torvalds 		changed = snd_interval_refine_last(hw_param_interval(params, var));
16712f4ca8e5STakashi Iwai 	else
16721da177e4SLinus Torvalds 		return -EINVAL;
16737a0a8716STakashi Iwai 	if (changed > 0) {
16741da177e4SLinus Torvalds 		params->cmask |= 1 << var;
16751da177e4SLinus Torvalds 		params->rmask |= 1 << var;
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds 	return changed;
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds /**
16821c85cc64SRandy Dunlap  * snd_pcm_hw_param_last - refine config space and return maximum value
1683df8db936STakashi Iwai  * @pcm: PCM instance
1684df8db936STakashi Iwai  * @params: the hw_params instance
1685df8db936STakashi Iwai  * @var: parameter to retrieve
16861c85cc64SRandy Dunlap  * @dir: pointer to the direction (-1,0,1) or %NULL
16871da177e4SLinus Torvalds  *
16881c85cc64SRandy Dunlap  * Inside configuration space defined by @params remove from @var all
16891da177e4SLinus Torvalds  * values < maximum. Reduce configuration space accordingly.
1690eb7c06e8SYacine Belkadi  *
1691eb7c06e8SYacine Belkadi  * Return: The maximum, or a negative error code on failure.
16921da177e4SLinus Torvalds  */
snd_pcm_hw_param_last(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,int * dir)1693e88e8ae6STakashi Iwai int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1694877211f5STakashi Iwai 			  struct snd_pcm_hw_params *params,
16951da177e4SLinus Torvalds 			  snd_pcm_hw_param_t var, int *dir)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	int changed = _snd_pcm_hw_param_last(params, var);
16981da177e4SLinus Torvalds 	if (changed < 0)
16991da177e4SLinus Torvalds 		return changed;
17001da177e4SLinus Torvalds 	if (params->rmask) {
17011da177e4SLinus Torvalds 		int err = snd_pcm_hw_refine(pcm, params);
1702fe08f34dSTakashi Iwai 		if (err < 0)
17037eaa943cSTakashi Iwai 			return err;
17041da177e4SLinus Torvalds 	}
17051da177e4SLinus Torvalds 	return snd_pcm_hw_param_value(params, var, dir);
17061da177e4SLinus Torvalds }
1707e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_hw_param_last);
17081da177e4SLinus Torvalds 
snd_pcm_lib_ioctl_reset(struct snd_pcm_substream * substream,void * arg)1709877211f5STakashi Iwai static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
17101da177e4SLinus Torvalds 				   void *arg)
17111da177e4SLinus Torvalds {
1712877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17131da177e4SLinus Torvalds 	unsigned long flags;
17141da177e4SLinus Torvalds 	snd_pcm_stream_lock_irqsave(substream, flags);
17151da177e4SLinus Torvalds 	if (snd_pcm_running(substream) &&
17161da177e4SLinus Torvalds 	    snd_pcm_update_hw_ptr(substream) >= 0)
17171da177e4SLinus Torvalds 		runtime->status->hw_ptr %= runtime->buffer_size;
17180e8014d7SPierre-Louis Bossart 	else {
17191da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
17200e8014d7SPierre-Louis Bossart 		runtime->hw_ptr_wrap = 0;
17210e8014d7SPierre-Louis Bossart 	}
17221da177e4SLinus Torvalds 	snd_pcm_stream_unlock_irqrestore(substream, flags);
17231da177e4SLinus Torvalds 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream * substream,void * arg)1726877211f5STakashi Iwai static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
17271da177e4SLinus Torvalds 					  void *arg)
17281da177e4SLinus Torvalds {
1729877211f5STakashi Iwai 	struct snd_pcm_channel_info *info = arg;
1730877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17311da177e4SLinus Torvalds 	int width;
17321da177e4SLinus Torvalds 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
17331da177e4SLinus Torvalds 		info->offset = -1;
17341da177e4SLinus Torvalds 		return 0;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 	width = snd_pcm_format_physical_width(runtime->format);
17371da177e4SLinus Torvalds 	if (width < 0)
17381da177e4SLinus Torvalds 		return width;
17391da177e4SLinus Torvalds 	info->offset = 0;
17401da177e4SLinus Torvalds 	switch (runtime->access) {
17411da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
17421da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
17431da177e4SLinus Torvalds 		info->first = info->channel * width;
17441da177e4SLinus Torvalds 		info->step = runtime->channels * width;
17451da177e4SLinus Torvalds 		break;
17461da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
17471da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
17481da177e4SLinus Torvalds 	{
17491da177e4SLinus Torvalds 		size_t size = runtime->dma_bytes / runtime->channels;
17501da177e4SLinus Torvalds 		info->first = info->channel * size * 8;
17511da177e4SLinus Torvalds 		info->step = width;
17521da177e4SLinus Torvalds 		break;
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 	default:
17551da177e4SLinus Torvalds 		snd_BUG();
17561da177e4SLinus Torvalds 		break;
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 	return 0;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream * substream,void * arg)17618bea869cSJaroslav Kysela static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
17628bea869cSJaroslav Kysela 				       void *arg)
17638bea869cSJaroslav Kysela {
17648bea869cSJaroslav Kysela 	struct snd_pcm_hw_params *params = arg;
17658bea869cSJaroslav Kysela 	snd_pcm_format_t format;
1766a9960e6aSClemens Ladisch 	int channels;
1767a9960e6aSClemens Ladisch 	ssize_t frame_size;
17688bea869cSJaroslav Kysela 
17698bea869cSJaroslav Kysela 	params->fifo_size = substream->runtime->hw.fifo_size;
17708bea869cSJaroslav Kysela 	if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
17718bea869cSJaroslav Kysela 		format = params_format(params);
17728bea869cSJaroslav Kysela 		channels = params_channels(params);
1773a9960e6aSClemens Ladisch 		frame_size = snd_pcm_format_size(format, channels);
1774a9960e6aSClemens Ladisch 		if (frame_size > 0)
1775f3eef46fSZubin Mithra 			params->fifo_size /= frame_size;
17768bea869cSJaroslav Kysela 	}
17778bea869cSJaroslav Kysela 	return 0;
17788bea869cSJaroslav Kysela }
17798bea869cSJaroslav Kysela 
17801da177e4SLinus Torvalds /**
17811da177e4SLinus Torvalds  * snd_pcm_lib_ioctl - a generic PCM ioctl callback
17821da177e4SLinus Torvalds  * @substream: the pcm substream instance
17831da177e4SLinus Torvalds  * @cmd: ioctl command
17841da177e4SLinus Torvalds  * @arg: ioctl argument
17851da177e4SLinus Torvalds  *
17861da177e4SLinus Torvalds  * Processes the generic ioctl commands for PCM.
17871da177e4SLinus Torvalds  * Can be passed as the ioctl callback for PCM ops.
17881da177e4SLinus Torvalds  *
1789eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
17901da177e4SLinus Torvalds  */
snd_pcm_lib_ioctl(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)1791877211f5STakashi Iwai int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
17921da177e4SLinus Torvalds 		      unsigned int cmd, void *arg)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds 	switch (cmd) {
17951da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
17961da177e4SLinus Torvalds 		return snd_pcm_lib_ioctl_reset(substream, arg);
17971da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
17981da177e4SLinus Torvalds 		return snd_pcm_lib_ioctl_channel_info(substream, arg);
17998bea869cSJaroslav Kysela 	case SNDRV_PCM_IOCTL1_FIFO_SIZE:
18008bea869cSJaroslav Kysela 		return snd_pcm_lib_ioctl_fifo_size(substream, arg);
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 	return -ENXIO;
18031da177e4SLinus Torvalds }
1804e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1805e88e8ae6STakashi Iwai 
18061da177e4SLinus Torvalds /**
180747271b1bSTakashi Sakamoto  * snd_pcm_period_elapsed_under_stream_lock() - update the status of runtime for the next period
180847271b1bSTakashi Sakamoto  *						under acquired lock of PCM substream.
180947271b1bSTakashi Sakamoto  * @substream: the instance of pcm substream.
18101da177e4SLinus Torvalds  *
181147271b1bSTakashi Sakamoto  * This function is called when the batch of audio data frames as the same size as the period of
181247271b1bSTakashi Sakamoto  * buffer is already processed in audio data transmission.
18131da177e4SLinus Torvalds  *
181447271b1bSTakashi Sakamoto  * The call of function updates the status of runtime with the latest position of audio data
181547271b1bSTakashi Sakamoto  * transmission, checks overrun and underrun over buffer, awaken user processes from waiting for
181647271b1bSTakashi Sakamoto  * available audio data frames, sampling audio timestamp, and performs stop or drain the PCM
181747271b1bSTakashi Sakamoto  * substream according to configured threshold.
181847271b1bSTakashi Sakamoto  *
181947271b1bSTakashi Sakamoto  * The function is intended to use for the case that PCM driver operates audio data frames under
182047271b1bSTakashi Sakamoto  * acquired lock of PCM substream; e.g. in callback of any operation of &snd_pcm_ops in process
182147271b1bSTakashi Sakamoto  * context. In any interrupt context, it's preferrable to use ``snd_pcm_period_elapsed()`` instead
182247271b1bSTakashi Sakamoto  * since lock of PCM substream should be acquired in advance.
182347271b1bSTakashi Sakamoto  *
182447271b1bSTakashi Sakamoto  * Developer should pay enough attention that some callbacks in &snd_pcm_ops are done by the call of
182547271b1bSTakashi Sakamoto  * function:
182647271b1bSTakashi Sakamoto  *
182747271b1bSTakashi Sakamoto  * - .pointer - to retrieve current position of audio data transmission by frame count or XRUN state.
182847271b1bSTakashi Sakamoto  * - .trigger - with SNDRV_PCM_TRIGGER_STOP at XRUN or DRAINING state.
182947271b1bSTakashi Sakamoto  * - .get_time_info - to retrieve audio time stamp if needed.
183047271b1bSTakashi Sakamoto  *
183147271b1bSTakashi Sakamoto  * Even if more than one periods have elapsed since the last call, you have to call this only once.
18321da177e4SLinus Torvalds  */
snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream * substream)183347271b1bSTakashi Sakamoto void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream)
18341da177e4SLinus Torvalds {
1835877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime;
18361da177e4SLinus Torvalds 
1837f5cdc9d4Spaulhsia 	if (PCM_RUNTIME_CHECK(substream))
183847271b1bSTakashi Sakamoto 		return;
1839f5cdc9d4Spaulhsia 	runtime = substream->runtime;
1840f5cdc9d4Spaulhsia 
18411da177e4SLinus Torvalds 	if (!snd_pcm_running(substream) ||
1842f240406bSJaroslav Kysela 	    snd_pcm_update_hw_ptr0(substream, 1) < 0)
18431da177e4SLinus Torvalds 		goto _end;
18441da177e4SLinus Torvalds 
184590bbaf66SJie Yang #ifdef CONFIG_SND_PCM_TIMER
18461da177e4SLinus Torvalds 	if (substream->timer_running)
18471da177e4SLinus Torvalds 		snd_timer_interrupt(substream->timer, 1);
184890bbaf66SJie Yang #endif
18491da177e4SLinus Torvalds  _end:
185096b09709STakashi Iwai 	snd_kill_fasync(runtime->fasync, SIGIO, POLL_IN);
185147271b1bSTakashi Sakamoto }
185247271b1bSTakashi Sakamoto EXPORT_SYMBOL(snd_pcm_period_elapsed_under_stream_lock);
185347271b1bSTakashi Sakamoto 
185447271b1bSTakashi Sakamoto /**
185547271b1bSTakashi Sakamoto  * snd_pcm_period_elapsed() - update the status of runtime for the next period by acquiring lock of
185647271b1bSTakashi Sakamoto  *			      PCM substream.
185747271b1bSTakashi Sakamoto  * @substream: the instance of PCM substream.
185847271b1bSTakashi Sakamoto  *
185947271b1bSTakashi Sakamoto  * This function is mostly similar to ``snd_pcm_period_elapsed_under_stream_lock()`` except for
186047271b1bSTakashi Sakamoto  * acquiring lock of PCM substream voluntarily.
186147271b1bSTakashi Sakamoto  *
186247271b1bSTakashi Sakamoto  * It's typically called by any type of IRQ handler when hardware IRQ occurs to notify event that
186347271b1bSTakashi Sakamoto  * the batch of audio data frames as the same size as the period of buffer is already processed in
186447271b1bSTakashi Sakamoto  * audio data transmission.
186547271b1bSTakashi Sakamoto  */
snd_pcm_period_elapsed(struct snd_pcm_substream * substream)186647271b1bSTakashi Sakamoto void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
186747271b1bSTakashi Sakamoto {
186847271b1bSTakashi Sakamoto 	unsigned long flags;
186947271b1bSTakashi Sakamoto 
187047271b1bSTakashi Sakamoto 	if (snd_BUG_ON(!substream))
187147271b1bSTakashi Sakamoto 		return;
187247271b1bSTakashi Sakamoto 
187347271b1bSTakashi Sakamoto 	snd_pcm_stream_lock_irqsave(substream, flags);
187447271b1bSTakashi Sakamoto 	snd_pcm_period_elapsed_under_stream_lock(substream);
18753aa02cb6STakashi Iwai 	snd_pcm_stream_unlock_irqrestore(substream, flags);
18761da177e4SLinus Torvalds }
1877e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_period_elapsed);
1878e88e8ae6STakashi Iwai 
187913075510STakashi Iwai /*
188013075510STakashi Iwai  * Wait until avail_min data becomes available
188113075510STakashi Iwai  * Returns a negative error code if any error occurs during operation.
188213075510STakashi Iwai  * The available space is stored on availp.  When err = 0 and avail = 0
188313075510STakashi Iwai  * on the capture stream, it indicates the stream is in DRAINING state.
188413075510STakashi Iwai  */
wait_for_avail(struct snd_pcm_substream * substream,snd_pcm_uframes_t * availp)18855daeba34SDavid Dillow static int wait_for_avail(struct snd_pcm_substream *substream,
188613075510STakashi Iwai 			      snd_pcm_uframes_t *availp)
188713075510STakashi Iwai {
188813075510STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
188913075510STakashi Iwai 	int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1890ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
189113075510STakashi Iwai 	int err = 0;
189213075510STakashi Iwai 	snd_pcm_uframes_t avail = 0;
1893f2b3614cSTakashi Iwai 	long wait_time, tout;
189413075510STakashi Iwai 
1895763437a9SArjan van de Ven 	init_waitqueue_entry(&wait, current);
1896763437a9SArjan van de Ven 	set_current_state(TASK_INTERRUPTIBLE);
1897763437a9SArjan van de Ven 	add_wait_queue(&runtime->tsleep, &wait);
1898763437a9SArjan van de Ven 
1899f2b3614cSTakashi Iwai 	if (runtime->no_period_wakeup)
1900f2b3614cSTakashi Iwai 		wait_time = MAX_SCHEDULE_TIMEOUT;
1901f2b3614cSTakashi Iwai 	else {
1902d64c5cf8SLiam Girdwood 		/* use wait time from substream if available */
1903d64c5cf8SLiam Girdwood 		if (substream->wait_time) {
1904d64c5cf8SLiam Girdwood 			wait_time = substream->wait_time;
1905d64c5cf8SLiam Girdwood 		} else {
19063ed2b549SOswald Buddenhagen 			wait_time = 100;
1907d64c5cf8SLiam Girdwood 
1908f2b3614cSTakashi Iwai 			if (runtime->rate) {
19093ed2b549SOswald Buddenhagen 				long t = runtime->buffer_size * 1100 / runtime->rate;
1910f2b3614cSTakashi Iwai 				wait_time = max(t, wait_time);
1911f2b3614cSTakashi Iwai 			}
1912f2b3614cSTakashi Iwai 		}
19133ed2b549SOswald Buddenhagen 		wait_time = msecs_to_jiffies(wait_time);
1914d64c5cf8SLiam Girdwood 	}
1915763437a9SArjan van de Ven 
191613075510STakashi Iwai 	for (;;) {
191713075510STakashi Iwai 		if (signal_pending(current)) {
191813075510STakashi Iwai 			err = -ERESTARTSYS;
191913075510STakashi Iwai 			break;
192013075510STakashi Iwai 		}
1921763437a9SArjan van de Ven 
1922763437a9SArjan van de Ven 		/*
1923763437a9SArjan van de Ven 		 * We need to check if space became available already
1924763437a9SArjan van de Ven 		 * (and thus the wakeup happened already) first to close
1925763437a9SArjan van de Ven 		 * the race of space already having become available.
1926763437a9SArjan van de Ven 		 * This check must happen after been added to the waitqueue
1927763437a9SArjan van de Ven 		 * and having current state be INTERRUPTIBLE.
1928763437a9SArjan van de Ven 		 */
1929763e5067STakashi Iwai 		avail = snd_pcm_avail(substream);
1930763437a9SArjan van de Ven 		if (avail >= runtime->twake)
1931763437a9SArjan van de Ven 			break;
193213075510STakashi Iwai 		snd_pcm_stream_unlock_irq(substream);
1933763437a9SArjan van de Ven 
1934763437a9SArjan van de Ven 		tout = schedule_timeout(wait_time);
1935763437a9SArjan van de Ven 
193613075510STakashi Iwai 		snd_pcm_stream_lock_irq(substream);
1937763437a9SArjan van de Ven 		set_current_state(TASK_INTERRUPTIBLE);
1938f0061c18STakashi Iwai 		switch (runtime->state) {
193913075510STakashi Iwai 		case SNDRV_PCM_STATE_SUSPENDED:
194013075510STakashi Iwai 			err = -ESTRPIPE;
194113075510STakashi Iwai 			goto _endloop;
194213075510STakashi Iwai 		case SNDRV_PCM_STATE_XRUN:
194313075510STakashi Iwai 			err = -EPIPE;
194413075510STakashi Iwai 			goto _endloop;
194513075510STakashi Iwai 		case SNDRV_PCM_STATE_DRAINING:
194613075510STakashi Iwai 			if (is_playback)
194713075510STakashi Iwai 				err = -EPIPE;
194813075510STakashi Iwai 			else
194913075510STakashi Iwai 				avail = 0; /* indicate draining */
195013075510STakashi Iwai 			goto _endloop;
195113075510STakashi Iwai 		case SNDRV_PCM_STATE_OPEN:
195213075510STakashi Iwai 		case SNDRV_PCM_STATE_SETUP:
195313075510STakashi Iwai 		case SNDRV_PCM_STATE_DISCONNECTED:
195413075510STakashi Iwai 			err = -EBADFD;
195513075510STakashi Iwai 			goto _endloop;
1956ed697e1aSJongHo Kim 		case SNDRV_PCM_STATE_PAUSED:
1957ed697e1aSJongHo Kim 			continue;
195813075510STakashi Iwai 		}
195913075510STakashi Iwai 		if (!tout) {
196009e56df8STakashi Iwai 			pcm_dbg(substream->pcm,
19613ed2b549SOswald Buddenhagen 				"%s timeout (DMA or IRQ trouble?)\n",
19623ed2b549SOswald Buddenhagen 				is_playback ? "playback write" : "capture read");
196313075510STakashi Iwai 			err = -EIO;
196413075510STakashi Iwai 			break;
196513075510STakashi Iwai 		}
196613075510STakashi Iwai 	}
196713075510STakashi Iwai  _endloop:
1968763437a9SArjan van de Ven 	set_current_state(TASK_RUNNING);
1969c91a988dSJaroslav Kysela 	remove_wait_queue(&runtime->tsleep, &wait);
197013075510STakashi Iwai 	*availp = avail;
197113075510STakashi Iwai 	return err;
197213075510STakashi Iwai }
197313075510STakashi Iwai 
19749f600630STakashi Iwai typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
19759f600630STakashi Iwai 			      int channel, unsigned long hwoff,
1976cf393babSTakashi Iwai 			      struct iov_iter *iter, unsigned long bytes);
1977bdc4acf7STakashi Iwai 
19789f600630STakashi Iwai typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1979cf393babSTakashi Iwai 			  snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f,
1980cf393babSTakashi Iwai 			  bool);
19819f600630STakashi Iwai 
19829f600630STakashi Iwai /* calculate the target DMA-buffer position to be written/read */
get_dma_ptr(struct snd_pcm_runtime * runtime,int channel,unsigned long hwoff)19839f600630STakashi Iwai static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
19849f600630STakashi Iwai 			   int channel, unsigned long hwoff)
19851da177e4SLinus Torvalds {
19869f600630STakashi Iwai 	return runtime->dma_area + hwoff +
19879f600630STakashi Iwai 		channel * (runtime->dma_bytes / runtime->channels);
19881da177e4SLinus Torvalds }
19899f600630STakashi Iwai 
1990cf393babSTakashi Iwai /* default copy ops for write; used for both interleaved and non- modes */
default_write_copy(struct snd_pcm_substream * substream,int channel,unsigned long hwoff,struct iov_iter * iter,unsigned long bytes)19915c7264cfSTakashi Iwai static int default_write_copy(struct snd_pcm_substream *substream,
19929f600630STakashi Iwai 			      int channel, unsigned long hwoff,
1993cf393babSTakashi Iwai 			      struct iov_iter *iter, unsigned long bytes)
19949f600630STakashi Iwai {
1995*e14ebde5STakashi Iwai 	if (copy_from_iter(get_dma_ptr(substream->runtime, channel, hwoff),
1996*e14ebde5STakashi Iwai 			   bytes, iter) != bytes)
19971da177e4SLinus Torvalds 		return -EFAULT;
19981da177e4SLinus Torvalds 	return 0;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds 
20019f600630STakashi Iwai /* fill silence instead of copy data; called as a transfer helper
20029f600630STakashi Iwai  * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
20039f600630STakashi Iwai  * a NULL buffer is passed
20049f600630STakashi Iwai  */
fill_silence(struct snd_pcm_substream * substream,int channel,unsigned long hwoff,struct iov_iter * iter,unsigned long bytes)20059f600630STakashi Iwai static int fill_silence(struct snd_pcm_substream *substream, int channel,
2006cf393babSTakashi Iwai 			unsigned long hwoff, struct iov_iter *iter,
2007cf393babSTakashi Iwai 			unsigned long bytes)
20081da177e4SLinus Torvalds {
2009877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
20101da177e4SLinus Torvalds 
20119f600630STakashi Iwai 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
20121da177e4SLinus Torvalds 		return 0;
20139f600630STakashi Iwai 	if (substream->ops->fill_silence)
20149f600630STakashi Iwai 		return substream->ops->fill_silence(substream, channel,
20159f600630STakashi Iwai 						    hwoff, bytes);
20161da177e4SLinus Torvalds 
20179f600630STakashi Iwai 	snd_pcm_format_set_silence(runtime->format,
20189f600630STakashi Iwai 				   get_dma_ptr(runtime, channel, hwoff),
20199f600630STakashi Iwai 				   bytes_to_samples(runtime, bytes));
20209f600630STakashi Iwai 	return 0;
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
2023cf393babSTakashi Iwai /* default copy ops for read; used for both interleaved and non- modes */
default_read_copy(struct snd_pcm_substream * substream,int channel,unsigned long hwoff,struct iov_iter * iter,unsigned long bytes)20245c7264cfSTakashi Iwai static int default_read_copy(struct snd_pcm_substream *substream,
20255c7264cfSTakashi Iwai 			     int channel, unsigned long hwoff,
2026cf393babSTakashi Iwai 			     struct iov_iter *iter, unsigned long bytes)
20275c7264cfSTakashi Iwai {
2028*e14ebde5STakashi Iwai 	if (copy_to_iter(get_dma_ptr(substream->runtime, channel, hwoff),
2029*e14ebde5STakashi Iwai 			 bytes, iter) != bytes)
20305c7264cfSTakashi Iwai 		return -EFAULT;
20315c7264cfSTakashi Iwai 	return 0;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034cf393babSTakashi Iwai /* call transfer with the filled iov_iter */
do_transfer(struct snd_pcm_substream * substream,int c,unsigned long hwoff,void * data,unsigned long bytes,pcm_transfer_f transfer,bool in_kernel)2035cf393babSTakashi Iwai static int do_transfer(struct snd_pcm_substream *substream, int c,
2036cf393babSTakashi Iwai 		       unsigned long hwoff, void *data, unsigned long bytes,
2037cf393babSTakashi Iwai 		       pcm_transfer_f transfer, bool in_kernel)
2038cf393babSTakashi Iwai {
2039cf393babSTakashi Iwai 	struct iov_iter iter;
2040cf393babSTakashi Iwai 	int err, type;
2041cf393babSTakashi Iwai 
2042cf393babSTakashi Iwai 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2043cf393babSTakashi Iwai 		type = ITER_SOURCE;
2044cf393babSTakashi Iwai 	else
2045cf393babSTakashi Iwai 		type = ITER_DEST;
2046cf393babSTakashi Iwai 
2047cf393babSTakashi Iwai 	if (in_kernel) {
2048cf393babSTakashi Iwai 		struct kvec kvec = { data, bytes };
2049cf393babSTakashi Iwai 
2050cf393babSTakashi Iwai 		iov_iter_kvec(&iter, type, &kvec, 1, bytes);
2051cf393babSTakashi Iwai 		return transfer(substream, c, hwoff, &iter, bytes);
2052cf393babSTakashi Iwai 	}
2053cf393babSTakashi Iwai 
2054cf393babSTakashi Iwai 	err = import_ubuf(type, (__force void __user *)data, bytes, &iter);
2055cf393babSTakashi Iwai 	if (err)
2056cf393babSTakashi Iwai 		return err;
2057cf393babSTakashi Iwai 	return transfer(substream, c, hwoff, &iter, bytes);
205868541213STakashi Iwai }
205968541213STakashi Iwai 
20609f600630STakashi Iwai /* call transfer function with the converted pointers and sizes;
20619f600630STakashi Iwai  * for interleaved mode, it's one shot for all samples
20629f600630STakashi Iwai  */
interleaved_copy(struct snd_pcm_substream * substream,snd_pcm_uframes_t hwoff,void * data,snd_pcm_uframes_t off,snd_pcm_uframes_t frames,pcm_transfer_f transfer,bool in_kernel)20639f600630STakashi Iwai static int interleaved_copy(struct snd_pcm_substream *substream,
20649f600630STakashi Iwai 			    snd_pcm_uframes_t hwoff, void *data,
20659f600630STakashi Iwai 			    snd_pcm_uframes_t off,
20669f600630STakashi Iwai 			    snd_pcm_uframes_t frames,
2067cf393babSTakashi Iwai 			    pcm_transfer_f transfer,
2068cf393babSTakashi Iwai 			    bool in_kernel)
20699f600630STakashi Iwai {
20709f600630STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
20719f600630STakashi Iwai 
20729f600630STakashi Iwai 	/* convert to bytes */
20739f600630STakashi Iwai 	hwoff = frames_to_bytes(runtime, hwoff);
20749f600630STakashi Iwai 	off = frames_to_bytes(runtime, off);
20759f600630STakashi Iwai 	frames = frames_to_bytes(runtime, frames);
2076cf393babSTakashi Iwai 
2077cf393babSTakashi Iwai 	return do_transfer(substream, 0, hwoff, data + off, frames, transfer,
2078cf393babSTakashi Iwai 			   in_kernel);
20799f600630STakashi Iwai }
20809f600630STakashi Iwai 
20819f600630STakashi Iwai /* call transfer function with the converted pointers and sizes for each
20829f600630STakashi Iwai  * non-interleaved channel; when buffer is NULL, silencing instead of copying
20839f600630STakashi Iwai  */
noninterleaved_copy(struct snd_pcm_substream * substream,snd_pcm_uframes_t hwoff,void * data,snd_pcm_uframes_t off,snd_pcm_uframes_t frames,pcm_transfer_f transfer,bool in_kernel)20849f600630STakashi Iwai static int noninterleaved_copy(struct snd_pcm_substream *substream,
20859f600630STakashi Iwai 			       snd_pcm_uframes_t hwoff, void *data,
20869f600630STakashi Iwai 			       snd_pcm_uframes_t off,
20879f600630STakashi Iwai 			       snd_pcm_uframes_t frames,
2088cf393babSTakashi Iwai 			       pcm_transfer_f transfer,
2089cf393babSTakashi Iwai 			       bool in_kernel)
20909f600630STakashi Iwai {
20919f600630STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
20929f600630STakashi Iwai 	int channels = runtime->channels;
20939f600630STakashi Iwai 	void **bufs = data;
20949f600630STakashi Iwai 	int c, err;
20959f600630STakashi Iwai 
20969f600630STakashi Iwai 	/* convert to bytes; note that it's not frames_to_bytes() here.
20979f600630STakashi Iwai 	 * in non-interleaved mode, we copy for each channel, thus
20989f600630STakashi Iwai 	 * each copy is n_samples bytes x channels = whole frames.
20999f600630STakashi Iwai 	 */
2100bdc4acf7STakashi Iwai 	off = samples_to_bytes(runtime, off);
2101bdc4acf7STakashi Iwai 	frames = samples_to_bytes(runtime, frames);
21029f600630STakashi Iwai 	hwoff = samples_to_bytes(runtime, hwoff);
2103bdc4acf7STakashi Iwai 	for (c = 0; c < channels; ++c, ++bufs) {
21049f600630STakashi Iwai 		if (!data || !*bufs)
21059f600630STakashi Iwai 			err = fill_silence(substream, c, hwoff, NULL, frames);
21069f600630STakashi Iwai 		else
2107cf393babSTakashi Iwai 			err = do_transfer(substream, c, hwoff, *bufs + off,
2108cf393babSTakashi Iwai 					  frames, transfer, in_kernel);
21091da177e4SLinus Torvalds 		if (err < 0)
2110bdc4acf7STakashi Iwai 			return err;
21111da177e4SLinus Torvalds 	}
2112bdc4acf7STakashi Iwai 	return 0;
21131da177e4SLinus Torvalds }
2114bdc4acf7STakashi Iwai 
2115a9cd29e7STakashi Iwai /* fill silence on the given buffer position;
2116a9cd29e7STakashi Iwai  * called from snd_pcm_playback_silence()
2117a9cd29e7STakashi Iwai  */
fill_silence_frames(struct snd_pcm_substream * substream,snd_pcm_uframes_t off,snd_pcm_uframes_t frames)2118a9cd29e7STakashi Iwai static int fill_silence_frames(struct snd_pcm_substream *substream,
2119a9cd29e7STakashi Iwai 			       snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2120a9cd29e7STakashi Iwai {
2121a9cd29e7STakashi Iwai 	if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2122a9cd29e7STakashi Iwai 	    substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2123a9cd29e7STakashi Iwai 		return interleaved_copy(substream, off, NULL, 0, frames,
2124cf393babSTakashi Iwai 					fill_silence, true);
2125a9cd29e7STakashi Iwai 	else
2126a9cd29e7STakashi Iwai 		return noninterleaved_copy(substream, off, NULL, 0, frames,
2127cf393babSTakashi Iwai 					   fill_silence, true);
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds 
21307eaa943cSTakashi Iwai /* sanity-check for read/write methods */
pcm_sanity_check(struct snd_pcm_substream * substream)21317eaa943cSTakashi Iwai static int pcm_sanity_check(struct snd_pcm_substream *substream)
21327eaa943cSTakashi Iwai {
21337eaa943cSTakashi Iwai 	struct snd_pcm_runtime *runtime;
21347eaa943cSTakashi Iwai 	if (PCM_RUNTIME_CHECK(substream))
21357eaa943cSTakashi Iwai 		return -ENXIO;
21367eaa943cSTakashi Iwai 	runtime = substream->runtime;
21376c0217b1STakashi Iwai 	if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
21387eaa943cSTakashi Iwai 		return -EINVAL;
2139f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_OPEN)
21407eaa943cSTakashi Iwai 		return -EBADFD;
21417eaa943cSTakashi Iwai 	return 0;
21427eaa943cSTakashi Iwai }
21437eaa943cSTakashi Iwai 
pcm_accessible_state(struct snd_pcm_runtime * runtime)21446ba63929STakashi Iwai static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
21451da177e4SLinus Torvalds {
2146f0061c18STakashi Iwai 	switch (runtime->state) {
21476ba63929STakashi Iwai 	case SNDRV_PCM_STATE_PREPARED:
21486ba63929STakashi Iwai 	case SNDRV_PCM_STATE_RUNNING:
21496ba63929STakashi Iwai 	case SNDRV_PCM_STATE_PAUSED:
21506ba63929STakashi Iwai 		return 0;
21516ba63929STakashi Iwai 	case SNDRV_PCM_STATE_XRUN:
21526ba63929STakashi Iwai 		return -EPIPE;
21536ba63929STakashi Iwai 	case SNDRV_PCM_STATE_SUSPENDED:
21546ba63929STakashi Iwai 		return -ESTRPIPE;
21556ba63929STakashi Iwai 	default:
21566ba63929STakashi Iwai 		return -EBADFD;
21576ba63929STakashi Iwai 	}
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
216066e01a5cSTakashi Sakamoto /* update to the given appl_ptr and call ack callback if needed;
216166e01a5cSTakashi Sakamoto  * when an error is returned, take back to the original value
216266e01a5cSTakashi Sakamoto  */
pcm_lib_apply_appl_ptr(struct snd_pcm_substream * substream,snd_pcm_uframes_t appl_ptr)216366e01a5cSTakashi Sakamoto int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
216466e01a5cSTakashi Sakamoto 			   snd_pcm_uframes_t appl_ptr)
21651da177e4SLinus Torvalds {
2166877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
216766e01a5cSTakashi Sakamoto 	snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2168b456abe6SPierre-Louis Bossart 	snd_pcm_sframes_t diff;
216966e01a5cSTakashi Sakamoto 	int ret;
217066e01a5cSTakashi Sakamoto 
2171f8ff2f28STakashi Iwai 	if (old_appl_ptr == appl_ptr)
2172f8ff2f28STakashi Iwai 		return 0;
2173f8ff2f28STakashi Iwai 
21740e888a74SPierre-Louis Bossart 	if (appl_ptr >= runtime->boundary)
21750e888a74SPierre-Louis Bossart 		return -EINVAL;
2176b456abe6SPierre-Louis Bossart 	/*
2177b456abe6SPierre-Louis Bossart 	 * check if a rewind is requested by the application
2178b456abe6SPierre-Louis Bossart 	 */
2179b456abe6SPierre-Louis Bossart 	if (substream->runtime->info & SNDRV_PCM_INFO_NO_REWINDS) {
2180b456abe6SPierre-Louis Bossart 		diff = appl_ptr - old_appl_ptr;
2181b456abe6SPierre-Louis Bossart 		if (diff >= 0) {
2182b456abe6SPierre-Louis Bossart 			if (diff > runtime->buffer_size)
2183b456abe6SPierre-Louis Bossart 				return -EINVAL;
2184b456abe6SPierre-Louis Bossart 		} else {
2185b456abe6SPierre-Louis Bossart 			if (runtime->boundary + diff > runtime->buffer_size)
2186b456abe6SPierre-Louis Bossart 				return -EINVAL;
2187b456abe6SPierre-Louis Bossart 		}
2188b456abe6SPierre-Louis Bossart 	}
21890e888a74SPierre-Louis Bossart 
219066e01a5cSTakashi Sakamoto 	runtime->control->appl_ptr = appl_ptr;
219166e01a5cSTakashi Sakamoto 	if (substream->ops->ack) {
219266e01a5cSTakashi Sakamoto 		ret = substream->ops->ack(substream);
219366e01a5cSTakashi Sakamoto 		if (ret < 0) {
219466e01a5cSTakashi Sakamoto 			runtime->control->appl_ptr = old_appl_ptr;
21958c721c53STakashi Iwai 			if (ret == -EPIPE)
21968c721c53STakashi Iwai 				__snd_pcm_xrun(substream);
219766e01a5cSTakashi Sakamoto 			return ret;
21981da177e4SLinus Torvalds 		}
21991da177e4SLinus Torvalds 	}
2200fccf5388STakashi Sakamoto 
2201fccf5388STakashi Sakamoto 	trace_applptr(substream, old_appl_ptr, appl_ptr);
2202fccf5388STakashi Sakamoto 
22031da177e4SLinus Torvalds 	return 0;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22065c7264cfSTakashi Iwai /* the common loop for read/write data */
__snd_pcm_lib_xfer(struct snd_pcm_substream * substream,void * data,bool interleaved,snd_pcm_uframes_t size,bool in_kernel)22075c7264cfSTakashi Iwai snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2208c48f12eeSTakashi Iwai 				     void *data, bool interleaved,
220968541213STakashi Iwai 				     snd_pcm_uframes_t size, bool in_kernel)
22101da177e4SLinus Torvalds {
2211877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
22121da177e4SLinus Torvalds 	snd_pcm_uframes_t xfer = 0;
22131da177e4SLinus Torvalds 	snd_pcm_uframes_t offset = 0;
22140910c216STakashi Iwai 	snd_pcm_uframes_t avail;
22159f600630STakashi Iwai 	pcm_copy_f writer;
22169f600630STakashi Iwai 	pcm_transfer_f transfer;
2217c48f12eeSTakashi Iwai 	bool nonblock;
22185c7264cfSTakashi Iwai 	bool is_playback;
2219c48f12eeSTakashi Iwai 	int err;
2220c48f12eeSTakashi Iwai 
2221c48f12eeSTakashi Iwai 	err = pcm_sanity_check(substream);
2222c48f12eeSTakashi Iwai 	if (err < 0)
2223c48f12eeSTakashi Iwai 		return err;
2224c48f12eeSTakashi Iwai 
22255c7264cfSTakashi Iwai 	is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2226c48f12eeSTakashi Iwai 	if (interleaved) {
2227c48f12eeSTakashi Iwai 		if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2228c48f12eeSTakashi Iwai 		    runtime->channels > 1)
2229c48f12eeSTakashi Iwai 			return -EINVAL;
22309f600630STakashi Iwai 		writer = interleaved_copy;
2231c48f12eeSTakashi Iwai 	} else {
2232c48f12eeSTakashi Iwai 		if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2233c48f12eeSTakashi Iwai 			return -EINVAL;
22349f600630STakashi Iwai 		writer = noninterleaved_copy;
22359f600630STakashi Iwai 	}
22369f600630STakashi Iwai 
22379f600630STakashi Iwai 	if (!data) {
22385c7264cfSTakashi Iwai 		if (is_playback)
22399f600630STakashi Iwai 			transfer = fill_silence;
22405c7264cfSTakashi Iwai 		else
22415c7264cfSTakashi Iwai 			return -EINVAL;
22429f600630STakashi Iwai 	} else {
2243cf393babSTakashi Iwai 		if (substream->ops->copy)
2244cf393babSTakashi Iwai 			transfer = substream->ops->copy;
22459f600630STakashi Iwai 		else
22465c7264cfSTakashi Iwai 			transfer = is_playback ?
22475c7264cfSTakashi Iwai 				default_write_copy : default_read_copy;
2248c48f12eeSTakashi Iwai 	}
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	if (size == 0)
22511da177e4SLinus Torvalds 		return 0;
22521da177e4SLinus Torvalds 
2253c48f12eeSTakashi Iwai 	nonblock = !!(substream->f_flags & O_NONBLOCK);
2254c48f12eeSTakashi Iwai 
22551da177e4SLinus Torvalds 	snd_pcm_stream_lock_irq(substream);
22566ba63929STakashi Iwai 	err = pcm_accessible_state(runtime);
22571da177e4SLinus Torvalds 	if (err < 0)
22581da177e4SLinus Torvalds 		goto _end_unlock;
22591da177e4SLinus Torvalds 
226064b6acf6SRicardo Biehl Pasquali 	runtime->twake = runtime->control->avail_min ? : 1;
2261f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_RUNNING)
226264b6acf6SRicardo Biehl Pasquali 		snd_pcm_update_hw_ptr(substream);
226364b6acf6SRicardo Biehl Pasquali 
2264932a8151SRicardo Biehl Pasquali 	/*
2265932a8151SRicardo Biehl Pasquali 	 * If size < start_threshold, wait indefinitely. Another
2266932a8151SRicardo Biehl Pasquali 	 * thread may start capture
2267932a8151SRicardo Biehl Pasquali 	 */
22685c7264cfSTakashi Iwai 	if (!is_playback &&
2269f0061c18STakashi Iwai 	    runtime->state == SNDRV_PCM_STATE_PREPARED &&
227000a399caSTakashi Iwai 	    size >= runtime->start_threshold) {
22715c7264cfSTakashi Iwai 		err = snd_pcm_start(substream);
22725c7264cfSTakashi Iwai 		if (err < 0)
22731da177e4SLinus Torvalds 			goto _end_unlock;
22741da177e4SLinus Torvalds 	}
22751da177e4SLinus Torvalds 
2276763e5067STakashi Iwai 	avail = snd_pcm_avail(substream);
227764b6acf6SRicardo Biehl Pasquali 
22780910c216STakashi Iwai 	while (size > 0) {
22790910c216STakashi Iwai 		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
22800910c216STakashi Iwai 		snd_pcm_uframes_t cont;
2281d948035aSTakashi Iwai 		if (!avail) {
22825c7264cfSTakashi Iwai 			if (!is_playback &&
2283f0061c18STakashi Iwai 			    runtime->state == SNDRV_PCM_STATE_DRAINING) {
228413075510STakashi Iwai 				snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
22851da177e4SLinus Torvalds 				goto _end_unlock;
22861da177e4SLinus Torvalds 			}
22871da177e4SLinus Torvalds 			if (nonblock) {
22881da177e4SLinus Torvalds 				err = -EAGAIN;
22891da177e4SLinus Torvalds 				goto _end_unlock;
22901da177e4SLinus Torvalds 			}
22915daeba34SDavid Dillow 			runtime->twake = min_t(snd_pcm_uframes_t, size,
22925daeba34SDavid Dillow 					runtime->control->avail_min ? : 1);
22935daeba34SDavid Dillow 			err = wait_for_avail(substream, &avail);
229413075510STakashi Iwai 			if (err < 0)
22951da177e4SLinus Torvalds 				goto _end_unlock;
229613075510STakashi Iwai 			if (!avail)
229713075510STakashi Iwai 				continue; /* draining */
22981da177e4SLinus Torvalds 		}
22991da177e4SLinus Torvalds 		frames = size > avail ? avail : size;
2300aa30db06STakashi Iwai 		appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2301aa30db06STakashi Iwai 		appl_ofs = appl_ptr % runtime->buffer_size;
2302aa30db06STakashi Iwai 		cont = runtime->buffer_size - appl_ofs;
23031da177e4SLinus Torvalds 		if (frames > cont)
23041da177e4SLinus Torvalds 			frames = cont;
23057eaa943cSTakashi Iwai 		if (snd_BUG_ON(!frames)) {
2306315d9f1bSTakashi Iwai 			err = -EINVAL;
2307315d9f1bSTakashi Iwai 			goto _end_unlock;
23087eaa943cSTakashi Iwai 		}
2309bc55cfd5STakashi Iwai 		if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
2310bc55cfd5STakashi Iwai 			err = -EBUSY;
2311bc55cfd5STakashi Iwai 			goto _end_unlock;
2312bc55cfd5STakashi Iwai 		}
23131da177e4SLinus Torvalds 		snd_pcm_stream_unlock_irq(substream);
2314a25684a9STakashi Iwai 		if (!is_playback)
2315a25684a9STakashi Iwai 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
23169f600630STakashi Iwai 		err = writer(substream, appl_ofs, data, offset, frames,
2317cf393babSTakashi Iwai 			     transfer, in_kernel);
2318a25684a9STakashi Iwai 		if (is_playback)
2319a25684a9STakashi Iwai 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
23201da177e4SLinus Torvalds 		snd_pcm_stream_lock_irq(substream);
2321bc55cfd5STakashi Iwai 		atomic_dec(&runtime->buffer_accessing);
23221250932eSJaroslav Kysela 		if (err < 0)
23231250932eSJaroslav Kysela 			goto _end_unlock;
23246ba63929STakashi Iwai 		err = pcm_accessible_state(runtime);
23256ba63929STakashi Iwai 		if (err < 0)
23261da177e4SLinus Torvalds 			goto _end_unlock;
23271da177e4SLinus Torvalds 		appl_ptr += frames;
23281da177e4SLinus Torvalds 		if (appl_ptr >= runtime->boundary)
23291da177e4SLinus Torvalds 			appl_ptr -= runtime->boundary;
233066e01a5cSTakashi Sakamoto 		err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
233166e01a5cSTakashi Sakamoto 		if (err < 0)
233266e01a5cSTakashi Sakamoto 			goto _end_unlock;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 		offset += frames;
23351da177e4SLinus Torvalds 		size -= frames;
23361da177e4SLinus Torvalds 		xfer += frames;
23370910c216STakashi Iwai 		avail -= frames;
23385c7264cfSTakashi Iwai 		if (is_playback &&
2339f0061c18STakashi Iwai 		    runtime->state == SNDRV_PCM_STATE_PREPARED &&
23401da177e4SLinus Torvalds 		    snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
23411da177e4SLinus Torvalds 			err = snd_pcm_start(substream);
23421da177e4SLinus Torvalds 			if (err < 0)
23431da177e4SLinus Torvalds 				goto _end_unlock;
23441da177e4SLinus Torvalds 		}
23451da177e4SLinus Torvalds 	}
23461da177e4SLinus Torvalds  _end_unlock:
2347c91a988dSJaroslav Kysela 	runtime->twake = 0;
23481250932eSJaroslav Kysela 	if (xfer > 0 && err >= 0)
23491250932eSJaroslav Kysela 		snd_pcm_update_state(substream, runtime);
23501da177e4SLinus Torvalds 	snd_pcm_stream_unlock_irq(substream);
23511da177e4SLinus Torvalds 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
23521da177e4SLinus Torvalds }
23535c7264cfSTakashi Iwai EXPORT_SYMBOL(__snd_pcm_lib_xfer);
23542d3391ecSTakashi Iwai 
23552d3391ecSTakashi Iwai /*
23562d3391ecSTakashi Iwai  * standard channel mapping helpers
23572d3391ecSTakashi Iwai  */
23582d3391ecSTakashi Iwai 
23592d3391ecSTakashi Iwai /* default channel maps for multi-channel playbacks, up to 8 channels */
23602d3391ecSTakashi Iwai const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
23612d3391ecSTakashi Iwai 	{ .channels = 1,
23625efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
23632d3391ecSTakashi Iwai 	{ .channels = 2,
23642d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
23652d3391ecSTakashi Iwai 	{ .channels = 4,
23662d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23672d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
23682d3391ecSTakashi Iwai 	{ .channels = 6,
23692d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23702d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
23712d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
23722d3391ecSTakashi Iwai 	{ .channels = 8,
23732d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23742d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
23752d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
23762d3391ecSTakashi Iwai 		   SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
23772d3391ecSTakashi Iwai 	{ }
23782d3391ecSTakashi Iwai };
23792d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
23802d3391ecSTakashi Iwai 
23812d3391ecSTakashi Iwai /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
23822d3391ecSTakashi Iwai const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
23832d3391ecSTakashi Iwai 	{ .channels = 1,
23845efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
23852d3391ecSTakashi Iwai 	{ .channels = 2,
23862d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
23872d3391ecSTakashi Iwai 	{ .channels = 4,
23882d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23892d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
23902d3391ecSTakashi Iwai 	{ .channels = 6,
23912d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23922d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
23932d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
23942d3391ecSTakashi Iwai 	{ .channels = 8,
23952d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23962d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
23972d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
23982d3391ecSTakashi Iwai 		   SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
23992d3391ecSTakashi Iwai 	{ }
24002d3391ecSTakashi Iwai };
24012d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
24022d3391ecSTakashi Iwai 
valid_chmap_channels(const struct snd_pcm_chmap * info,int ch)24032d3391ecSTakashi Iwai static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
24042d3391ecSTakashi Iwai {
24052d3391ecSTakashi Iwai 	if (ch > info->max_channels)
24062d3391ecSTakashi Iwai 		return false;
24072d3391ecSTakashi Iwai 	return !info->channel_mask || (info->channel_mask & (1U << ch));
24082d3391ecSTakashi Iwai }
24092d3391ecSTakashi Iwai 
pcm_chmap_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)24102d3391ecSTakashi Iwai static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
24112d3391ecSTakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
24122d3391ecSTakashi Iwai {
24132d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24142d3391ecSTakashi Iwai 
24152d3391ecSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
24162d3391ecSTakashi Iwai 	uinfo->count = info->max_channels;
24172d3391ecSTakashi Iwai 	uinfo->value.integer.min = 0;
24182d3391ecSTakashi Iwai 	uinfo->value.integer.max = SNDRV_CHMAP_LAST;
24192d3391ecSTakashi Iwai 	return 0;
24202d3391ecSTakashi Iwai }
24212d3391ecSTakashi Iwai 
24222d3391ecSTakashi Iwai /* get callback for channel map ctl element
24232d3391ecSTakashi Iwai  * stores the channel position firstly matching with the current channels
24242d3391ecSTakashi Iwai  */
pcm_chmap_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)24252d3391ecSTakashi Iwai static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
24262d3391ecSTakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
24272d3391ecSTakashi Iwai {
24282d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24292d3391ecSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
24302d3391ecSTakashi Iwai 	struct snd_pcm_substream *substream;
24312d3391ecSTakashi Iwai 	const struct snd_pcm_chmap_elem *map;
24322d3391ecSTakashi Iwai 
24332deaeaf1STakashi Iwai 	if (!info->chmap)
24342d3391ecSTakashi Iwai 		return -EINVAL;
24352d3391ecSTakashi Iwai 	substream = snd_pcm_chmap_substream(info, idx);
24362d3391ecSTakashi Iwai 	if (!substream)
24372d3391ecSTakashi Iwai 		return -ENODEV;
24382d3391ecSTakashi Iwai 	memset(ucontrol->value.integer.value, 0,
2439fbd3eb7fSTakashi Iwai 	       sizeof(long) * info->max_channels);
24402d3391ecSTakashi Iwai 	if (!substream->runtime)
24412d3391ecSTakashi Iwai 		return 0; /* no channels set */
24422d3391ecSTakashi Iwai 	for (map = info->chmap; map->channels; map++) {
24432d3391ecSTakashi Iwai 		int i;
24442d3391ecSTakashi Iwai 		if (map->channels == substream->runtime->channels &&
24452d3391ecSTakashi Iwai 		    valid_chmap_channels(info, map->channels)) {
24462d3391ecSTakashi Iwai 			for (i = 0; i < map->channels; i++)
24472d3391ecSTakashi Iwai 				ucontrol->value.integer.value[i] = map->map[i];
24482d3391ecSTakashi Iwai 			return 0;
24492d3391ecSTakashi Iwai 		}
24502d3391ecSTakashi Iwai 	}
24512d3391ecSTakashi Iwai 	return -EINVAL;
24522d3391ecSTakashi Iwai }
24532d3391ecSTakashi Iwai 
24542d3391ecSTakashi Iwai /* tlv callback for channel map ctl element
24552d3391ecSTakashi Iwai  * expands the pre-defined channel maps in a form of TLV
24562d3391ecSTakashi Iwai  */
pcm_chmap_ctl_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)24572d3391ecSTakashi Iwai static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
24582d3391ecSTakashi Iwai 			     unsigned int size, unsigned int __user *tlv)
24592d3391ecSTakashi Iwai {
24602d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24612d3391ecSTakashi Iwai 	const struct snd_pcm_chmap_elem *map;
24622d3391ecSTakashi Iwai 	unsigned int __user *dst;
24632d3391ecSTakashi Iwai 	int c, count = 0;
24642d3391ecSTakashi Iwai 
24652deaeaf1STakashi Iwai 	if (!info->chmap)
24662d3391ecSTakashi Iwai 		return -EINVAL;
24672d3391ecSTakashi Iwai 	if (size < 8)
24682d3391ecSTakashi Iwai 		return -ENOMEM;
24692d3391ecSTakashi Iwai 	if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
24702d3391ecSTakashi Iwai 		return -EFAULT;
24712d3391ecSTakashi Iwai 	size -= 8;
24722d3391ecSTakashi Iwai 	dst = tlv + 2;
24732d3391ecSTakashi Iwai 	for (map = info->chmap; map->channels; map++) {
24742d3391ecSTakashi Iwai 		int chs_bytes = map->channels * 4;
24752d3391ecSTakashi Iwai 		if (!valid_chmap_channels(info, map->channels))
24762d3391ecSTakashi Iwai 			continue;
24772d3391ecSTakashi Iwai 		if (size < 8)
24782d3391ecSTakashi Iwai 			return -ENOMEM;
24792d3391ecSTakashi Iwai 		if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
24802d3391ecSTakashi Iwai 		    put_user(chs_bytes, dst + 1))
24812d3391ecSTakashi Iwai 			return -EFAULT;
24822d3391ecSTakashi Iwai 		dst += 2;
24832d3391ecSTakashi Iwai 		size -= 8;
24842d3391ecSTakashi Iwai 		count += 8;
24852d3391ecSTakashi Iwai 		if (size < chs_bytes)
24862d3391ecSTakashi Iwai 			return -ENOMEM;
24872d3391ecSTakashi Iwai 		size -= chs_bytes;
24882d3391ecSTakashi Iwai 		count += chs_bytes;
24892d3391ecSTakashi Iwai 		for (c = 0; c < map->channels; c++) {
24902d3391ecSTakashi Iwai 			if (put_user(map->map[c], dst))
24912d3391ecSTakashi Iwai 				return -EFAULT;
24922d3391ecSTakashi Iwai 			dst++;
24932d3391ecSTakashi Iwai 		}
24942d3391ecSTakashi Iwai 	}
24952d3391ecSTakashi Iwai 	if (put_user(count, tlv + 1))
24962d3391ecSTakashi Iwai 		return -EFAULT;
24972d3391ecSTakashi Iwai 	return 0;
24982d3391ecSTakashi Iwai }
24992d3391ecSTakashi Iwai 
pcm_chmap_ctl_private_free(struct snd_kcontrol * kcontrol)25002d3391ecSTakashi Iwai static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
25012d3391ecSTakashi Iwai {
25022d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
25032d3391ecSTakashi Iwai 	info->pcm->streams[info->stream].chmap_kctl = NULL;
25042d3391ecSTakashi Iwai 	kfree(info);
25052d3391ecSTakashi Iwai }
25062d3391ecSTakashi Iwai 
25072d3391ecSTakashi Iwai /**
25082d3391ecSTakashi Iwai  * snd_pcm_add_chmap_ctls - create channel-mapping control elements
25092d3391ecSTakashi Iwai  * @pcm: the assigned PCM instance
25102d3391ecSTakashi Iwai  * @stream: stream direction
25112d3391ecSTakashi Iwai  * @chmap: channel map elements (for query)
25122d3391ecSTakashi Iwai  * @max_channels: the max number of channels for the stream
25132d3391ecSTakashi Iwai  * @private_value: the value passed to each kcontrol's private_value field
25142d3391ecSTakashi Iwai  * @info_ret: store struct snd_pcm_chmap instance if non-NULL
25152d3391ecSTakashi Iwai  *
25162d3391ecSTakashi Iwai  * Create channel-mapping control elements assigned to the given PCM stream(s).
2517eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error value.
25182d3391ecSTakashi Iwai  */
snd_pcm_add_chmap_ctls(struct snd_pcm * pcm,int stream,const struct snd_pcm_chmap_elem * chmap,int max_channels,unsigned long private_value,struct snd_pcm_chmap ** info_ret)25192d3391ecSTakashi Iwai int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
25202d3391ecSTakashi Iwai 			   const struct snd_pcm_chmap_elem *chmap,
25212d3391ecSTakashi Iwai 			   int max_channels,
25222d3391ecSTakashi Iwai 			   unsigned long private_value,
25232d3391ecSTakashi Iwai 			   struct snd_pcm_chmap **info_ret)
25242d3391ecSTakashi Iwai {
25252d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info;
25262d3391ecSTakashi Iwai 	struct snd_kcontrol_new knew = {
25272d3391ecSTakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
25282d3391ecSTakashi Iwai 		.access = SNDRV_CTL_ELEM_ACCESS_READ |
25292d3391ecSTakashi Iwai 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
25302d3391ecSTakashi Iwai 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
25312d3391ecSTakashi Iwai 		.info = pcm_chmap_ctl_info,
25322d3391ecSTakashi Iwai 		.get = pcm_chmap_ctl_get,
25332d3391ecSTakashi Iwai 		.tlv.c = pcm_chmap_ctl_tlv,
25342d3391ecSTakashi Iwai 	};
25352d3391ecSTakashi Iwai 	int err;
25362d3391ecSTakashi Iwai 
25378d879be8STakashi Iwai 	if (WARN_ON(pcm->streams[stream].chmap_kctl))
25388d879be8STakashi Iwai 		return -EBUSY;
25392d3391ecSTakashi Iwai 	info = kzalloc(sizeof(*info), GFP_KERNEL);
25402d3391ecSTakashi Iwai 	if (!info)
25412d3391ecSTakashi Iwai 		return -ENOMEM;
25422d3391ecSTakashi Iwai 	info->pcm = pcm;
25432d3391ecSTakashi Iwai 	info->stream = stream;
25442d3391ecSTakashi Iwai 	info->chmap = chmap;
25452d3391ecSTakashi Iwai 	info->max_channels = max_channels;
25462d3391ecSTakashi Iwai 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
25472d3391ecSTakashi Iwai 		knew.name = "Playback Channel Map";
25482d3391ecSTakashi Iwai 	else
25492d3391ecSTakashi Iwai 		knew.name = "Capture Channel Map";
25502d3391ecSTakashi Iwai 	knew.device = pcm->device;
25512d3391ecSTakashi Iwai 	knew.count = pcm->streams[stream].substream_count;
25522d3391ecSTakashi Iwai 	knew.private_value = private_value;
25532d3391ecSTakashi Iwai 	info->kctl = snd_ctl_new1(&knew, info);
25542d3391ecSTakashi Iwai 	if (!info->kctl) {
25552d3391ecSTakashi Iwai 		kfree(info);
25562d3391ecSTakashi Iwai 		return -ENOMEM;
25572d3391ecSTakashi Iwai 	}
25582d3391ecSTakashi Iwai 	info->kctl->private_free = pcm_chmap_ctl_private_free;
25592d3391ecSTakashi Iwai 	err = snd_ctl_add(pcm->card, info->kctl);
25602d3391ecSTakashi Iwai 	if (err < 0)
25612d3391ecSTakashi Iwai 		return err;
25622d3391ecSTakashi Iwai 	pcm->streams[stream].chmap_kctl = info->kctl;
25632d3391ecSTakashi Iwai 	if (info_ret)
25642d3391ecSTakashi Iwai 		*info_ret = info;
25652d3391ecSTakashi Iwai 	return 0;
25662d3391ecSTakashi Iwai }
25672d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
2568