xref: /linux/sound/core/pcm_lib.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
1709d24f1a09SCezary Rojewski /**
1710d24f1a09SCezary Rojewski  * snd_pcm_hw_params_bits - Get the number of bits per the sample.
1711d24f1a09SCezary Rojewski  * @p: hardware parameters
1712d24f1a09SCezary Rojewski  *
1713d24f1a09SCezary Rojewski  * Return: The number of bits per sample based on the format,
1714d24f1a09SCezary Rojewski  * subformat and msbits the specified hw params has.
1715d24f1a09SCezary Rojewski  */
snd_pcm_hw_params_bits(const struct snd_pcm_hw_params * p)1716d24f1a09SCezary Rojewski int snd_pcm_hw_params_bits(const struct snd_pcm_hw_params *p)
1717d24f1a09SCezary Rojewski {
1718d24f1a09SCezary Rojewski 	snd_pcm_subformat_t subformat = params_subformat(p);
1719d24f1a09SCezary Rojewski 	snd_pcm_format_t format = params_format(p);
1720d24f1a09SCezary Rojewski 
1721d24f1a09SCezary Rojewski 	switch (format) {
1722d24f1a09SCezary Rojewski 	case SNDRV_PCM_FORMAT_S32_LE:
1723d24f1a09SCezary Rojewski 	case SNDRV_PCM_FORMAT_U32_LE:
1724d24f1a09SCezary Rojewski 	case SNDRV_PCM_FORMAT_S32_BE:
1725d24f1a09SCezary Rojewski 	case SNDRV_PCM_FORMAT_U32_BE:
1726d24f1a09SCezary Rojewski 		switch (subformat) {
1727d24f1a09SCezary Rojewski 		case SNDRV_PCM_SUBFORMAT_MSBITS_20:
1728d24f1a09SCezary Rojewski 			return 20;
1729d24f1a09SCezary Rojewski 		case SNDRV_PCM_SUBFORMAT_MSBITS_24:
1730d24f1a09SCezary Rojewski 			return 24;
1731d24f1a09SCezary Rojewski 		case SNDRV_PCM_SUBFORMAT_MSBITS_MAX:
1732d24f1a09SCezary Rojewski 		case SNDRV_PCM_SUBFORMAT_STD:
1733d24f1a09SCezary Rojewski 		default:
1734d24f1a09SCezary Rojewski 			break;
1735d24f1a09SCezary Rojewski 		}
1736d24f1a09SCezary Rojewski 		fallthrough;
1737d24f1a09SCezary Rojewski 	default:
1738d24f1a09SCezary Rojewski 		return snd_pcm_format_width(format);
1739d24f1a09SCezary Rojewski 	}
1740d24f1a09SCezary Rojewski }
1741d24f1a09SCezary Rojewski EXPORT_SYMBOL(snd_pcm_hw_params_bits);
1742d24f1a09SCezary Rojewski 
snd_pcm_lib_ioctl_reset(struct snd_pcm_substream * substream,void * arg)1743877211f5STakashi Iwai static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
17441da177e4SLinus Torvalds 				   void *arg)
17451da177e4SLinus Torvalds {
1746877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1747*650224feSTakashi Iwai 
1748*650224feSTakashi Iwai 	guard(pcm_stream_lock_irqsave)(substream);
17491da177e4SLinus Torvalds 	if (snd_pcm_running(substream) &&
17501da177e4SLinus Torvalds 	    snd_pcm_update_hw_ptr(substream) >= 0)
17511da177e4SLinus Torvalds 		runtime->status->hw_ptr %= runtime->buffer_size;
17520e8014d7SPierre-Louis Bossart 	else {
17531da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
17540e8014d7SPierre-Louis Bossart 		runtime->hw_ptr_wrap = 0;
17550e8014d7SPierre-Louis Bossart 	}
17561da177e4SLinus Torvalds 	return 0;
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream * substream,void * arg)1759877211f5STakashi Iwai static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
17601da177e4SLinus Torvalds 					  void *arg)
17611da177e4SLinus Torvalds {
1762877211f5STakashi Iwai 	struct snd_pcm_channel_info *info = arg;
1763877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17641da177e4SLinus Torvalds 	int width;
17651da177e4SLinus Torvalds 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
17661da177e4SLinus Torvalds 		info->offset = -1;
17671da177e4SLinus Torvalds 		return 0;
17681da177e4SLinus Torvalds 	}
17691da177e4SLinus Torvalds 	width = snd_pcm_format_physical_width(runtime->format);
17701da177e4SLinus Torvalds 	if (width < 0)
17711da177e4SLinus Torvalds 		return width;
17721da177e4SLinus Torvalds 	info->offset = 0;
17731da177e4SLinus Torvalds 	switch (runtime->access) {
17741da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
17751da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
17761da177e4SLinus Torvalds 		info->first = info->channel * width;
17771da177e4SLinus Torvalds 		info->step = runtime->channels * width;
17781da177e4SLinus Torvalds 		break;
17791da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
17801da177e4SLinus Torvalds 	case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
17811da177e4SLinus Torvalds 	{
17821da177e4SLinus Torvalds 		size_t size = runtime->dma_bytes / runtime->channels;
17831da177e4SLinus Torvalds 		info->first = info->channel * size * 8;
17841da177e4SLinus Torvalds 		info->step = width;
17851da177e4SLinus Torvalds 		break;
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds 	default:
17881da177e4SLinus Torvalds 		snd_BUG();
17891da177e4SLinus Torvalds 		break;
17901da177e4SLinus Torvalds 	}
17911da177e4SLinus Torvalds 	return 0;
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream * substream,void * arg)17948bea869cSJaroslav Kysela static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
17958bea869cSJaroslav Kysela 				       void *arg)
17968bea869cSJaroslav Kysela {
17978bea869cSJaroslav Kysela 	struct snd_pcm_hw_params *params = arg;
17988bea869cSJaroslav Kysela 	snd_pcm_format_t format;
1799a9960e6aSClemens Ladisch 	int channels;
1800a9960e6aSClemens Ladisch 	ssize_t frame_size;
18018bea869cSJaroslav Kysela 
18028bea869cSJaroslav Kysela 	params->fifo_size = substream->runtime->hw.fifo_size;
18038bea869cSJaroslav Kysela 	if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
18048bea869cSJaroslav Kysela 		format = params_format(params);
18058bea869cSJaroslav Kysela 		channels = params_channels(params);
1806a9960e6aSClemens Ladisch 		frame_size = snd_pcm_format_size(format, channels);
1807a9960e6aSClemens Ladisch 		if (frame_size > 0)
1808f3eef46fSZubin Mithra 			params->fifo_size /= frame_size;
18098bea869cSJaroslav Kysela 	}
18108bea869cSJaroslav Kysela 	return 0;
18118bea869cSJaroslav Kysela }
18128bea869cSJaroslav Kysela 
18131da177e4SLinus Torvalds /**
18141da177e4SLinus Torvalds  * snd_pcm_lib_ioctl - a generic PCM ioctl callback
18151da177e4SLinus Torvalds  * @substream: the pcm substream instance
18161da177e4SLinus Torvalds  * @cmd: ioctl command
18171da177e4SLinus Torvalds  * @arg: ioctl argument
18181da177e4SLinus Torvalds  *
18191da177e4SLinus Torvalds  * Processes the generic ioctl commands for PCM.
18201da177e4SLinus Torvalds  * Can be passed as the ioctl callback for PCM ops.
18211da177e4SLinus Torvalds  *
1822eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
18231da177e4SLinus Torvalds  */
snd_pcm_lib_ioctl(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)1824877211f5STakashi Iwai int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
18251da177e4SLinus Torvalds 		      unsigned int cmd, void *arg)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	switch (cmd) {
18281da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
18291da177e4SLinus Torvalds 		return snd_pcm_lib_ioctl_reset(substream, arg);
18301da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
18311da177e4SLinus Torvalds 		return snd_pcm_lib_ioctl_channel_info(substream, arg);
18328bea869cSJaroslav Kysela 	case SNDRV_PCM_IOCTL1_FIFO_SIZE:
18338bea869cSJaroslav Kysela 		return snd_pcm_lib_ioctl_fifo_size(substream, arg);
18341da177e4SLinus Torvalds 	}
18351da177e4SLinus Torvalds 	return -ENXIO;
18361da177e4SLinus Torvalds }
1837e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1838e88e8ae6STakashi Iwai 
18391da177e4SLinus Torvalds /**
184047271b1bSTakashi Sakamoto  * snd_pcm_period_elapsed_under_stream_lock() - update the status of runtime for the next period
184147271b1bSTakashi Sakamoto  *						under acquired lock of PCM substream.
184247271b1bSTakashi Sakamoto  * @substream: the instance of pcm substream.
18431da177e4SLinus Torvalds  *
184447271b1bSTakashi Sakamoto  * This function is called when the batch of audio data frames as the same size as the period of
184547271b1bSTakashi Sakamoto  * buffer is already processed in audio data transmission.
18461da177e4SLinus Torvalds  *
184747271b1bSTakashi Sakamoto  * The call of function updates the status of runtime with the latest position of audio data
184847271b1bSTakashi Sakamoto  * transmission, checks overrun and underrun over buffer, awaken user processes from waiting for
184947271b1bSTakashi Sakamoto  * available audio data frames, sampling audio timestamp, and performs stop or drain the PCM
185047271b1bSTakashi Sakamoto  * substream according to configured threshold.
185147271b1bSTakashi Sakamoto  *
185247271b1bSTakashi Sakamoto  * The function is intended to use for the case that PCM driver operates audio data frames under
185347271b1bSTakashi Sakamoto  * acquired lock of PCM substream; e.g. in callback of any operation of &snd_pcm_ops in process
185447271b1bSTakashi Sakamoto  * context. In any interrupt context, it's preferrable to use ``snd_pcm_period_elapsed()`` instead
185547271b1bSTakashi Sakamoto  * since lock of PCM substream should be acquired in advance.
185647271b1bSTakashi Sakamoto  *
185747271b1bSTakashi Sakamoto  * Developer should pay enough attention that some callbacks in &snd_pcm_ops are done by the call of
185847271b1bSTakashi Sakamoto  * function:
185947271b1bSTakashi Sakamoto  *
186047271b1bSTakashi Sakamoto  * - .pointer - to retrieve current position of audio data transmission by frame count or XRUN state.
186147271b1bSTakashi Sakamoto  * - .trigger - with SNDRV_PCM_TRIGGER_STOP at XRUN or DRAINING state.
186247271b1bSTakashi Sakamoto  * - .get_time_info - to retrieve audio time stamp if needed.
186347271b1bSTakashi Sakamoto  *
186447271b1bSTakashi Sakamoto  * Even if more than one periods have elapsed since the last call, you have to call this only once.
18651da177e4SLinus Torvalds  */
snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream * substream)186647271b1bSTakashi Sakamoto void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream)
18671da177e4SLinus Torvalds {
1868877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime;
18691da177e4SLinus Torvalds 
1870f5cdc9d4Spaulhsia 	if (PCM_RUNTIME_CHECK(substream))
187147271b1bSTakashi Sakamoto 		return;
1872f5cdc9d4Spaulhsia 	runtime = substream->runtime;
1873f5cdc9d4Spaulhsia 
18741da177e4SLinus Torvalds 	if (!snd_pcm_running(substream) ||
1875f240406bSJaroslav Kysela 	    snd_pcm_update_hw_ptr0(substream, 1) < 0)
18761da177e4SLinus Torvalds 		goto _end;
18771da177e4SLinus Torvalds 
187890bbaf66SJie Yang #ifdef CONFIG_SND_PCM_TIMER
18791da177e4SLinus Torvalds 	if (substream->timer_running)
18801da177e4SLinus Torvalds 		snd_timer_interrupt(substream->timer, 1);
188190bbaf66SJie Yang #endif
18821da177e4SLinus Torvalds  _end:
188396b09709STakashi Iwai 	snd_kill_fasync(runtime->fasync, SIGIO, POLL_IN);
188447271b1bSTakashi Sakamoto }
188547271b1bSTakashi Sakamoto EXPORT_SYMBOL(snd_pcm_period_elapsed_under_stream_lock);
188647271b1bSTakashi Sakamoto 
188747271b1bSTakashi Sakamoto /**
188847271b1bSTakashi Sakamoto  * snd_pcm_period_elapsed() - update the status of runtime for the next period by acquiring lock of
188947271b1bSTakashi Sakamoto  *			      PCM substream.
189047271b1bSTakashi Sakamoto  * @substream: the instance of PCM substream.
189147271b1bSTakashi Sakamoto  *
189247271b1bSTakashi Sakamoto  * This function is mostly similar to ``snd_pcm_period_elapsed_under_stream_lock()`` except for
189347271b1bSTakashi Sakamoto  * acquiring lock of PCM substream voluntarily.
189447271b1bSTakashi Sakamoto  *
189547271b1bSTakashi Sakamoto  * It's typically called by any type of IRQ handler when hardware IRQ occurs to notify event that
189647271b1bSTakashi Sakamoto  * the batch of audio data frames as the same size as the period of buffer is already processed in
189747271b1bSTakashi Sakamoto  * audio data transmission.
189847271b1bSTakashi Sakamoto  */
snd_pcm_period_elapsed(struct snd_pcm_substream * substream)189947271b1bSTakashi Sakamoto void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
190047271b1bSTakashi Sakamoto {
190147271b1bSTakashi Sakamoto 	if (snd_BUG_ON(!substream))
190247271b1bSTakashi Sakamoto 		return;
190347271b1bSTakashi Sakamoto 
1904*650224feSTakashi Iwai 	guard(pcm_stream_lock_irqsave)(substream);
190547271b1bSTakashi Sakamoto 	snd_pcm_period_elapsed_under_stream_lock(substream);
19061da177e4SLinus Torvalds }
1907e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_period_elapsed);
1908e88e8ae6STakashi Iwai 
190913075510STakashi Iwai /*
191013075510STakashi Iwai  * Wait until avail_min data becomes available
191113075510STakashi Iwai  * Returns a negative error code if any error occurs during operation.
191213075510STakashi Iwai  * The available space is stored on availp.  When err = 0 and avail = 0
191313075510STakashi Iwai  * on the capture stream, it indicates the stream is in DRAINING state.
191413075510STakashi Iwai  */
wait_for_avail(struct snd_pcm_substream * substream,snd_pcm_uframes_t * availp)19155daeba34SDavid Dillow static int wait_for_avail(struct snd_pcm_substream *substream,
191613075510STakashi Iwai 			      snd_pcm_uframes_t *availp)
191713075510STakashi Iwai {
191813075510STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
191913075510STakashi Iwai 	int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1920ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
192113075510STakashi Iwai 	int err = 0;
192213075510STakashi Iwai 	snd_pcm_uframes_t avail = 0;
1923f2b3614cSTakashi Iwai 	long wait_time, tout;
192413075510STakashi Iwai 
1925763437a9SArjan van de Ven 	init_waitqueue_entry(&wait, current);
1926763437a9SArjan van de Ven 	set_current_state(TASK_INTERRUPTIBLE);
1927763437a9SArjan van de Ven 	add_wait_queue(&runtime->tsleep, &wait);
1928763437a9SArjan van de Ven 
1929f2b3614cSTakashi Iwai 	if (runtime->no_period_wakeup)
1930f2b3614cSTakashi Iwai 		wait_time = MAX_SCHEDULE_TIMEOUT;
1931f2b3614cSTakashi Iwai 	else {
1932d64c5cf8SLiam Girdwood 		/* use wait time from substream if available */
1933d64c5cf8SLiam Girdwood 		if (substream->wait_time) {
1934d64c5cf8SLiam Girdwood 			wait_time = substream->wait_time;
1935d64c5cf8SLiam Girdwood 		} else {
19363ed2b549SOswald Buddenhagen 			wait_time = 100;
1937d64c5cf8SLiam Girdwood 
1938f2b3614cSTakashi Iwai 			if (runtime->rate) {
19393ed2b549SOswald Buddenhagen 				long t = runtime->buffer_size * 1100 / runtime->rate;
1940f2b3614cSTakashi Iwai 				wait_time = max(t, wait_time);
1941f2b3614cSTakashi Iwai 			}
1942f2b3614cSTakashi Iwai 		}
19433ed2b549SOswald Buddenhagen 		wait_time = msecs_to_jiffies(wait_time);
1944d64c5cf8SLiam Girdwood 	}
1945763437a9SArjan van de Ven 
194613075510STakashi Iwai 	for (;;) {
194713075510STakashi Iwai 		if (signal_pending(current)) {
194813075510STakashi Iwai 			err = -ERESTARTSYS;
194913075510STakashi Iwai 			break;
195013075510STakashi Iwai 		}
1951763437a9SArjan van de Ven 
1952763437a9SArjan van de Ven 		/*
1953763437a9SArjan van de Ven 		 * We need to check if space became available already
1954763437a9SArjan van de Ven 		 * (and thus the wakeup happened already) first to close
1955763437a9SArjan van de Ven 		 * the race of space already having become available.
1956763437a9SArjan van de Ven 		 * This check must happen after been added to the waitqueue
1957763437a9SArjan van de Ven 		 * and having current state be INTERRUPTIBLE.
1958763437a9SArjan van de Ven 		 */
1959763e5067STakashi Iwai 		avail = snd_pcm_avail(substream);
1960763437a9SArjan van de Ven 		if (avail >= runtime->twake)
1961763437a9SArjan van de Ven 			break;
196213075510STakashi Iwai 		snd_pcm_stream_unlock_irq(substream);
1963763437a9SArjan van de Ven 
1964763437a9SArjan van de Ven 		tout = schedule_timeout(wait_time);
1965763437a9SArjan van de Ven 
196613075510STakashi Iwai 		snd_pcm_stream_lock_irq(substream);
1967763437a9SArjan van de Ven 		set_current_state(TASK_INTERRUPTIBLE);
1968f0061c18STakashi Iwai 		switch (runtime->state) {
196913075510STakashi Iwai 		case SNDRV_PCM_STATE_SUSPENDED:
197013075510STakashi Iwai 			err = -ESTRPIPE;
197113075510STakashi Iwai 			goto _endloop;
197213075510STakashi Iwai 		case SNDRV_PCM_STATE_XRUN:
197313075510STakashi Iwai 			err = -EPIPE;
197413075510STakashi Iwai 			goto _endloop;
197513075510STakashi Iwai 		case SNDRV_PCM_STATE_DRAINING:
197613075510STakashi Iwai 			if (is_playback)
197713075510STakashi Iwai 				err = -EPIPE;
197813075510STakashi Iwai 			else
197913075510STakashi Iwai 				avail = 0; /* indicate draining */
198013075510STakashi Iwai 			goto _endloop;
198113075510STakashi Iwai 		case SNDRV_PCM_STATE_OPEN:
198213075510STakashi Iwai 		case SNDRV_PCM_STATE_SETUP:
198313075510STakashi Iwai 		case SNDRV_PCM_STATE_DISCONNECTED:
198413075510STakashi Iwai 			err = -EBADFD;
198513075510STakashi Iwai 			goto _endloop;
1986ed697e1aSJongHo Kim 		case SNDRV_PCM_STATE_PAUSED:
1987ed697e1aSJongHo Kim 			continue;
198813075510STakashi Iwai 		}
198913075510STakashi Iwai 		if (!tout) {
199009e56df8STakashi Iwai 			pcm_dbg(substream->pcm,
19913ed2b549SOswald Buddenhagen 				"%s timeout (DMA or IRQ trouble?)\n",
19923ed2b549SOswald Buddenhagen 				is_playback ? "playback write" : "capture read");
199313075510STakashi Iwai 			err = -EIO;
199413075510STakashi Iwai 			break;
199513075510STakashi Iwai 		}
199613075510STakashi Iwai 	}
199713075510STakashi Iwai  _endloop:
1998763437a9SArjan van de Ven 	set_current_state(TASK_RUNNING);
1999c91a988dSJaroslav Kysela 	remove_wait_queue(&runtime->tsleep, &wait);
200013075510STakashi Iwai 	*availp = avail;
200113075510STakashi Iwai 	return err;
200213075510STakashi Iwai }
200313075510STakashi Iwai 
20049f600630STakashi Iwai typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
20059f600630STakashi Iwai 			      int channel, unsigned long hwoff,
2006cf393babSTakashi Iwai 			      struct iov_iter *iter, unsigned long bytes);
2007bdc4acf7STakashi Iwai 
20089f600630STakashi Iwai typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
2009cf393babSTakashi Iwai 			  snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f,
2010cf393babSTakashi Iwai 			  bool);
20119f600630STakashi Iwai 
20129f600630STakashi Iwai /* calculate the target DMA-buffer position to be written/read */
get_dma_ptr(struct snd_pcm_runtime * runtime,int channel,unsigned long hwoff)20139f600630STakashi Iwai static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
20149f600630STakashi Iwai 			   int channel, unsigned long hwoff)
20151da177e4SLinus Torvalds {
20169f600630STakashi Iwai 	return runtime->dma_area + hwoff +
20179f600630STakashi Iwai 		channel * (runtime->dma_bytes / runtime->channels);
20181da177e4SLinus Torvalds }
20199f600630STakashi Iwai 
2020cf393babSTakashi 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)20215c7264cfSTakashi Iwai static int default_write_copy(struct snd_pcm_substream *substream,
20229f600630STakashi Iwai 			      int channel, unsigned long hwoff,
2023cf393babSTakashi Iwai 			      struct iov_iter *iter, unsigned long bytes)
20249f600630STakashi Iwai {
2025e14ebde5STakashi Iwai 	if (copy_from_iter(get_dma_ptr(substream->runtime, channel, hwoff),
2026e14ebde5STakashi Iwai 			   bytes, iter) != bytes)
20271da177e4SLinus Torvalds 		return -EFAULT;
20281da177e4SLinus Torvalds 	return 0;
20291da177e4SLinus Torvalds }
20301da177e4SLinus Torvalds 
20319f600630STakashi Iwai /* fill silence instead of copy data; called as a transfer helper
20329f600630STakashi Iwai  * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
20339f600630STakashi Iwai  * a NULL buffer is passed
20349f600630STakashi Iwai  */
fill_silence(struct snd_pcm_substream * substream,int channel,unsigned long hwoff,struct iov_iter * iter,unsigned long bytes)20359f600630STakashi Iwai static int fill_silence(struct snd_pcm_substream *substream, int channel,
2036cf393babSTakashi Iwai 			unsigned long hwoff, struct iov_iter *iter,
2037cf393babSTakashi Iwai 			unsigned long bytes)
20381da177e4SLinus Torvalds {
2039877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
20401da177e4SLinus Torvalds 
20419f600630STakashi Iwai 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
20421da177e4SLinus Torvalds 		return 0;
20439f600630STakashi Iwai 	if (substream->ops->fill_silence)
20449f600630STakashi Iwai 		return substream->ops->fill_silence(substream, channel,
20459f600630STakashi Iwai 						    hwoff, bytes);
20461da177e4SLinus Torvalds 
20479f600630STakashi Iwai 	snd_pcm_format_set_silence(runtime->format,
20489f600630STakashi Iwai 				   get_dma_ptr(runtime, channel, hwoff),
20499f600630STakashi Iwai 				   bytes_to_samples(runtime, bytes));
20509f600630STakashi Iwai 	return 0;
20511da177e4SLinus Torvalds }
20521da177e4SLinus Torvalds 
2053cf393babSTakashi 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)20545c7264cfSTakashi Iwai static int default_read_copy(struct snd_pcm_substream *substream,
20555c7264cfSTakashi Iwai 			     int channel, unsigned long hwoff,
2056cf393babSTakashi Iwai 			     struct iov_iter *iter, unsigned long bytes)
20575c7264cfSTakashi Iwai {
2058e14ebde5STakashi Iwai 	if (copy_to_iter(get_dma_ptr(substream->runtime, channel, hwoff),
2059e14ebde5STakashi Iwai 			 bytes, iter) != bytes)
20605c7264cfSTakashi Iwai 		return -EFAULT;
20615c7264cfSTakashi Iwai 	return 0;
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds 
2064cf393babSTakashi 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)2065cf393babSTakashi Iwai static int do_transfer(struct snd_pcm_substream *substream, int c,
2066cf393babSTakashi Iwai 		       unsigned long hwoff, void *data, unsigned long bytes,
2067cf393babSTakashi Iwai 		       pcm_transfer_f transfer, bool in_kernel)
2068cf393babSTakashi Iwai {
2069cf393babSTakashi Iwai 	struct iov_iter iter;
2070cf393babSTakashi Iwai 	int err, type;
2071cf393babSTakashi Iwai 
2072cf393babSTakashi Iwai 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2073cf393babSTakashi Iwai 		type = ITER_SOURCE;
2074cf393babSTakashi Iwai 	else
2075cf393babSTakashi Iwai 		type = ITER_DEST;
2076cf393babSTakashi Iwai 
2077cf393babSTakashi Iwai 	if (in_kernel) {
2078cf393babSTakashi Iwai 		struct kvec kvec = { data, bytes };
2079cf393babSTakashi Iwai 
2080cf393babSTakashi Iwai 		iov_iter_kvec(&iter, type, &kvec, 1, bytes);
2081cf393babSTakashi Iwai 		return transfer(substream, c, hwoff, &iter, bytes);
2082cf393babSTakashi Iwai 	}
2083cf393babSTakashi Iwai 
2084cf393babSTakashi Iwai 	err = import_ubuf(type, (__force void __user *)data, bytes, &iter);
2085cf393babSTakashi Iwai 	if (err)
2086cf393babSTakashi Iwai 		return err;
2087cf393babSTakashi Iwai 	return transfer(substream, c, hwoff, &iter, bytes);
208868541213STakashi Iwai }
208968541213STakashi Iwai 
20909f600630STakashi Iwai /* call transfer function with the converted pointers and sizes;
20919f600630STakashi Iwai  * for interleaved mode, it's one shot for all samples
20929f600630STakashi 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)20939f600630STakashi Iwai static int interleaved_copy(struct snd_pcm_substream *substream,
20949f600630STakashi Iwai 			    snd_pcm_uframes_t hwoff, void *data,
20959f600630STakashi Iwai 			    snd_pcm_uframes_t off,
20969f600630STakashi Iwai 			    snd_pcm_uframes_t frames,
2097cf393babSTakashi Iwai 			    pcm_transfer_f transfer,
2098cf393babSTakashi Iwai 			    bool in_kernel)
20999f600630STakashi Iwai {
21009f600630STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
21019f600630STakashi Iwai 
21029f600630STakashi Iwai 	/* convert to bytes */
21039f600630STakashi Iwai 	hwoff = frames_to_bytes(runtime, hwoff);
21049f600630STakashi Iwai 	off = frames_to_bytes(runtime, off);
21059f600630STakashi Iwai 	frames = frames_to_bytes(runtime, frames);
2106cf393babSTakashi Iwai 
2107cf393babSTakashi Iwai 	return do_transfer(substream, 0, hwoff, data + off, frames, transfer,
2108cf393babSTakashi Iwai 			   in_kernel);
21099f600630STakashi Iwai }
21109f600630STakashi Iwai 
21119f600630STakashi Iwai /* call transfer function with the converted pointers and sizes for each
21129f600630STakashi Iwai  * non-interleaved channel; when buffer is NULL, silencing instead of copying
21139f600630STakashi 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)21149f600630STakashi Iwai static int noninterleaved_copy(struct snd_pcm_substream *substream,
21159f600630STakashi Iwai 			       snd_pcm_uframes_t hwoff, void *data,
21169f600630STakashi Iwai 			       snd_pcm_uframes_t off,
21179f600630STakashi Iwai 			       snd_pcm_uframes_t frames,
2118cf393babSTakashi Iwai 			       pcm_transfer_f transfer,
2119cf393babSTakashi Iwai 			       bool in_kernel)
21209f600630STakashi Iwai {
21219f600630STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
21229f600630STakashi Iwai 	int channels = runtime->channels;
21239f600630STakashi Iwai 	void **bufs = data;
21249f600630STakashi Iwai 	int c, err;
21259f600630STakashi Iwai 
21269f600630STakashi Iwai 	/* convert to bytes; note that it's not frames_to_bytes() here.
21279f600630STakashi Iwai 	 * in non-interleaved mode, we copy for each channel, thus
21289f600630STakashi Iwai 	 * each copy is n_samples bytes x channels = whole frames.
21299f600630STakashi Iwai 	 */
2130bdc4acf7STakashi Iwai 	off = samples_to_bytes(runtime, off);
2131bdc4acf7STakashi Iwai 	frames = samples_to_bytes(runtime, frames);
21329f600630STakashi Iwai 	hwoff = samples_to_bytes(runtime, hwoff);
2133bdc4acf7STakashi Iwai 	for (c = 0; c < channels; ++c, ++bufs) {
21349f600630STakashi Iwai 		if (!data || !*bufs)
21359f600630STakashi Iwai 			err = fill_silence(substream, c, hwoff, NULL, frames);
21369f600630STakashi Iwai 		else
2137cf393babSTakashi Iwai 			err = do_transfer(substream, c, hwoff, *bufs + off,
2138cf393babSTakashi Iwai 					  frames, transfer, in_kernel);
21391da177e4SLinus Torvalds 		if (err < 0)
2140bdc4acf7STakashi Iwai 			return err;
21411da177e4SLinus Torvalds 	}
2142bdc4acf7STakashi Iwai 	return 0;
21431da177e4SLinus Torvalds }
2144bdc4acf7STakashi Iwai 
2145a9cd29e7STakashi Iwai /* fill silence on the given buffer position;
2146a9cd29e7STakashi Iwai  * called from snd_pcm_playback_silence()
2147a9cd29e7STakashi Iwai  */
fill_silence_frames(struct snd_pcm_substream * substream,snd_pcm_uframes_t off,snd_pcm_uframes_t frames)2148a9cd29e7STakashi Iwai static int fill_silence_frames(struct snd_pcm_substream *substream,
2149a9cd29e7STakashi Iwai 			       snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2150a9cd29e7STakashi Iwai {
2151a9cd29e7STakashi Iwai 	if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2152a9cd29e7STakashi Iwai 	    substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2153a9cd29e7STakashi Iwai 		return interleaved_copy(substream, off, NULL, 0, frames,
2154cf393babSTakashi Iwai 					fill_silence, true);
2155a9cd29e7STakashi Iwai 	else
2156a9cd29e7STakashi Iwai 		return noninterleaved_copy(substream, off, NULL, 0, frames,
2157cf393babSTakashi Iwai 					   fill_silence, true);
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
21607eaa943cSTakashi Iwai /* sanity-check for read/write methods */
pcm_sanity_check(struct snd_pcm_substream * substream)21617eaa943cSTakashi Iwai static int pcm_sanity_check(struct snd_pcm_substream *substream)
21627eaa943cSTakashi Iwai {
21637eaa943cSTakashi Iwai 	struct snd_pcm_runtime *runtime;
21647eaa943cSTakashi Iwai 	if (PCM_RUNTIME_CHECK(substream))
21657eaa943cSTakashi Iwai 		return -ENXIO;
21667eaa943cSTakashi Iwai 	runtime = substream->runtime;
21676c0217b1STakashi Iwai 	if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
21687eaa943cSTakashi Iwai 		return -EINVAL;
2169f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_OPEN)
21707eaa943cSTakashi Iwai 		return -EBADFD;
21717eaa943cSTakashi Iwai 	return 0;
21727eaa943cSTakashi Iwai }
21737eaa943cSTakashi Iwai 
pcm_accessible_state(struct snd_pcm_runtime * runtime)21746ba63929STakashi Iwai static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
21751da177e4SLinus Torvalds {
2176f0061c18STakashi Iwai 	switch (runtime->state) {
21776ba63929STakashi Iwai 	case SNDRV_PCM_STATE_PREPARED:
21786ba63929STakashi Iwai 	case SNDRV_PCM_STATE_RUNNING:
21796ba63929STakashi Iwai 	case SNDRV_PCM_STATE_PAUSED:
21806ba63929STakashi Iwai 		return 0;
21816ba63929STakashi Iwai 	case SNDRV_PCM_STATE_XRUN:
21826ba63929STakashi Iwai 		return -EPIPE;
21836ba63929STakashi Iwai 	case SNDRV_PCM_STATE_SUSPENDED:
21846ba63929STakashi Iwai 		return -ESTRPIPE;
21856ba63929STakashi Iwai 	default:
21866ba63929STakashi Iwai 		return -EBADFD;
21876ba63929STakashi Iwai 	}
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
219066e01a5cSTakashi Sakamoto /* update to the given appl_ptr and call ack callback if needed;
219166e01a5cSTakashi Sakamoto  * when an error is returned, take back to the original value
219266e01a5cSTakashi Sakamoto  */
pcm_lib_apply_appl_ptr(struct snd_pcm_substream * substream,snd_pcm_uframes_t appl_ptr)219366e01a5cSTakashi Sakamoto int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
219466e01a5cSTakashi Sakamoto 			   snd_pcm_uframes_t appl_ptr)
21951da177e4SLinus Torvalds {
2196877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
219766e01a5cSTakashi Sakamoto 	snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2198b456abe6SPierre-Louis Bossart 	snd_pcm_sframes_t diff;
219966e01a5cSTakashi Sakamoto 	int ret;
220066e01a5cSTakashi Sakamoto 
2201f8ff2f28STakashi Iwai 	if (old_appl_ptr == appl_ptr)
2202f8ff2f28STakashi Iwai 		return 0;
2203f8ff2f28STakashi Iwai 
22040e888a74SPierre-Louis Bossart 	if (appl_ptr >= runtime->boundary)
22050e888a74SPierre-Louis Bossart 		return -EINVAL;
2206b456abe6SPierre-Louis Bossart 	/*
2207b456abe6SPierre-Louis Bossart 	 * check if a rewind is requested by the application
2208b456abe6SPierre-Louis Bossart 	 */
2209b456abe6SPierre-Louis Bossart 	if (substream->runtime->info & SNDRV_PCM_INFO_NO_REWINDS) {
2210b456abe6SPierre-Louis Bossart 		diff = appl_ptr - old_appl_ptr;
2211b456abe6SPierre-Louis Bossart 		if (diff >= 0) {
2212b456abe6SPierre-Louis Bossart 			if (diff > runtime->buffer_size)
2213b456abe6SPierre-Louis Bossart 				return -EINVAL;
2214b456abe6SPierre-Louis Bossart 		} else {
2215b456abe6SPierre-Louis Bossart 			if (runtime->boundary + diff > runtime->buffer_size)
2216b456abe6SPierre-Louis Bossart 				return -EINVAL;
2217b456abe6SPierre-Louis Bossart 		}
2218b456abe6SPierre-Louis Bossart 	}
22190e888a74SPierre-Louis Bossart 
222066e01a5cSTakashi Sakamoto 	runtime->control->appl_ptr = appl_ptr;
222166e01a5cSTakashi Sakamoto 	if (substream->ops->ack) {
222266e01a5cSTakashi Sakamoto 		ret = substream->ops->ack(substream);
222366e01a5cSTakashi Sakamoto 		if (ret < 0) {
222466e01a5cSTakashi Sakamoto 			runtime->control->appl_ptr = old_appl_ptr;
22258c721c53STakashi Iwai 			if (ret == -EPIPE)
22268c721c53STakashi Iwai 				__snd_pcm_xrun(substream);
222766e01a5cSTakashi Sakamoto 			return ret;
22281da177e4SLinus Torvalds 		}
22291da177e4SLinus Torvalds 	}
2230fccf5388STakashi Sakamoto 
2231fccf5388STakashi Sakamoto 	trace_applptr(substream, old_appl_ptr, appl_ptr);
2232fccf5388STakashi Sakamoto 
22331da177e4SLinus Torvalds 	return 0;
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22365c7264cfSTakashi 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)22375c7264cfSTakashi Iwai snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2238c48f12eeSTakashi Iwai 				     void *data, bool interleaved,
223968541213STakashi Iwai 				     snd_pcm_uframes_t size, bool in_kernel)
22401da177e4SLinus Torvalds {
2241877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
22421da177e4SLinus Torvalds 	snd_pcm_uframes_t xfer = 0;
22431da177e4SLinus Torvalds 	snd_pcm_uframes_t offset = 0;
22440910c216STakashi Iwai 	snd_pcm_uframes_t avail;
22459f600630STakashi Iwai 	pcm_copy_f writer;
22469f600630STakashi Iwai 	pcm_transfer_f transfer;
2247c48f12eeSTakashi Iwai 	bool nonblock;
22485c7264cfSTakashi Iwai 	bool is_playback;
2249c48f12eeSTakashi Iwai 	int err;
2250c48f12eeSTakashi Iwai 
2251c48f12eeSTakashi Iwai 	err = pcm_sanity_check(substream);
2252c48f12eeSTakashi Iwai 	if (err < 0)
2253c48f12eeSTakashi Iwai 		return err;
2254c48f12eeSTakashi Iwai 
22555c7264cfSTakashi Iwai 	is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2256c48f12eeSTakashi Iwai 	if (interleaved) {
2257c48f12eeSTakashi Iwai 		if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2258c48f12eeSTakashi Iwai 		    runtime->channels > 1)
2259c48f12eeSTakashi Iwai 			return -EINVAL;
22609f600630STakashi Iwai 		writer = interleaved_copy;
2261c48f12eeSTakashi Iwai 	} else {
2262c48f12eeSTakashi Iwai 		if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2263c48f12eeSTakashi Iwai 			return -EINVAL;
22649f600630STakashi Iwai 		writer = noninterleaved_copy;
22659f600630STakashi Iwai 	}
22669f600630STakashi Iwai 
22679f600630STakashi Iwai 	if (!data) {
22685c7264cfSTakashi Iwai 		if (is_playback)
22699f600630STakashi Iwai 			transfer = fill_silence;
22705c7264cfSTakashi Iwai 		else
22715c7264cfSTakashi Iwai 			return -EINVAL;
22729f600630STakashi Iwai 	} else {
2273cf393babSTakashi Iwai 		if (substream->ops->copy)
2274cf393babSTakashi Iwai 			transfer = substream->ops->copy;
22759f600630STakashi Iwai 		else
22765c7264cfSTakashi Iwai 			transfer = is_playback ?
22775c7264cfSTakashi Iwai 				default_write_copy : default_read_copy;
2278c48f12eeSTakashi Iwai 	}
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	if (size == 0)
22811da177e4SLinus Torvalds 		return 0;
22821da177e4SLinus Torvalds 
2283c48f12eeSTakashi Iwai 	nonblock = !!(substream->f_flags & O_NONBLOCK);
2284c48f12eeSTakashi Iwai 
22851da177e4SLinus Torvalds 	snd_pcm_stream_lock_irq(substream);
22866ba63929STakashi Iwai 	err = pcm_accessible_state(runtime);
22871da177e4SLinus Torvalds 	if (err < 0)
22881da177e4SLinus Torvalds 		goto _end_unlock;
22891da177e4SLinus Torvalds 
229064b6acf6SRicardo Biehl Pasquali 	runtime->twake = runtime->control->avail_min ? : 1;
2291f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_RUNNING)
229264b6acf6SRicardo Biehl Pasquali 		snd_pcm_update_hw_ptr(substream);
229364b6acf6SRicardo Biehl Pasquali 
2294932a8151SRicardo Biehl Pasquali 	/*
2295932a8151SRicardo Biehl Pasquali 	 * If size < start_threshold, wait indefinitely. Another
2296932a8151SRicardo Biehl Pasquali 	 * thread may start capture
2297932a8151SRicardo Biehl Pasquali 	 */
22985c7264cfSTakashi Iwai 	if (!is_playback &&
2299f0061c18STakashi Iwai 	    runtime->state == SNDRV_PCM_STATE_PREPARED &&
230000a399caSTakashi Iwai 	    size >= runtime->start_threshold) {
23015c7264cfSTakashi Iwai 		err = snd_pcm_start(substream);
23025c7264cfSTakashi Iwai 		if (err < 0)
23031da177e4SLinus Torvalds 			goto _end_unlock;
23041da177e4SLinus Torvalds 	}
23051da177e4SLinus Torvalds 
2306763e5067STakashi Iwai 	avail = snd_pcm_avail(substream);
230764b6acf6SRicardo Biehl Pasquali 
23080910c216STakashi Iwai 	while (size > 0) {
23090910c216STakashi Iwai 		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
23100910c216STakashi Iwai 		snd_pcm_uframes_t cont;
2311d948035aSTakashi Iwai 		if (!avail) {
23125c7264cfSTakashi Iwai 			if (!is_playback &&
2313f0061c18STakashi Iwai 			    runtime->state == SNDRV_PCM_STATE_DRAINING) {
231413075510STakashi Iwai 				snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
23151da177e4SLinus Torvalds 				goto _end_unlock;
23161da177e4SLinus Torvalds 			}
23171da177e4SLinus Torvalds 			if (nonblock) {
23181da177e4SLinus Torvalds 				err = -EAGAIN;
23191da177e4SLinus Torvalds 				goto _end_unlock;
23201da177e4SLinus Torvalds 			}
23215daeba34SDavid Dillow 			runtime->twake = min_t(snd_pcm_uframes_t, size,
23225daeba34SDavid Dillow 					runtime->control->avail_min ? : 1);
23235daeba34SDavid Dillow 			err = wait_for_avail(substream, &avail);
232413075510STakashi Iwai 			if (err < 0)
23251da177e4SLinus Torvalds 				goto _end_unlock;
232613075510STakashi Iwai 			if (!avail)
232713075510STakashi Iwai 				continue; /* draining */
23281da177e4SLinus Torvalds 		}
23291da177e4SLinus Torvalds 		frames = size > avail ? avail : size;
2330aa30db06STakashi Iwai 		appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2331aa30db06STakashi Iwai 		appl_ofs = appl_ptr % runtime->buffer_size;
2332aa30db06STakashi Iwai 		cont = runtime->buffer_size - appl_ofs;
23331da177e4SLinus Torvalds 		if (frames > cont)
23341da177e4SLinus Torvalds 			frames = cont;
23357eaa943cSTakashi Iwai 		if (snd_BUG_ON(!frames)) {
2336315d9f1bSTakashi Iwai 			err = -EINVAL;
2337315d9f1bSTakashi Iwai 			goto _end_unlock;
23387eaa943cSTakashi Iwai 		}
2339bc55cfd5STakashi Iwai 		if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
2340bc55cfd5STakashi Iwai 			err = -EBUSY;
2341bc55cfd5STakashi Iwai 			goto _end_unlock;
2342bc55cfd5STakashi Iwai 		}
23431da177e4SLinus Torvalds 		snd_pcm_stream_unlock_irq(substream);
2344a25684a9STakashi Iwai 		if (!is_playback)
2345a25684a9STakashi Iwai 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
23469f600630STakashi Iwai 		err = writer(substream, appl_ofs, data, offset, frames,
2347cf393babSTakashi Iwai 			     transfer, in_kernel);
2348a25684a9STakashi Iwai 		if (is_playback)
2349a25684a9STakashi Iwai 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
23501da177e4SLinus Torvalds 		snd_pcm_stream_lock_irq(substream);
2351bc55cfd5STakashi Iwai 		atomic_dec(&runtime->buffer_accessing);
23521250932eSJaroslav Kysela 		if (err < 0)
23531250932eSJaroslav Kysela 			goto _end_unlock;
23546ba63929STakashi Iwai 		err = pcm_accessible_state(runtime);
23556ba63929STakashi Iwai 		if (err < 0)
23561da177e4SLinus Torvalds 			goto _end_unlock;
23571da177e4SLinus Torvalds 		appl_ptr += frames;
23581da177e4SLinus Torvalds 		if (appl_ptr >= runtime->boundary)
23591da177e4SLinus Torvalds 			appl_ptr -= runtime->boundary;
236066e01a5cSTakashi Sakamoto 		err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
236166e01a5cSTakashi Sakamoto 		if (err < 0)
236266e01a5cSTakashi Sakamoto 			goto _end_unlock;
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 		offset += frames;
23651da177e4SLinus Torvalds 		size -= frames;
23661da177e4SLinus Torvalds 		xfer += frames;
23670910c216STakashi Iwai 		avail -= frames;
23685c7264cfSTakashi Iwai 		if (is_playback &&
2369f0061c18STakashi Iwai 		    runtime->state == SNDRV_PCM_STATE_PREPARED &&
23701da177e4SLinus Torvalds 		    snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
23711da177e4SLinus Torvalds 			err = snd_pcm_start(substream);
23721da177e4SLinus Torvalds 			if (err < 0)
23731da177e4SLinus Torvalds 				goto _end_unlock;
23741da177e4SLinus Torvalds 		}
23751da177e4SLinus Torvalds 	}
23761da177e4SLinus Torvalds  _end_unlock:
2377c91a988dSJaroslav Kysela 	runtime->twake = 0;
23781250932eSJaroslav Kysela 	if (xfer > 0 && err >= 0)
23791250932eSJaroslav Kysela 		snd_pcm_update_state(substream, runtime);
23801da177e4SLinus Torvalds 	snd_pcm_stream_unlock_irq(substream);
23811da177e4SLinus Torvalds 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
23821da177e4SLinus Torvalds }
23835c7264cfSTakashi Iwai EXPORT_SYMBOL(__snd_pcm_lib_xfer);
23842d3391ecSTakashi Iwai 
23852d3391ecSTakashi Iwai /*
23862d3391ecSTakashi Iwai  * standard channel mapping helpers
23872d3391ecSTakashi Iwai  */
23882d3391ecSTakashi Iwai 
23892d3391ecSTakashi Iwai /* default channel maps for multi-channel playbacks, up to 8 channels */
23902d3391ecSTakashi Iwai const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
23912d3391ecSTakashi Iwai 	{ .channels = 1,
23925efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
23932d3391ecSTakashi Iwai 	{ .channels = 2,
23942d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
23952d3391ecSTakashi Iwai 	{ .channels = 4,
23962d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
23972d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
23982d3391ecSTakashi Iwai 	{ .channels = 6,
23992d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
24002d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
24012d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
24022d3391ecSTakashi Iwai 	{ .channels = 8,
24032d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
24042d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
24052d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
24062d3391ecSTakashi Iwai 		   SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
24072d3391ecSTakashi Iwai 	{ }
24082d3391ecSTakashi Iwai };
24092d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
24102d3391ecSTakashi Iwai 
24112d3391ecSTakashi Iwai /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
24122d3391ecSTakashi Iwai const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
24132d3391ecSTakashi Iwai 	{ .channels = 1,
24145efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
24152d3391ecSTakashi Iwai 	{ .channels = 2,
24162d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
24172d3391ecSTakashi Iwai 	{ .channels = 4,
24182d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
24192d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
24202d3391ecSTakashi Iwai 	{ .channels = 6,
24212d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
24222d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
24232d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
24242d3391ecSTakashi Iwai 	{ .channels = 8,
24252d3391ecSTakashi Iwai 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
24262d3391ecSTakashi Iwai 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
24272d3391ecSTakashi Iwai 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
24282d3391ecSTakashi Iwai 		   SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
24292d3391ecSTakashi Iwai 	{ }
24302d3391ecSTakashi Iwai };
24312d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
24322d3391ecSTakashi Iwai 
valid_chmap_channels(const struct snd_pcm_chmap * info,int ch)24332d3391ecSTakashi Iwai static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
24342d3391ecSTakashi Iwai {
24352d3391ecSTakashi Iwai 	if (ch > info->max_channels)
24362d3391ecSTakashi Iwai 		return false;
24372d3391ecSTakashi Iwai 	return !info->channel_mask || (info->channel_mask & (1U << ch));
24382d3391ecSTakashi Iwai }
24392d3391ecSTakashi Iwai 
pcm_chmap_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)24402d3391ecSTakashi Iwai static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
24412d3391ecSTakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
24422d3391ecSTakashi Iwai {
24432d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24442d3391ecSTakashi Iwai 
24452d3391ecSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
24462d3391ecSTakashi Iwai 	uinfo->count = info->max_channels;
24472d3391ecSTakashi Iwai 	uinfo->value.integer.min = 0;
24482d3391ecSTakashi Iwai 	uinfo->value.integer.max = SNDRV_CHMAP_LAST;
24492d3391ecSTakashi Iwai 	return 0;
24502d3391ecSTakashi Iwai }
24512d3391ecSTakashi Iwai 
24522d3391ecSTakashi Iwai /* get callback for channel map ctl element
24532d3391ecSTakashi Iwai  * stores the channel position firstly matching with the current channels
24542d3391ecSTakashi Iwai  */
pcm_chmap_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)24552d3391ecSTakashi Iwai static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
24562d3391ecSTakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
24572d3391ecSTakashi Iwai {
24582d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24592d3391ecSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
24602d3391ecSTakashi Iwai 	struct snd_pcm_substream *substream;
24612d3391ecSTakashi Iwai 	const struct snd_pcm_chmap_elem *map;
24622d3391ecSTakashi Iwai 
24632deaeaf1STakashi Iwai 	if (!info->chmap)
24642d3391ecSTakashi Iwai 		return -EINVAL;
24652d3391ecSTakashi Iwai 	substream = snd_pcm_chmap_substream(info, idx);
24662d3391ecSTakashi Iwai 	if (!substream)
24672d3391ecSTakashi Iwai 		return -ENODEV;
24682d3391ecSTakashi Iwai 	memset(ucontrol->value.integer.value, 0,
2469fbd3eb7fSTakashi Iwai 	       sizeof(long) * info->max_channels);
24702d3391ecSTakashi Iwai 	if (!substream->runtime)
24712d3391ecSTakashi Iwai 		return 0; /* no channels set */
24722d3391ecSTakashi Iwai 	for (map = info->chmap; map->channels; map++) {
24732d3391ecSTakashi Iwai 		int i;
24742d3391ecSTakashi Iwai 		if (map->channels == substream->runtime->channels &&
24752d3391ecSTakashi Iwai 		    valid_chmap_channels(info, map->channels)) {
24762d3391ecSTakashi Iwai 			for (i = 0; i < map->channels; i++)
24772d3391ecSTakashi Iwai 				ucontrol->value.integer.value[i] = map->map[i];
24782d3391ecSTakashi Iwai 			return 0;
24792d3391ecSTakashi Iwai 		}
24802d3391ecSTakashi Iwai 	}
24812d3391ecSTakashi Iwai 	return -EINVAL;
24822d3391ecSTakashi Iwai }
24832d3391ecSTakashi Iwai 
24842d3391ecSTakashi Iwai /* tlv callback for channel map ctl element
24852d3391ecSTakashi Iwai  * expands the pre-defined channel maps in a form of TLV
24862d3391ecSTakashi Iwai  */
pcm_chmap_ctl_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)24872d3391ecSTakashi Iwai static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
24882d3391ecSTakashi Iwai 			     unsigned int size, unsigned int __user *tlv)
24892d3391ecSTakashi Iwai {
24902d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
24912d3391ecSTakashi Iwai 	const struct snd_pcm_chmap_elem *map;
24922d3391ecSTakashi Iwai 	unsigned int __user *dst;
24932d3391ecSTakashi Iwai 	int c, count = 0;
24942d3391ecSTakashi Iwai 
24952deaeaf1STakashi Iwai 	if (!info->chmap)
24962d3391ecSTakashi Iwai 		return -EINVAL;
24972d3391ecSTakashi Iwai 	if (size < 8)
24982d3391ecSTakashi Iwai 		return -ENOMEM;
24992d3391ecSTakashi Iwai 	if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
25002d3391ecSTakashi Iwai 		return -EFAULT;
25012d3391ecSTakashi Iwai 	size -= 8;
25022d3391ecSTakashi Iwai 	dst = tlv + 2;
25032d3391ecSTakashi Iwai 	for (map = info->chmap; map->channels; map++) {
25042d3391ecSTakashi Iwai 		int chs_bytes = map->channels * 4;
25052d3391ecSTakashi Iwai 		if (!valid_chmap_channels(info, map->channels))
25062d3391ecSTakashi Iwai 			continue;
25072d3391ecSTakashi Iwai 		if (size < 8)
25082d3391ecSTakashi Iwai 			return -ENOMEM;
25092d3391ecSTakashi Iwai 		if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
25102d3391ecSTakashi Iwai 		    put_user(chs_bytes, dst + 1))
25112d3391ecSTakashi Iwai 			return -EFAULT;
25122d3391ecSTakashi Iwai 		dst += 2;
25132d3391ecSTakashi Iwai 		size -= 8;
25142d3391ecSTakashi Iwai 		count += 8;
25152d3391ecSTakashi Iwai 		if (size < chs_bytes)
25162d3391ecSTakashi Iwai 			return -ENOMEM;
25172d3391ecSTakashi Iwai 		size -= chs_bytes;
25182d3391ecSTakashi Iwai 		count += chs_bytes;
25192d3391ecSTakashi Iwai 		for (c = 0; c < map->channels; c++) {
25202d3391ecSTakashi Iwai 			if (put_user(map->map[c], dst))
25212d3391ecSTakashi Iwai 				return -EFAULT;
25222d3391ecSTakashi Iwai 			dst++;
25232d3391ecSTakashi Iwai 		}
25242d3391ecSTakashi Iwai 	}
25252d3391ecSTakashi Iwai 	if (put_user(count, tlv + 1))
25262d3391ecSTakashi Iwai 		return -EFAULT;
25272d3391ecSTakashi Iwai 	return 0;
25282d3391ecSTakashi Iwai }
25292d3391ecSTakashi Iwai 
pcm_chmap_ctl_private_free(struct snd_kcontrol * kcontrol)25302d3391ecSTakashi Iwai static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
25312d3391ecSTakashi Iwai {
25322d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
25332d3391ecSTakashi Iwai 	info->pcm->streams[info->stream].chmap_kctl = NULL;
25342d3391ecSTakashi Iwai 	kfree(info);
25352d3391ecSTakashi Iwai }
25362d3391ecSTakashi Iwai 
25372d3391ecSTakashi Iwai /**
25382d3391ecSTakashi Iwai  * snd_pcm_add_chmap_ctls - create channel-mapping control elements
25392d3391ecSTakashi Iwai  * @pcm: the assigned PCM instance
25402d3391ecSTakashi Iwai  * @stream: stream direction
25412d3391ecSTakashi Iwai  * @chmap: channel map elements (for query)
25422d3391ecSTakashi Iwai  * @max_channels: the max number of channels for the stream
25432d3391ecSTakashi Iwai  * @private_value: the value passed to each kcontrol's private_value field
25442d3391ecSTakashi Iwai  * @info_ret: store struct snd_pcm_chmap instance if non-NULL
25452d3391ecSTakashi Iwai  *
25462d3391ecSTakashi Iwai  * Create channel-mapping control elements assigned to the given PCM stream(s).
2547eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error value.
25482d3391ecSTakashi 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)25492d3391ecSTakashi Iwai int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
25502d3391ecSTakashi Iwai 			   const struct snd_pcm_chmap_elem *chmap,
25512d3391ecSTakashi Iwai 			   int max_channels,
25522d3391ecSTakashi Iwai 			   unsigned long private_value,
25532d3391ecSTakashi Iwai 			   struct snd_pcm_chmap **info_ret)
25542d3391ecSTakashi Iwai {
25552d3391ecSTakashi Iwai 	struct snd_pcm_chmap *info;
25562d3391ecSTakashi Iwai 	struct snd_kcontrol_new knew = {
25572d3391ecSTakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
25582d3391ecSTakashi Iwai 		.access = SNDRV_CTL_ELEM_ACCESS_READ |
25592d3391ecSTakashi Iwai 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
25602d3391ecSTakashi Iwai 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
25612d3391ecSTakashi Iwai 		.info = pcm_chmap_ctl_info,
25622d3391ecSTakashi Iwai 		.get = pcm_chmap_ctl_get,
25632d3391ecSTakashi Iwai 		.tlv.c = pcm_chmap_ctl_tlv,
25642d3391ecSTakashi Iwai 	};
25652d3391ecSTakashi Iwai 	int err;
25662d3391ecSTakashi Iwai 
25678d879be8STakashi Iwai 	if (WARN_ON(pcm->streams[stream].chmap_kctl))
25688d879be8STakashi Iwai 		return -EBUSY;
25692d3391ecSTakashi Iwai 	info = kzalloc(sizeof(*info), GFP_KERNEL);
25702d3391ecSTakashi Iwai 	if (!info)
25712d3391ecSTakashi Iwai 		return -ENOMEM;
25722d3391ecSTakashi Iwai 	info->pcm = pcm;
25732d3391ecSTakashi Iwai 	info->stream = stream;
25742d3391ecSTakashi Iwai 	info->chmap = chmap;
25752d3391ecSTakashi Iwai 	info->max_channels = max_channels;
25762d3391ecSTakashi Iwai 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
25772d3391ecSTakashi Iwai 		knew.name = "Playback Channel Map";
25782d3391ecSTakashi Iwai 	else
25792d3391ecSTakashi Iwai 		knew.name = "Capture Channel Map";
25802d3391ecSTakashi Iwai 	knew.device = pcm->device;
25812d3391ecSTakashi Iwai 	knew.count = pcm->streams[stream].substream_count;
25822d3391ecSTakashi Iwai 	knew.private_value = private_value;
25832d3391ecSTakashi Iwai 	info->kctl = snd_ctl_new1(&knew, info);
25842d3391ecSTakashi Iwai 	if (!info->kctl) {
25852d3391ecSTakashi Iwai 		kfree(info);
25862d3391ecSTakashi Iwai 		return -ENOMEM;
25872d3391ecSTakashi Iwai 	}
25882d3391ecSTakashi Iwai 	info->kctl->private_free = pcm_chmap_ctl_private_free;
25892d3391ecSTakashi Iwai 	err = snd_ctl_add(pcm->card, info->kctl);
25902d3391ecSTakashi Iwai 	if (err < 0)
25912d3391ecSTakashi Iwai 		return err;
25922d3391ecSTakashi Iwai 	pcm->streams[stream].chmap_kctl = info->kctl;
25932d3391ecSTakashi Iwai 	if (info_ret)
25942d3391ecSTakashi Iwai 		*info_ret = info;
25952d3391ecSTakashi Iwai 	return 0;
25962d3391ecSTakashi Iwai }
25972d3391ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
2598