blob: 3640269732f5a3b80c1eefd7c4a1665158c54473 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/time.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020025#include <linux/math64.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020029#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/info.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/timer.h>
34
Takashi Iwaif5914902014-11-04 12:45:59 +010035#ifdef CONFIG_SND_PCM_XRUN_DEBUG
36#define CREATE_TRACE_POINTS
37#include "pcm_trace.h"
38#else
39#define trace_hwptr(substream, pos, in_interrupt)
40#define trace_xrun(substream)
41#define trace_hw_ptr_error(substream, reason)
42#endif
43
Banajit Goswami55b24c52016-11-21 19:08:27 -080044#define STRING_LENGTH_OF_INT 12
Banajit Goswamicec03b62016-11-21 19:22:56 -080045#define MAX_USR_CTRL_CNT 128
Banajit Goswami55b24c52016-11-21 19:08:27 -080046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * fill ring buffer with silence
49 * runtime->silence_start: starting pointer to silence area
50 * runtime->silence_filled: size filled with silence
51 * runtime->silence_threshold: threshold from application
52 * runtime->silence_size: maximal size from application
53 *
54 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
55 */
Takashi Iwai877211f2005-11-17 13:59:38 +010056void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Takashi Iwai877211f2005-11-17 13:59:38 +010058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 snd_pcm_uframes_t frames, ofs, transfer;
60
61 if (runtime->silence_size < runtime->boundary) {
62 snd_pcm_sframes_t noise_dist, n;
63 if (runtime->silence_start != runtime->control->appl_ptr) {
64 n = runtime->control->appl_ptr - runtime->silence_start;
65 if (n < 0)
66 n += runtime->boundary;
67 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
68 runtime->silence_filled -= n;
69 else
70 runtime->silence_filled = 0;
71 runtime->silence_start = runtime->control->appl_ptr;
72 }
Takashi Iwai235475c2005-12-07 15:28:07 +010073 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
76 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
77 return;
78 frames = runtime->silence_threshold - noise_dist;
79 if (frames > runtime->silence_size)
80 frames = runtime->silence_size;
81 } else {
82 if (new_hw_ptr == ULONG_MAX) { /* initialization */
83 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020084 if (avail > runtime->buffer_size)
85 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 runtime->silence_filled = avail > 0 ? avail : 0;
87 runtime->silence_start = (runtime->status->hw_ptr +
88 runtime->silence_filled) %
89 runtime->boundary;
90 } else {
91 ofs = runtime->status->hw_ptr;
92 frames = new_hw_ptr - ofs;
93 if ((snd_pcm_sframes_t)frames < 0)
94 frames += runtime->boundary;
95 runtime->silence_filled -= frames;
96 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
97 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020098 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200100 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 frames = runtime->buffer_size - runtime->silence_filled;
104 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200105 if (snd_BUG_ON(frames > runtime->buffer_size))
106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (frames == 0)
108 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200109 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 while (frames > 0) {
111 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
112 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
113 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
114 if (substream->ops->silence) {
115 int err;
116 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200117 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } else {
119 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
120 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
121 }
122 } else {
123 unsigned int c;
124 unsigned int channels = runtime->channels;
125 if (substream->ops->silence) {
126 for (c = 0; c < channels; ++c) {
127 int err;
128 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200129 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 } else {
132 size_t dma_csize = runtime->dma_bytes / channels;
133 for (c = 0; c < channels; ++c) {
134 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
135 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
136 }
137 }
138 }
139 runtime->silence_filled += transfer;
140 frames -= transfer;
141 ofs = 0;
142 }
143}
144
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200145#ifdef CONFIG_SND_DEBUG
146void snd_pcm_debug_name(struct snd_pcm_substream *substream,
Takashi Iwaic0070112009-06-08 15:58:48 +0200147 char *name, size_t len)
148{
149 snprintf(name, len, "pcmC%dD%d%c:%d",
150 substream->pcm->card->number,
151 substream->pcm->device,
152 substream->stream ? 'c' : 'p',
153 substream->number);
154}
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200155EXPORT_SYMBOL(snd_pcm_debug_name);
156#endif
Takashi Iwaic0070112009-06-08 15:58:48 +0200157
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100158#define XRUN_DEBUG_BASIC (1<<0)
159#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
160#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100161
162#ifdef CONFIG_SND_PCM_XRUN_DEBUG
163
164#define xrun_debug(substream, mask) \
165 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200166#else
167#define xrun_debug(substream, mask) 0
168#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100169
170#define dump_stack_on_xrun(substream) do { \
171 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
172 dump_stack(); \
173 } while (0)
174
Takashi Iwai877211f2005-11-17 13:59:38 +0100175static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200177 struct snd_pcm_runtime *runtime = substream->runtime;
178
Takashi Iwaif5914902014-11-04 12:45:59 +0100179 trace_xrun(substream);
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200180 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
181 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100183 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200184 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200185 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100186 pcm_warn(substream->pcm, "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100187 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Jarkko Nikula0f170142010-03-26 16:07:25 +0200191#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwaif5914902014-11-04 12:45:59 +0100192#define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100193 do { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100194 trace_hw_ptr_error(substream, reason); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100195 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100196 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
197 (in_interrupt) ? 'Q' : 'P', ##args); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100198 dump_stack_on_xrun(substream); \
199 } \
200 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100202#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
203
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100204#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100205
206#endif
207
Jaroslav Kysela12509322010-01-07 15:36:31 +0100208int snd_pcm_update_state(struct snd_pcm_substream *substream,
209 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 snd_pcm_uframes_t avail;
212
213 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
214 avail = snd_pcm_playback_avail(runtime);
215 else
216 avail = snd_pcm_capture_avail(runtime);
217 if (avail > runtime->avail_max)
218 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200219 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
220 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200222 return -EPIPE;
223 }
224 } else {
225 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200227 return -EPIPE;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
David Dillow5daeba32010-06-27 00:13:20 +0200230 if (runtime->twake) {
231 if (avail >= runtime->twake)
232 wake_up(&runtime->tsleep);
233 } else if (avail >= runtime->control->avail_min)
234 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600238static void update_audio_tstamp(struct snd_pcm_substream *substream,
239 struct timespec *curr_tstamp,
240 struct timespec *audio_tstamp)
241{
242 struct snd_pcm_runtime *runtime = substream->runtime;
243 u64 audio_frames, audio_nsecs;
244 struct timespec driver_tstamp;
245
246 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
247 return;
248
249 if (!(substream->ops->get_time_info) ||
250 (runtime->audio_tstamp_report.actual_type ==
251 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
252
253 /*
254 * provide audio timestamp derived from pointer position
255 * add delay only if requested
256 */
257
258 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
259
260 if (runtime->audio_tstamp_config.report_delay) {
261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
262 audio_frames -= runtime->delay;
263 else
264 audio_frames += runtime->delay;
265 }
266 audio_nsecs = div_u64(audio_frames * 1000000000LL,
267 runtime->rate);
268 *audio_tstamp = ns_to_timespec(audio_nsecs);
269 }
Henrik Eriksson14eb4542017-11-21 09:29:28 +0100270 if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
271 runtime->status->audio_tstamp = *audio_tstamp;
272 runtime->status->tstamp = *curr_tstamp;
273 }
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600274
275 /*
276 * re-take a driver timestamp to let apps detect if the reference tstamp
277 * read by low-level hardware was provided with a delay
278 */
279 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
280 runtime->driver_tstamp = driver_tstamp;
281}
282
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100283static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
284 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Takashi Iwai877211f2005-11-17 13:59:38 +0100286 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100288 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200289 snd_pcm_sframes_t hdelta, delta;
290 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500291 unsigned long curr_jiffies;
292 struct timespec curr_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500293 struct timespec audio_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500294 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200296 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500297
298 /*
299 * group pointer, time and jiffies reads to allow for more
300 * accurate correlations/corrections.
301 * The values are stored at the end of this routine after
302 * corrections for hw_ptr position
303 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100304 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500305 curr_jiffies = jiffies;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500306 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600307 if ((substream->ops->get_time_info) &&
308 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
309 substream->ops->get_time_info(substream, &curr_tstamp,
310 &audio_tstamp,
311 &runtime->audio_tstamp_config,
312 &runtime->audio_tstamp_report);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500313
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600314 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
315 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
316 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
317 } else
318 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (pos == SNDRV_PCM_POS_XRUN) {
322 xrun(substream);
323 return -EPIPE;
324 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100325 if (pos >= runtime->buffer_size) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100326 if (printk_ratelimit()) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100327 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200328 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 pcm_err(substream->pcm,
Takashi Iwai0ab1ace2016-03-10 20:56:20 +0100330 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
Takashi Iwai09e56df2014-02-04 18:19:48 +0100331 name, pos, runtime->buffer_size,
332 runtime->period_size);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100333 }
334 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200335 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100336 pos -= pos % runtime->min_align;
Takashi Iwaif5914902014-11-04 12:45:59 +0100337 trace_hwptr(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100338 hw_base = runtime->hw_ptr_base;
339 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100340 if (in_interrupt) {
341 /* we know that one period was processed */
342 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100343 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100344 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200345 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500346 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Koro Chen13a98832015-05-13 22:39:03 +0800347 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200348 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500349 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200350 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500351 crossed_boundary++;
352 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200353 new_hw_ptr = hw_base + pos;
354 goto __delta;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100358 /* new_hw_ptr might be lower than old_hw_ptr in case when */
359 /* pointer crosses the end of the ring buffer */
360 if (new_hw_ptr < old_hw_ptr) {
361 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500362 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100363 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500364 crossed_boundary++;
365 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100366 new_hw_ptr = hw_base + pos;
367 }
368 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200369 delta = new_hw_ptr - old_hw_ptr;
370 if (delta < 0)
371 delta += runtime->boundary;
Clemens Ladischab69a492010-11-15 10:46:23 +0100372
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100373 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200374 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100375 /*
376 * Without regular period interrupts, we have to check
377 * the elapsed time to detect xruns.
378 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500379 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Preetam Singh Ranawate12ae3c2017-01-17 16:24:40 +0530380 if ((jdelta < runtime->hw_ptr_buffer_jiffies / 2) ||
381 (runtime->hw_ptr_buffer_jiffies <= 0))
Clemens Ladisch47228e42010-11-18 09:53:07 +0100382 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100383 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200384 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
385 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100386 delta += runtime->buffer_size;
387 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500388 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100389 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500390 crossed_boundary++;
391 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100392 new_hw_ptr = hw_base + pos;
393 hdelta -= runtime->hw_ptr_buffer_jiffies;
394 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100395 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100396 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100397
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100398 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100399 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100400 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
401 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
402 substream->stream, (long)pos,
403 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100404 return 0;
405 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200406
407 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100408 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200409 goto no_jiffies_check;
410
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200411 /* Skip the jiffies check for hardwares with BATCH flag.
412 * Such hardware usually just increases the position at each IRQ,
413 * thus it can't give any strange position.
414 */
415 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
416 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100417 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200418 if (hdelta < runtime->delay)
419 goto no_jiffies_check;
420 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500421 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200422 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
423 delta = jdelta /
424 (((runtime->period_size * HZ) / runtime->rate)
425 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100426 /* move new_hw_ptr according jiffies not pos variable */
427 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100428 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100429 /* use loop to avoid checks for delta overflows */
430 /* the delta value is small or zero in most cases */
431 while (delta > 0) {
432 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500433 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100434 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500435 crossed_boundary--;
436 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100437 delta--;
438 }
439 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100440 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
441 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200442 (long)pos, (long)hdelta,
443 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100444 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100445 (unsigned long)old_hw_ptr,
446 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100447 /* reset values to proper state */
448 delta = 0;
449 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200450 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200451 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200452 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100453 hw_ptr_error(substream, in_interrupt,
454 "Lost interrupts?",
455 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100456 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100457 (long)new_hw_ptr,
458 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100459 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100460
Clemens Ladischab69a492010-11-15 10:46:23 +0100461 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600462 if (runtime->status->hw_ptr == new_hw_ptr) {
463 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100464 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600465 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
468 runtime->silence_size > 0)
469 snd_pcm_playback_silence(substream, new_hw_ptr);
470
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100471 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200472 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
473 if (delta < 0)
474 delta += runtime->boundary;
475 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
476 runtime->hw_ptr_interrupt += delta;
477 if (runtime->hw_ptr_interrupt >= runtime->boundary)
478 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100479 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100480 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500482 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500483 if (crossed_boundary) {
484 snd_BUG_ON(crossed_boundary != 1);
485 runtime->hw_ptr_wrap += runtime->boundary;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600488 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500489
Jaroslav Kysela12509322010-01-07 15:36:31 +0100490 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100494int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100496 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * snd_pcm_set_ops - set the PCM operators
501 * @pcm: the pcm instance
502 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
503 * @ops: the operator table
504 *
505 * Sets the given PCM operators to the pcm instance.
506 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200507void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
508 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Takashi Iwai877211f2005-11-17 13:59:38 +0100510 struct snd_pcm_str *stream = &pcm->streams[direction];
511 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 for (substream = stream->substream; substream != NULL; substream = substream->next)
514 substream->ops = ops;
515}
516
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200517EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/**
520 * snd_pcm_sync - set the PCM sync id
521 * @substream: the pcm substream
522 *
523 * Sets the PCM sync identifier for the card.
524 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100525void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwai877211f2005-11-17 13:59:38 +0100527 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 runtime->sync.id32[0] = substream->pcm->card->number;
530 runtime->sync.id32[1] = -1;
531 runtime->sync.id32[2] = -1;
532 runtime->sync.id32[3] = -1;
533}
534
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200535EXPORT_SYMBOL(snd_pcm_set_sync);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Standard ioctl routine
539 */
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static inline unsigned int div32(unsigned int a, unsigned int b,
542 unsigned int *r)
543{
544 if (b == 0) {
545 *r = 0;
546 return UINT_MAX;
547 }
548 *r = a % b;
549 return a / b;
550}
551
552static inline unsigned int div_down(unsigned int a, unsigned int b)
553{
554 if (b == 0)
555 return UINT_MAX;
556 return a / b;
557}
558
559static inline unsigned int div_up(unsigned int a, unsigned int b)
560{
561 unsigned int r;
562 unsigned int q;
563 if (b == 0)
564 return UINT_MAX;
565 q = div32(a, b, &r);
566 if (r)
567 ++q;
568 return q;
569}
570
571static inline unsigned int mul(unsigned int a, unsigned int b)
572{
573 if (a == 0)
574 return 0;
575 if (div_down(UINT_MAX, a) < b)
576 return UINT_MAX;
577 return a * b;
578}
579
580static inline unsigned int muldiv32(unsigned int a, unsigned int b,
581 unsigned int c, unsigned int *r)
582{
583 u_int64_t n = (u_int64_t) a * b;
584 if (c == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *r = 0;
586 return UINT_MAX;
587 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200588 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (n >= UINT_MAX) {
590 *r = 0;
591 return UINT_MAX;
592 }
593 return n;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
597 * snd_interval_refine - refine the interval value of configurator
598 * @i: the interval value to refine
599 * @v: the interval value to refer to
600 *
601 * Refines the interval value with the reference value.
602 * The interval is changed to the range satisfying both intervals.
603 * The interval status (min, max, integer, etc.) are evaluated.
604 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100605 * Return: Positive if the value is changed, zero if it's not changed, or a
606 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100608int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200611 if (snd_BUG_ON(snd_interval_empty(i)))
612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (i->min < v->min) {
614 i->min = v->min;
615 i->openmin = v->openmin;
616 changed = 1;
617 } else if (i->min == v->min && !i->openmin && v->openmin) {
618 i->openmin = 1;
619 changed = 1;
620 }
621 if (i->max > v->max) {
622 i->max = v->max;
623 i->openmax = v->openmax;
624 changed = 1;
625 } else if (i->max == v->max && !i->openmax && v->openmax) {
626 i->openmax = 1;
627 changed = 1;
628 }
629 if (!i->integer && v->integer) {
630 i->integer = 1;
631 changed = 1;
632 }
633 if (i->integer) {
634 if (i->openmin) {
635 i->min++;
636 i->openmin = 0;
637 }
638 if (i->openmax) {
639 i->max--;
640 i->openmax = 0;
641 }
642 } else if (!i->openmin && !i->openmax && i->min == i->max)
643 i->integer = 1;
644 if (snd_interval_checkempty(i)) {
645 snd_interval_none(i);
646 return -EINVAL;
647 }
648 return changed;
649}
650
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200651EXPORT_SYMBOL(snd_interval_refine);
652
Takashi Iwai877211f2005-11-17 13:59:38 +0100653static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200655 const unsigned int last_max = i->max;
656
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200657 if (snd_BUG_ON(snd_interval_empty(i)))
658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (snd_interval_single(i))
660 return 0;
661 i->max = i->min;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200662 if (i->openmin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 i->max++;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200664 /* only exclude max value if also excluded before refine */
665 i->openmax = (i->openmax && i->max >= last_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 1;
667}
668
Takashi Iwai877211f2005-11-17 13:59:38 +0100669static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200671 const unsigned int last_min = i->min;
672
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200673 if (snd_BUG_ON(snd_interval_empty(i)))
674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (snd_interval_single(i))
676 return 0;
677 i->min = i->max;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200678 if (i->openmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 i->min--;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200680 /* only exclude min value if also excluded before refine */
681 i->openmin = (i->openmin && i->min <= last_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
683}
684
Takashi Iwai877211f2005-11-17 13:59:38 +0100685void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 if (a->empty || b->empty) {
688 snd_interval_none(c);
689 return;
690 }
691 c->empty = 0;
692 c->min = mul(a->min, b->min);
693 c->openmin = (a->openmin || b->openmin);
694 c->max = mul(a->max, b->max);
695 c->openmax = (a->openmax || b->openmax);
696 c->integer = (a->integer && b->integer);
697}
698
699/**
700 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200701 * @a: dividend
702 * @b: divisor
703 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
705 * c = a / b
706 *
707 * Returns non-zero if the value is changed, zero if not changed.
708 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100709void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 unsigned int r;
712 if (a->empty || b->empty) {
713 snd_interval_none(c);
714 return;
715 }
716 c->empty = 0;
717 c->min = div32(a->min, b->max, &r);
718 c->openmin = (r || a->openmin || b->openmax);
719 if (b->min > 0) {
720 c->max = div32(a->max, b->min, &r);
721 if (r) {
722 c->max++;
723 c->openmax = 1;
724 } else
725 c->openmax = (a->openmax || b->openmin);
726 } else {
727 c->max = UINT_MAX;
728 c->openmax = 0;
729 }
730 c->integer = 0;
731}
732
733/**
734 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200735 * @a: dividend 1
736 * @b: dividend 2
737 * @k: divisor (as integer)
738 * @c: result
739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * c = a * b / k
741 *
742 * Returns non-zero if the value is changed, zero if not changed.
743 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100744void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
745 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 unsigned int r;
748 if (a->empty || b->empty) {
749 snd_interval_none(c);
750 return;
751 }
752 c->empty = 0;
753 c->min = muldiv32(a->min, b->min, k, &r);
754 c->openmin = (r || a->openmin || b->openmin);
755 c->max = muldiv32(a->max, b->max, k, &r);
756 if (r) {
757 c->max++;
758 c->openmax = 1;
759 } else
760 c->openmax = (a->openmax || b->openmax);
761 c->integer = 0;
762}
763
764/**
765 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200766 * @a: dividend 1
767 * @k: dividend 2 (as integer)
768 * @b: divisor
769 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
771 * c = a * k / b
772 *
773 * Returns non-zero if the value is changed, zero if not changed.
774 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100775void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
776 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 unsigned int r;
779 if (a->empty || b->empty) {
780 snd_interval_none(c);
781 return;
782 }
783 c->empty = 0;
784 c->min = muldiv32(a->min, k, b->max, &r);
785 c->openmin = (r || a->openmin || b->openmax);
786 if (b->min > 0) {
787 c->max = muldiv32(a->max, k, b->min, &r);
788 if (r) {
789 c->max++;
790 c->openmax = 1;
791 } else
792 c->openmax = (a->openmax || b->openmin);
793 } else {
794 c->max = UINT_MAX;
795 c->openmax = 0;
796 }
797 c->integer = 0;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/* ---- */
801
802
803/**
804 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200805 * @i: interval to refine
806 * @rats_count: number of ratnum_t
807 * @rats: ratnum_t array
808 * @nump: pointer to store the resultant numerator
809 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100811 * Return: Positive if the value is changed, zero if it's not changed, or a
812 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100814int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100815 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100816 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100818 unsigned int best_num, best_den;
819 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100821 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100823 unsigned int result_num, result_den;
824 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 best_num = best_den = best_diff = 0;
827 for (k = 0; k < rats_count; ++k) {
828 unsigned int num = rats[k].num;
829 unsigned int den;
830 unsigned int q = i->min;
831 int diff;
832 if (q == 0)
833 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100834 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (den < rats[k].den_min)
836 continue;
837 if (den > rats[k].den_max)
838 den = rats[k].den_max;
839 else {
840 unsigned int r;
841 r = (den - rats[k].den_min) % rats[k].den_step;
842 if (r != 0)
843 den -= r;
844 }
845 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100846 if (diff < 0)
847 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (best_num == 0 ||
849 diff * best_den < best_diff * den) {
850 best_diff = diff;
851 best_den = den;
852 best_num = num;
853 }
854 }
855 if (best_den == 0) {
856 i->empty = 1;
857 return -EINVAL;
858 }
859 t.min = div_down(best_num, best_den);
860 t.openmin = !!(best_num % best_den);
861
Krzysztof Helt8374e242009-12-21 17:07:08 +0100862 result_num = best_num;
863 result_diff = best_diff;
864 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 best_num = best_den = best_diff = 0;
866 for (k = 0; k < rats_count; ++k) {
867 unsigned int num = rats[k].num;
868 unsigned int den;
869 unsigned int q = i->max;
870 int diff;
871 if (q == 0) {
872 i->empty = 1;
873 return -EINVAL;
874 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100875 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (den > rats[k].den_max)
877 continue;
878 if (den < rats[k].den_min)
879 den = rats[k].den_min;
880 else {
881 unsigned int r;
882 r = (den - rats[k].den_min) % rats[k].den_step;
883 if (r != 0)
884 den += rats[k].den_step - r;
885 }
886 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100887 if (diff < 0)
888 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (best_num == 0 ||
890 diff * best_den < best_diff * den) {
891 best_diff = diff;
892 best_den = den;
893 best_num = num;
894 }
895 }
896 if (best_den == 0) {
897 i->empty = 1;
898 return -EINVAL;
899 }
900 t.max = div_up(best_num, best_den);
901 t.openmax = !!(best_num % best_den);
902 t.integer = 0;
903 err = snd_interval_refine(i, &t);
904 if (err < 0)
905 return err;
906
907 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100908 if (best_diff * result_den < result_diff * best_den) {
909 result_num = best_num;
910 result_den = best_den;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100913 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100915 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 return err;
918}
919
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200920EXPORT_SYMBOL(snd_interval_ratnum);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/**
923 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200924 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 * @rats_count: number of struct ratden
926 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200927 * @nump: pointer to store the resultant numerator
928 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100930 * Return: Positive if the value is changed, zero if it's not changed, or a
931 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100933static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100934 unsigned int rats_count,
935 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned int *nump, unsigned int *denp)
937{
938 unsigned int best_num, best_diff, best_den;
939 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100940 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int err;
942
943 best_num = best_den = best_diff = 0;
944 for (k = 0; k < rats_count; ++k) {
945 unsigned int num;
946 unsigned int den = rats[k].den;
947 unsigned int q = i->min;
948 int diff;
949 num = mul(q, den);
950 if (num > rats[k].num_max)
951 continue;
952 if (num < rats[k].num_min)
953 num = rats[k].num_max;
954 else {
955 unsigned int r;
956 r = (num - rats[k].num_min) % rats[k].num_step;
957 if (r != 0)
958 num += rats[k].num_step - r;
959 }
960 diff = num - q * den;
961 if (best_num == 0 ||
962 diff * best_den < best_diff * den) {
963 best_diff = diff;
964 best_den = den;
965 best_num = num;
966 }
967 }
968 if (best_den == 0) {
969 i->empty = 1;
970 return -EINVAL;
971 }
972 t.min = div_down(best_num, best_den);
973 t.openmin = !!(best_num % best_den);
974
975 best_num = best_den = best_diff = 0;
976 for (k = 0; k < rats_count; ++k) {
977 unsigned int num;
978 unsigned int den = rats[k].den;
979 unsigned int q = i->max;
980 int diff;
981 num = mul(q, den);
982 if (num < rats[k].num_min)
983 continue;
984 if (num > rats[k].num_max)
985 num = rats[k].num_max;
986 else {
987 unsigned int r;
988 r = (num - rats[k].num_min) % rats[k].num_step;
989 if (r != 0)
990 num -= r;
991 }
992 diff = q * den - num;
993 if (best_num == 0 ||
994 diff * best_den < best_diff * den) {
995 best_diff = diff;
996 best_den = den;
997 best_num = num;
998 }
999 }
1000 if (best_den == 0) {
1001 i->empty = 1;
1002 return -EINVAL;
1003 }
1004 t.max = div_up(best_num, best_den);
1005 t.openmax = !!(best_num % best_den);
1006 t.integer = 0;
1007 err = snd_interval_refine(i, &t);
1008 if (err < 0)
1009 return err;
1010
1011 if (snd_interval_single(i)) {
1012 if (nump)
1013 *nump = best_num;
1014 if (denp)
1015 *denp = best_den;
1016 }
1017 return err;
1018}
1019
1020/**
1021 * snd_interval_list - refine the interval value from the list
1022 * @i: the interval value to refine
1023 * @count: the number of elements in the list
1024 * @list: the value list
1025 * @mask: the bit-mask to evaluate
1026 *
1027 * Refines the interval value from the list.
1028 * When mask is non-zero, only the elements corresponding to bit 1 are
1029 * evaluated.
1030 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001031 * Return: Positive if the value is changed, zero if it's not changed, or a
1032 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Mark Brown4af87a92012-03-14 19:48:43 +00001034int snd_interval_list(struct snd_interval *i, unsigned int count,
1035 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001038 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001039
1040 if (!count) {
1041 i->empty = 1;
1042 return -EINVAL;
1043 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001044 snd_interval_any(&list_range);
1045 list_range.min = UINT_MAX;
1046 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 for (k = 0; k < count; k++) {
1048 if (mask && !(mask & (1 << k)))
1049 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001050 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001052 list_range.min = min(list_range.min, list[k]);
1053 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001055 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001058EXPORT_SYMBOL(snd_interval_list);
1059
Peter Rosinf66f8982015-01-28 15:16:06 +01001060/**
1061 * snd_interval_ranges - refine the interval value from the list of ranges
1062 * @i: the interval value to refine
1063 * @count: the number of elements in the list of ranges
1064 * @ranges: the ranges list
1065 * @mask: the bit-mask to evaluate
1066 *
1067 * Refines the interval value from the list of ranges.
1068 * When mask is non-zero, only the elements corresponding to bit 1 are
1069 * evaluated.
1070 *
1071 * Return: Positive if the value is changed, zero if it's not changed, or a
1072 * negative error code.
1073 */
1074int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1075 const struct snd_interval *ranges, unsigned int mask)
1076{
1077 unsigned int k;
1078 struct snd_interval range_union;
1079 struct snd_interval range;
1080
1081 if (!count) {
1082 snd_interval_none(i);
1083 return -EINVAL;
1084 }
1085 snd_interval_any(&range_union);
1086 range_union.min = UINT_MAX;
1087 range_union.max = 0;
1088 for (k = 0; k < count; k++) {
1089 if (mask && !(mask & (1 << k)))
1090 continue;
1091 snd_interval_copy(&range, &ranges[k]);
1092 if (snd_interval_refine(&range, i) < 0)
1093 continue;
1094 if (snd_interval_empty(&range))
1095 continue;
1096
1097 if (range.min < range_union.min) {
1098 range_union.min = range.min;
1099 range_union.openmin = 1;
1100 }
1101 if (range.min == range_union.min && !range.openmin)
1102 range_union.openmin = 0;
1103 if (range.max > range_union.max) {
1104 range_union.max = range.max;
1105 range_union.openmax = 1;
1106 }
1107 if (range.max == range_union.max && !range.openmax)
1108 range_union.openmax = 0;
1109 }
1110 return snd_interval_refine(i, &range_union);
1111}
1112EXPORT_SYMBOL(snd_interval_ranges);
1113
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001114static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 unsigned int n;
1117 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001118 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (n != 0 || i->openmin) {
1120 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001121 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 changed = 1;
1123 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001124 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (n != 0 || i->openmax) {
1126 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001127 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 changed = 1;
1129 }
1130 if (snd_interval_checkempty(i)) {
1131 i->empty = 1;
1132 return -EINVAL;
1133 }
1134 return changed;
1135}
1136
1137/* Info constraints helpers */
1138
1139/**
1140 * snd_pcm_hw_rule_add - add the hw-constraint rule
1141 * @runtime: the pcm runtime instance
1142 * @cond: condition bits
1143 * @var: the variable to evaluate
1144 * @func: the evaluation function
1145 * @private: the private data pointer passed to function
1146 * @dep: the dependent variables
1147 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001148 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001150int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int var,
1152 snd_pcm_hw_rule_func_t func, void *private,
1153 int dep, ...)
1154{
Takashi Iwai877211f2005-11-17 13:59:38 +01001155 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1156 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 unsigned int k;
1158 va_list args;
1159 va_start(args, dep);
1160 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001161 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 unsigned int new_rules = constrs->rules_all + 16;
1163 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001164 if (!new) {
1165 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (constrs->rules) {
1169 memcpy(new, constrs->rules,
1170 constrs->rules_num * sizeof(*c));
1171 kfree(constrs->rules);
1172 }
1173 constrs->rules = new;
1174 constrs->rules_all = new_rules;
1175 }
1176 c = &constrs->rules[constrs->rules_num];
1177 c->cond = cond;
1178 c->func = func;
1179 c->var = var;
1180 c->private = private;
1181 k = 0;
1182 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001183 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1184 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001185 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 c->deps[k++] = dep;
1188 if (dep < 0)
1189 break;
1190 dep = va_arg(args, int);
1191 }
1192 constrs->rules_num++;
1193 va_end(args);
1194 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001197EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001200 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001201 * @runtime: PCM runtime instance
1202 * @var: hw_params variable to apply the mask
1203 * @mask: the bitmap mask
1204 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001205 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001206 *
1207 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001209int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 u_int32_t mask)
1211{
Takashi Iwai877211f2005-11-17 13:59:38 +01001212 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1213 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *maskp->bits &= mask;
1215 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1216 if (*maskp->bits == 0)
1217 return -EINVAL;
1218 return 0;
1219}
1220
1221/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001222 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001223 * @runtime: PCM runtime instance
1224 * @var: hw_params variable to apply the mask
1225 * @mask: the 64bit bitmap mask
1226 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001227 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001228 *
1229 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001231int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 u_int64_t mask)
1233{
Takashi Iwai877211f2005-11-17 13:59:38 +01001234 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1235 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 maskp->bits[0] &= (u_int32_t)mask;
1237 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1238 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1239 if (! maskp->bits[0] && ! maskp->bits[1])
1240 return -EINVAL;
1241 return 0;
1242}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001243EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001246 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001247 * @runtime: PCM runtime instance
1248 * @var: hw_params variable to apply the integer constraint
1249 *
1250 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001251 *
1252 * Return: Positive if the value is changed, zero if it's not changed, or a
1253 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001255int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Takashi Iwai877211f2005-11-17 13:59:38 +01001257 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return snd_interval_setinteger(constrs_interval(constrs, var));
1259}
1260
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001261EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001264 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001265 * @runtime: PCM runtime instance
1266 * @var: hw_params variable to apply the range
1267 * @min: the minimal value
1268 * @max: the maximal value
1269 *
1270 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001271 *
1272 * Return: Positive if the value is changed, zero if it's not changed, or a
1273 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001275int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned int min, unsigned int max)
1277{
Takashi Iwai877211f2005-11-17 13:59:38 +01001278 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1279 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 t.min = min;
1281 t.max = max;
1282 t.openmin = t.openmax = 0;
1283 t.integer = 0;
1284 return snd_interval_refine(constrs_interval(constrs, var), &t);
1285}
1286
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001287EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1288
Takashi Iwai877211f2005-11-17 13:59:38 +01001289static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1290 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwai877211f2005-11-17 13:59:38 +01001292 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1294}
1295
1296
1297/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001298 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001299 * @runtime: PCM runtime instance
1300 * @cond: condition bits
1301 * @var: hw_params variable to apply the list constraint
1302 * @l: list
1303 *
1304 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001305 *
1306 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001308int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned int cond,
1310 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001311 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001314 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 var, -1);
1316}
1317
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001318EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1319
Peter Rosinf66f8982015-01-28 15:16:06 +01001320static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1321 struct snd_pcm_hw_rule *rule)
1322{
1323 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1324 return snd_interval_ranges(hw_param_interval(params, rule->var),
1325 r->count, r->ranges, r->mask);
1326}
1327
1328
1329/**
1330 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1331 * @runtime: PCM runtime instance
1332 * @cond: condition bits
1333 * @var: hw_params variable to apply the list of range constraints
1334 * @r: ranges
1335 *
1336 * Apply the list of range constraints to an interval parameter.
1337 *
1338 * Return: Zero if successful, or a negative error code on failure.
1339 */
1340int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1341 unsigned int cond,
1342 snd_pcm_hw_param_t var,
1343 const struct snd_pcm_hw_constraint_ranges *r)
1344{
1345 return snd_pcm_hw_rule_add(runtime, cond, var,
1346 snd_pcm_hw_rule_ranges, (void *)r,
1347 var, -1);
1348}
1349EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1352 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001354 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned int num = 0, den = 0;
1356 int err;
1357 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1358 r->nrats, r->rats, &num, &den);
1359 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1360 params->rate_num = num;
1361 params->rate_den = den;
1362 }
1363 return err;
1364}
1365
1366/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001367 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001368 * @runtime: PCM runtime instance
1369 * @cond: condition bits
1370 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001371 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001372 *
1373 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001375int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned int cond,
1377 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001378 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001381 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 var, -1);
1383}
1384
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001385EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1386
Takashi Iwai877211f2005-11-17 13:59:38 +01001387static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1388 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001390 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 unsigned int num = 0, den = 0;
1392 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1393 r->nrats, r->rats, &num, &den);
1394 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1395 params->rate_num = num;
1396 params->rate_den = den;
1397 }
1398 return err;
1399}
1400
1401/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001402 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001403 * @runtime: PCM runtime instance
1404 * @cond: condition bits
1405 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001406 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001407 *
1408 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001410int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 unsigned int cond,
1412 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001413 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001416 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 var, -1);
1418}
1419
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001420EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1421
Takashi Iwai877211f2005-11-17 13:59:38 +01001422static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1423 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 unsigned int l = (unsigned long) rule->private;
1426 int width = l & 0xffff;
1427 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001428 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001429
1430 if (!snd_interval_single(i))
1431 return 0;
1432
1433 if ((snd_interval_value(i) == width) ||
1434 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001435 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438}
1439
1440/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001441 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001442 * @runtime: PCM runtime instance
1443 * @cond: condition bits
1444 * @width: sample bits width
1445 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001446 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001447 * This constraint will set the number of most significant bits (msbits) if a
1448 * sample format with the specified width has been select. If width is set to 0
1449 * the msbits will be set for any sample format with a width larger than the
1450 * specified msbits.
1451 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001452 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001454int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 unsigned int cond,
1456 unsigned int width,
1457 unsigned int msbits)
1458{
1459 unsigned long l = (msbits << 16) | width;
1460 return snd_pcm_hw_rule_add(runtime, cond, -1,
1461 snd_pcm_hw_rule_msbits,
1462 (void*) l,
1463 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1464}
1465
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001466EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1467
Takashi Iwai877211f2005-11-17 13:59:38 +01001468static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1469 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001472 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001476 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001477 * @runtime: PCM runtime instance
1478 * @cond: condition bits
1479 * @var: hw_params variable to apply the step constraint
1480 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001481 *
1482 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001484int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 unsigned int cond,
1486 snd_pcm_hw_param_t var,
1487 unsigned long step)
1488{
1489 return snd_pcm_hw_rule_add(runtime, cond, var,
1490 snd_pcm_hw_rule_step, (void *) step,
1491 var, -1);
1492}
1493
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001494EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1495
Takashi Iwai877211f2005-11-17 13:59:38 +01001496static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001498 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1500 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1501 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1502 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1503 };
1504 return snd_interval_list(hw_param_interval(params, rule->var),
1505 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1506}
1507
1508/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001509 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001510 * @runtime: PCM runtime instance
1511 * @cond: condition bits
1512 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001513 *
1514 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001516int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 unsigned int cond,
1518 snd_pcm_hw_param_t var)
1519{
1520 return snd_pcm_hw_rule_add(runtime, cond, var,
1521 snd_pcm_hw_rule_pow2, NULL,
1522 var, -1);
1523}
1524
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001525EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1526
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001527static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1528 struct snd_pcm_hw_rule *rule)
1529{
1530 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1531 struct snd_interval *rate;
1532
1533 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1534 return snd_interval_list(rate, 1, &base_rate, 0);
1535}
1536
1537/**
1538 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1539 * @runtime: PCM runtime instance
1540 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001541 *
1542 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001543 */
1544int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1545 unsigned int base_rate)
1546{
1547 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1548 SNDRV_PCM_HW_PARAM_RATE,
1549 snd_pcm_hw_rule_noresample_func,
1550 (void *)(uintptr_t)base_rate,
1551 SNDRV_PCM_HW_PARAM_RATE, -1);
1552}
1553EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1554
Takashi Iwai877211f2005-11-17 13:59:38 +01001555static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001556 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 if (hw_is_mask(var)) {
1559 snd_mask_any(hw_param_mask(params, var));
1560 params->cmask |= 1 << var;
1561 params->rmask |= 1 << var;
1562 return;
1563 }
1564 if (hw_is_interval(var)) {
1565 snd_interval_any(hw_param_interval(params, var));
1566 params->cmask |= 1 << var;
1567 params->rmask |= 1 << var;
1568 return;
1569 }
1570 snd_BUG();
1571}
1572
Takashi Iwai877211f2005-11-17 13:59:38 +01001573void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 unsigned int k;
1576 memset(params, 0, sizeof(*params));
1577 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1578 _snd_pcm_hw_param_any(params, k);
1579 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1580 _snd_pcm_hw_param_any(params, k);
1581 params->info = ~0U;
1582}
1583
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001584EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001587 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001588 * @params: the hw_params instance
1589 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001590 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001592 * Return: The value for field @var if it's fixed in configuration space
1593 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001595int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1596 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001599 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!snd_mask_single(mask))
1601 return -EINVAL;
1602 if (dir)
1603 *dir = 0;
1604 return snd_mask_value(mask);
1605 }
1606 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001607 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (!snd_interval_single(i))
1609 return -EINVAL;
1610 if (dir)
1611 *dir = i->openmin;
1612 return snd_interval_value(i);
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return -EINVAL;
1615}
1616
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001617EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Takashi Iwai877211f2005-11-17 13:59:38 +01001619void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 snd_pcm_hw_param_t var)
1621{
1622 if (hw_is_mask(var)) {
1623 snd_mask_none(hw_param_mask(params, var));
1624 params->cmask |= 1 << var;
1625 params->rmask |= 1 << var;
1626 } else if (hw_is_interval(var)) {
1627 snd_interval_none(hw_param_interval(params, var));
1628 params->cmask |= 1 << var;
1629 params->rmask |= 1 << var;
1630 } else {
1631 snd_BUG();
1632 }
1633}
1634
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001635EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Takashi Iwai877211f2005-11-17 13:59:38 +01001637static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001638 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 int changed;
1641 if (hw_is_mask(var))
1642 changed = snd_mask_refine_first(hw_param_mask(params, var));
1643 else if (hw_is_interval(var))
1644 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001645 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (changed) {
1648 params->cmask |= 1 << var;
1649 params->rmask |= 1 << var;
1650 }
1651 return changed;
1652}
1653
1654
1655/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001656 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001657 * @pcm: PCM instance
1658 * @params: the hw_params instance
1659 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001660 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001662 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001664 *
1665 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001667int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1668 struct snd_pcm_hw_params *params,
1669 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 int changed = _snd_pcm_hw_param_first(params, var);
1672 if (changed < 0)
1673 return changed;
1674 if (params->rmask) {
1675 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001676 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679 return snd_pcm_hw_param_value(params, var, dir);
1680}
1681
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001682EXPORT_SYMBOL(snd_pcm_hw_param_first);
1683
Takashi Iwai877211f2005-11-17 13:59:38 +01001684static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001685 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 int changed;
1688 if (hw_is_mask(var))
1689 changed = snd_mask_refine_last(hw_param_mask(params, var));
1690 else if (hw_is_interval(var))
1691 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001692 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (changed) {
1695 params->cmask |= 1 << var;
1696 params->rmask |= 1 << var;
1697 }
1698 return changed;
1699}
1700
1701
1702/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001703 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001704 * @pcm: PCM instance
1705 * @params: the hw_params instance
1706 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001707 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001709 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001711 *
1712 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001714int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1715 struct snd_pcm_hw_params *params,
1716 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 int changed = _snd_pcm_hw_param_last(params, var);
1719 if (changed < 0)
1720 return changed;
1721 if (params->rmask) {
1722 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001723 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001724 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 return snd_pcm_hw_param_value(params, var, dir);
1727}
1728
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001729EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001732 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001733 * @pcm: PCM instance
1734 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001736 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 * The configuration chosen is that obtained fixing in this order:
1738 * first access, first format, first subformat, min channels,
1739 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001740 *
1741 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001743int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1744 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001746 static int vars[] = {
1747 SNDRV_PCM_HW_PARAM_ACCESS,
1748 SNDRV_PCM_HW_PARAM_FORMAT,
1749 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1750 SNDRV_PCM_HW_PARAM_CHANNELS,
1751 SNDRV_PCM_HW_PARAM_RATE,
1752 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1753 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1754 SNDRV_PCM_HW_PARAM_TICK_TIME,
1755 -1
1756 };
1757 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001759 for (v = vars; *v != -1; v++) {
1760 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1761 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1762 else
1763 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001764 if (snd_BUG_ON(err < 0))
1765 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
1769
Takashi Iwai877211f2005-11-17 13:59:38 +01001770static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 void *arg)
1772{
Takashi Iwai877211f2005-11-17 13:59:38 +01001773 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 unsigned long flags;
1775 snd_pcm_stream_lock_irqsave(substream, flags);
1776 if (snd_pcm_running(substream) &&
1777 snd_pcm_update_hw_ptr(substream) >= 0)
1778 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001779 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001781 runtime->hw_ptr_wrap = 0;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 snd_pcm_stream_unlock_irqrestore(substream, flags);
1784 return 0;
1785}
1786
Takashi Iwai877211f2005-11-17 13:59:38 +01001787static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 void *arg)
1789{
Takashi Iwai877211f2005-11-17 13:59:38 +01001790 struct snd_pcm_channel_info *info = arg;
1791 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int width;
1793 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1794 info->offset = -1;
1795 return 0;
1796 }
1797 width = snd_pcm_format_physical_width(runtime->format);
1798 if (width < 0)
1799 return width;
1800 info->offset = 0;
1801 switch (runtime->access) {
1802 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1803 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001804 if ((UINT_MAX/width) < info->channel) {
1805 snd_printd("%s: integer overflow while multiply\n",
1806 __func__);
1807 return -EINVAL;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 info->first = info->channel * width;
1810 info->step = runtime->channels * width;
1811 break;
1812 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1813 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1814 {
1815 size_t size = runtime->dma_bytes / runtime->channels;
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001816
1817 if ((size > 0) && ((UINT_MAX/(size * 8)) < info->channel)) {
1818 snd_printd("%s: integer overflow while multiply\n",
1819 __func__);
1820 return -EINVAL;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 info->first = info->channel * size * 8;
1823 info->step = width;
1824 break;
1825 }
1826 default:
1827 snd_BUG();
1828 break;
1829 }
1830 return 0;
1831}
1832
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001833static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1834 void *arg)
1835{
1836 struct snd_pcm_hw_params *params = arg;
1837 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001838 int channels;
1839 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001840
1841 params->fifo_size = substream->runtime->hw.fifo_size;
1842 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1843 format = params_format(params);
1844 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001845 frame_size = snd_pcm_format_size(format, channels);
1846 if (frame_size > 0)
1847 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001848 }
1849 return 0;
1850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852/**
1853 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1854 * @substream: the pcm substream instance
1855 * @cmd: ioctl command
1856 * @arg: ioctl argument
1857 *
1858 * Processes the generic ioctl commands for PCM.
1859 * Can be passed as the ioctl callback for PCM ops.
1860 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001861 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001863int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 unsigned int cmd, void *arg)
1865{
1866 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 case SNDRV_PCM_IOCTL1_RESET:
1868 return snd_pcm_lib_ioctl_reset(substream, arg);
1869 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1870 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001871 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1872 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 return -ENXIO;
1875}
1876
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001877EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879/**
1880 * snd_pcm_period_elapsed - update the pcm status for the next period
1881 * @substream: the pcm substream instance
1882 *
1883 * This function is called from the interrupt handler when the
1884 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001885 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *
1887 * Even if more than one periods have elapsed since the last call, you
1888 * have to call this only once.
1889 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001890void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwai877211f2005-11-17 13:59:38 +01001892 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned long flags;
1894
paulhsia4bec1e72019-11-13 01:17:14 +08001895 if (snd_BUG_ON(!substream))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001896 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 snd_pcm_stream_lock_irqsave(substream, flags);
paulhsia4bec1e72019-11-13 01:17:14 +08001899 if (PCM_RUNTIME_CHECK(substream))
1900 goto _unlock;
1901 runtime = substream->runtime;
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001904 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto _end;
1906
Jie Yang90bbaf62015-10-16 17:57:46 +08001907#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (substream->timer_running)
1909 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001910#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
paulhsia4bec1e72019-11-13 01:17:14 +08001913 _unlock:
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001914 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001917EXPORT_SYMBOL(snd_pcm_period_elapsed);
1918
Takashi Iwai13075512008-01-08 18:08:14 +01001919/*
1920 * Wait until avail_min data becomes available
1921 * Returns a negative error code if any error occurs during operation.
1922 * The available space is stored on availp. When err = 0 and avail = 0
1923 * on the capture stream, it indicates the stream is in DRAINING state.
1924 */
David Dillow5daeba32010-06-27 00:13:20 +02001925static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001926 snd_pcm_uframes_t *availp)
1927{
1928 struct snd_pcm_runtime *runtime = substream->runtime;
1929 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1930 wait_queue_t wait;
1931 int err = 0;
1932 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001933 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001934
Arjan van de Ven763437a2011-09-15 08:49:25 +02001935 init_waitqueue_entry(&wait, current);
1936 set_current_state(TASK_INTERRUPTIBLE);
1937 add_wait_queue(&runtime->tsleep, &wait);
1938
Takashi Iwaif2b36142011-05-26 08:09:38 +02001939 if (runtime->no_period_wakeup)
1940 wait_time = MAX_SCHEDULE_TIMEOUT;
1941 else {
1942 wait_time = 10;
1943 if (runtime->rate) {
1944 long t = runtime->period_size * 2 / runtime->rate;
1945 wait_time = max(t, wait_time);
1946 }
1947 wait_time = msecs_to_jiffies(wait_time * 1000);
1948 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001949
Takashi Iwai13075512008-01-08 18:08:14 +01001950 for (;;) {
1951 if (signal_pending(current)) {
1952 err = -ERESTARTSYS;
1953 break;
1954 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001955
1956 /*
1957 * We need to check if space became available already
1958 * (and thus the wakeup happened already) first to close
1959 * the race of space already having become available.
1960 * This check must happen after been added to the waitqueue
1961 * and having current state be INTERRUPTIBLE.
1962 */
1963 if (is_playback)
1964 avail = snd_pcm_playback_avail(runtime);
1965 else
1966 avail = snd_pcm_capture_avail(runtime);
1967 if (avail >= runtime->twake)
1968 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001969 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001970
1971 tout = schedule_timeout(wait_time);
1972
Takashi Iwai13075512008-01-08 18:08:14 +01001973 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001974 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001975 switch (runtime->status->state) {
1976 case SNDRV_PCM_STATE_SUSPENDED:
1977 err = -ESTRPIPE;
1978 goto _endloop;
1979 case SNDRV_PCM_STATE_XRUN:
1980 err = -EPIPE;
1981 goto _endloop;
1982 case SNDRV_PCM_STATE_DRAINING:
1983 if (is_playback)
1984 err = -EPIPE;
1985 else
1986 avail = 0; /* indicate draining */
1987 goto _endloop;
1988 case SNDRV_PCM_STATE_OPEN:
1989 case SNDRV_PCM_STATE_SETUP:
1990 case SNDRV_PCM_STATE_DISCONNECTED:
1991 err = -EBADFD;
1992 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001993 case SNDRV_PCM_STATE_PAUSED:
1994 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001995 }
1996 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001997 pcm_dbg(substream->pcm,
1998 "%s write error (DMA or IRQ trouble?)\n",
1999 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01002000 err = -EIO;
2001 break;
2002 }
Takashi Iwai13075512008-01-08 18:08:14 +01002003 }
2004 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02002005 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002006 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01002007 *availp = avail;
2008 return err;
2009}
2010
Takashi Iwai877211f2005-11-17 13:59:38 +01002011static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 unsigned int hwoff,
2013 unsigned long data, unsigned int off,
2014 snd_pcm_uframes_t frames)
2015{
Takashi Iwai877211f2005-11-17 13:59:38 +01002016 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int err;
2018 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2019 if (substream->ops->copy) {
2020 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2021 return err;
2022 } else {
2023 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2025 return -EFAULT;
2026 }
2027 return 0;
2028}
2029
Takashi Iwai877211f2005-11-17 13:59:38 +01002030typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 unsigned long data, unsigned int off,
2032 snd_pcm_uframes_t size);
2033
Takashi Iwai877211f2005-11-17 13:59:38 +01002034static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 unsigned long data,
2036 snd_pcm_uframes_t size,
2037 int nonblock,
2038 transfer_f transfer)
2039{
Takashi Iwai877211f2005-11-17 13:59:38 +01002040 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 snd_pcm_uframes_t xfer = 0;
2042 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002043 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int err = 0;
2045
2046 if (size == 0)
2047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 snd_pcm_stream_lock_irq(substream);
2050 switch (runtime->status->state) {
2051 case SNDRV_PCM_STATE_PREPARED:
2052 case SNDRV_PCM_STATE_RUNNING:
2053 case SNDRV_PCM_STATE_PAUSED:
2054 break;
2055 case SNDRV_PCM_STATE_XRUN:
2056 err = -EPIPE;
2057 goto _end_unlock;
2058 case SNDRV_PCM_STATE_SUSPENDED:
2059 err = -ESTRPIPE;
2060 goto _end_unlock;
2061 default:
2062 err = -EBADFD;
2063 goto _end_unlock;
2064 }
2065
David Dillow5daeba32010-06-27 00:13:20 +02002066 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002067 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2068 snd_pcm_update_hw_ptr(substream);
2069 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 while (size > 0) {
2071 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002073 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (nonblock) {
2075 err = -EAGAIN;
2076 goto _end_unlock;
2077 }
David Dillow5daeba32010-06-27 00:13:20 +02002078 runtime->twake = min_t(snd_pcm_uframes_t, size,
2079 runtime->control->avail_min ? : 1);
2080 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002081 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 frames = size > avail ? avail : size;
2085 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2086 if (frames > cont)
2087 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002088 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002089 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002090 snd_pcm_stream_unlock_irq(substream);
2091 return -EINVAL;
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 appl_ptr = runtime->control->appl_ptr;
2094 appl_ofs = appl_ptr % runtime->buffer_size;
2095 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002096 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002098 if (err < 0)
2099 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 switch (runtime->status->state) {
2101 case SNDRV_PCM_STATE_XRUN:
2102 err = -EPIPE;
2103 goto _end_unlock;
2104 case SNDRV_PCM_STATE_SUSPENDED:
2105 err = -ESTRPIPE;
2106 goto _end_unlock;
2107 default:
2108 break;
2109 }
2110 appl_ptr += frames;
2111 if (appl_ptr >= runtime->boundary)
2112 appl_ptr -= runtime->boundary;
2113 runtime->control->appl_ptr = appl_ptr;
2114 if (substream->ops->ack)
2115 substream->ops->ack(substream);
2116
2117 offset += frames;
2118 size -= frames;
2119 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002120 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2122 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2123 err = snd_pcm_start(substream);
2124 if (err < 0)
2125 goto _end_unlock;
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002129 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002130 if (xfer > 0 && err >= 0)
2131 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2134}
2135
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002136/* sanity-check for read/write methods */
2137static int pcm_sanity_check(struct snd_pcm_substream *substream)
2138{
2139 struct snd_pcm_runtime *runtime;
2140 if (PCM_RUNTIME_CHECK(substream))
2141 return -ENXIO;
Liam Girdwoodf2727332016-01-29 02:27:17 +05302142 /* TODO: consider and -EINVAL here */
2143 if (substream->hw_no_buffer)
2144 snd_printd("%s: warning this PCM is host less\n", __func__);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002145 runtime = substream->runtime;
2146 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2147 return -EINVAL;
2148 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2149 return -EBADFD;
2150 return 0;
2151}
2152
Takashi Iwai877211f2005-11-17 13:59:38 +01002153snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai877211f2005-11-17 13:59:38 +01002155 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002159 err = pcm_sanity_check(substream);
2160 if (err < 0)
2161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002163 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2166 runtime->channels > 1)
2167 return -EINVAL;
2168 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2169 snd_pcm_lib_write_transfer);
2170}
2171
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002172EXPORT_SYMBOL(snd_pcm_lib_write);
2173
Takashi Iwai877211f2005-11-17 13:59:38 +01002174static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 unsigned int hwoff,
2176 unsigned long data, unsigned int off,
2177 snd_pcm_uframes_t frames)
2178{
Takashi Iwai877211f2005-11-17 13:59:38 +01002179 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int err;
2181 void __user **bufs = (void __user **)data;
2182 int channels = runtime->channels;
2183 int c;
2184 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002185 if (snd_BUG_ON(!substream->ops->silence))
2186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 for (c = 0; c < channels; ++c, ++bufs) {
2188 if (*bufs == NULL) {
2189 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2190 return err;
2191 } else {
2192 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2193 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2194 return err;
2195 }
2196 }
2197 } else {
2198 /* default transfer behaviour */
2199 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 for (c = 0; c < channels; ++c, ++bufs) {
2201 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2202 if (*bufs == NULL) {
2203 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2204 } else {
2205 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2206 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2207 return -EFAULT;
2208 }
2209 }
2210 }
2211 return 0;
2212}
2213
Takashi Iwai877211f2005-11-17 13:59:38 +01002214snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 void __user **bufs,
2216 snd_pcm_uframes_t frames)
2217{
Takashi Iwai877211f2005-11-17 13:59:38 +01002218 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002220 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002222 err = pcm_sanity_check(substream);
2223 if (err < 0)
2224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002226 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2229 return -EINVAL;
2230 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2231 nonblock, snd_pcm_lib_writev_transfer);
2232}
2233
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002234EXPORT_SYMBOL(snd_pcm_lib_writev);
2235
Takashi Iwai877211f2005-11-17 13:59:38 +01002236static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 unsigned int hwoff,
2238 unsigned long data, unsigned int off,
2239 snd_pcm_uframes_t frames)
2240{
Takashi Iwai877211f2005-11-17 13:59:38 +01002241 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int err;
2243 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2244 if (substream->ops->copy) {
2245 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2246 return err;
2247 } else {
2248 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2250 return -EFAULT;
2251 }
2252 return 0;
2253}
2254
Takashi Iwai877211f2005-11-17 13:59:38 +01002255static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 unsigned long data,
2257 snd_pcm_uframes_t size,
2258 int nonblock,
2259 transfer_f transfer)
2260{
Takashi Iwai877211f2005-11-17 13:59:38 +01002261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 snd_pcm_uframes_t xfer = 0;
2263 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002264 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 int err = 0;
2266
2267 if (size == 0)
2268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 snd_pcm_stream_lock_irq(substream);
2271 switch (runtime->status->state) {
2272 case SNDRV_PCM_STATE_PREPARED:
2273 if (size >= runtime->start_threshold) {
2274 err = snd_pcm_start(substream);
2275 if (err < 0)
2276 goto _end_unlock;
2277 }
2278 break;
2279 case SNDRV_PCM_STATE_DRAINING:
2280 case SNDRV_PCM_STATE_RUNNING:
2281 case SNDRV_PCM_STATE_PAUSED:
2282 break;
2283 case SNDRV_PCM_STATE_XRUN:
2284 err = -EPIPE;
2285 goto _end_unlock;
2286 case SNDRV_PCM_STATE_SUSPENDED:
2287 err = -ESTRPIPE;
2288 goto _end_unlock;
2289 default:
2290 err = -EBADFD;
2291 goto _end_unlock;
2292 }
2293
David Dillow5daeba32010-06-27 00:13:20 +02002294 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002295 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2296 snd_pcm_update_hw_ptr(substream);
2297 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 while (size > 0) {
2299 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002301 if (!avail) {
2302 if (runtime->status->state ==
2303 SNDRV_PCM_STATE_DRAINING) {
2304 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto _end_unlock;
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (nonblock) {
2308 err = -EAGAIN;
2309 goto _end_unlock;
2310 }
David Dillow5daeba32010-06-27 00:13:20 +02002311 runtime->twake = min_t(snd_pcm_uframes_t, size,
2312 runtime->control->avail_min ? : 1);
2313 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002314 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002316 if (!avail)
2317 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 frames = size > avail ? avail : size;
2320 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2321 if (frames > cont)
2322 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002323 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002324 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002325 snd_pcm_stream_unlock_irq(substream);
2326 return -EINVAL;
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 appl_ptr = runtime->control->appl_ptr;
2329 appl_ofs = appl_ptr % runtime->buffer_size;
2330 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002331 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002333 if (err < 0)
2334 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 switch (runtime->status->state) {
2336 case SNDRV_PCM_STATE_XRUN:
2337 err = -EPIPE;
2338 goto _end_unlock;
2339 case SNDRV_PCM_STATE_SUSPENDED:
2340 err = -ESTRPIPE;
2341 goto _end_unlock;
2342 default:
2343 break;
2344 }
2345 appl_ptr += frames;
2346 if (appl_ptr >= runtime->boundary)
2347 appl_ptr -= runtime->boundary;
2348 runtime->control->appl_ptr = appl_ptr;
2349 if (substream->ops->ack)
2350 substream->ops->ack(substream);
2351
2352 offset += frames;
2353 size -= frames;
2354 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002355 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002358 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002359 if (xfer > 0 && err >= 0)
2360 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2363}
2364
Takashi Iwai877211f2005-11-17 13:59:38 +01002365snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Takashi Iwai877211f2005-11-17 13:59:38 +01002367 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002369 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002371 err = pcm_sanity_check(substream);
2372 if (err < 0)
2373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002375 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2377 return -EINVAL;
2378 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2379}
2380
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002381EXPORT_SYMBOL(snd_pcm_lib_read);
2382
Takashi Iwai877211f2005-11-17 13:59:38 +01002383static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 unsigned int hwoff,
2385 unsigned long data, unsigned int off,
2386 snd_pcm_uframes_t frames)
2387{
Takashi Iwai877211f2005-11-17 13:59:38 +01002388 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 int err;
2390 void __user **bufs = (void __user **)data;
2391 int channels = runtime->channels;
2392 int c;
2393 if (substream->ops->copy) {
2394 for (c = 0; c < channels; ++c, ++bufs) {
2395 char __user *buf;
2396 if (*bufs == NULL)
2397 continue;
2398 buf = *bufs + samples_to_bytes(runtime, off);
2399 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2400 return err;
2401 }
2402 } else {
2403 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 for (c = 0; c < channels; ++c, ++bufs) {
2405 char *hwbuf;
2406 char __user *buf;
2407 if (*bufs == NULL)
2408 continue;
2409
2410 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2411 buf = *bufs + samples_to_bytes(runtime, off);
2412 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2413 return -EFAULT;
2414 }
2415 }
2416 return 0;
2417}
2418
Takashi Iwai877211f2005-11-17 13:59:38 +01002419snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 void __user **bufs,
2421 snd_pcm_uframes_t frames)
2422{
Takashi Iwai877211f2005-11-17 13:59:38 +01002423 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002425 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002427 err = pcm_sanity_check(substream);
2428 if (err < 0)
2429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2432 return -EBADFD;
2433
Takashi Iwai0df63e42006-04-28 15:13:41 +02002434 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2436 return -EINVAL;
2437 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002441
2442/*
2443 * standard channel mapping helpers
2444 */
2445
2446/* default channel maps for multi-channel playbacks, up to 8 channels */
2447const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2448 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002449 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002450 { .channels = 2,
2451 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2452 { .channels = 4,
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2454 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2455 { .channels = 6,
2456 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2457 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2458 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2459 { .channels = 8,
2460 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2461 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2462 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2463 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2464 { }
2465};
2466EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2467
2468/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2469const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2470 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002471 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002472 { .channels = 2,
2473 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2474 { .channels = 4,
2475 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2476 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2477 { .channels = 6,
2478 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2479 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2480 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2481 { .channels = 8,
2482 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2483 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2484 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2485 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2486 { }
2487};
2488EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2489
2490static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2491{
2492 if (ch > info->max_channels)
2493 return false;
2494 return !info->channel_mask || (info->channel_mask & (1U << ch));
2495}
2496
2497static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_info *uinfo)
2499{
2500 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2501
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2503 uinfo->count = 0;
2504 uinfo->count = info->max_channels;
2505 uinfo->value.integer.min = 0;
2506 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2507 return 0;
2508}
2509
2510/* get callback for channel map ctl element
2511 * stores the channel position firstly matching with the current channels
2512 */
2513static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_value *ucontrol)
2515{
2516 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2517 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2518 struct snd_pcm_substream *substream;
2519 const struct snd_pcm_chmap_elem *map;
2520
Takashi Iwai552a14a2017-06-14 16:20:32 +02002521 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002522 return -EINVAL;
2523 substream = snd_pcm_chmap_substream(info, idx);
2524 if (!substream)
2525 return -ENODEV;
2526 memset(ucontrol->value.integer.value, 0,
2527 sizeof(ucontrol->value.integer.value));
2528 if (!substream->runtime)
2529 return 0; /* no channels set */
2530 for (map = info->chmap; map->channels; map++) {
2531 int i;
2532 if (map->channels == substream->runtime->channels &&
2533 valid_chmap_channels(info, map->channels)) {
2534 for (i = 0; i < map->channels; i++)
2535 ucontrol->value.integer.value[i] = map->map[i];
2536 return 0;
2537 }
2538 }
2539 return -EINVAL;
2540}
2541
2542/* tlv callback for channel map ctl element
2543 * expands the pre-defined channel maps in a form of TLV
2544 */
2545static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2546 unsigned int size, unsigned int __user *tlv)
2547{
2548 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2549 const struct snd_pcm_chmap_elem *map;
2550 unsigned int __user *dst;
2551 int c, count = 0;
2552
Takashi Iwai552a14a2017-06-14 16:20:32 +02002553 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002554 return -EINVAL;
2555 if (size < 8)
2556 return -ENOMEM;
2557 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2558 return -EFAULT;
2559 size -= 8;
2560 dst = tlv + 2;
2561 for (map = info->chmap; map->channels; map++) {
2562 int chs_bytes = map->channels * 4;
2563 if (!valid_chmap_channels(info, map->channels))
2564 continue;
2565 if (size < 8)
2566 return -ENOMEM;
2567 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2568 put_user(chs_bytes, dst + 1))
2569 return -EFAULT;
2570 dst += 2;
2571 size -= 8;
2572 count += 8;
2573 if (size < chs_bytes)
2574 return -ENOMEM;
2575 size -= chs_bytes;
2576 count += chs_bytes;
2577 for (c = 0; c < map->channels; c++) {
2578 if (put_user(map->map[c], dst))
2579 return -EFAULT;
2580 dst++;
2581 }
2582 }
2583 if (put_user(count, tlv + 1))
2584 return -EFAULT;
2585 return 0;
2586}
2587
2588static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2589{
2590 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2591 info->pcm->streams[info->stream].chmap_kctl = NULL;
2592 kfree(info);
2593}
2594
Banajit Goswami55b24c52016-11-21 19:08:27 -08002595static int pcm_volume_ctl_info(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_info *uinfo)
2597{
2598 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2599 uinfo->count = 1;
2600 uinfo->value.integer.min = 0;
2601 uinfo->value.integer.max = 0x2000;
2602 return 0;
2603}
2604
2605static void pcm_volume_ctl_private_free(struct snd_kcontrol *kcontrol)
2606{
2607 struct snd_pcm_volume *info = snd_kcontrol_chip(kcontrol);
2608
2609 info->pcm->streams[info->stream].vol_kctl = NULL;
2610 kfree(info);
2611}
2612
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002613/**
2614 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2615 * @pcm: the assigned PCM instance
2616 * @stream: stream direction
2617 * @chmap: channel map elements (for query)
2618 * @max_channels: the max number of channels for the stream
2619 * @private_value: the value passed to each kcontrol's private_value field
2620 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2621 *
2622 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002623 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002624 */
2625int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2626 const struct snd_pcm_chmap_elem *chmap,
2627 int max_channels,
2628 unsigned long private_value,
2629 struct snd_pcm_chmap **info_ret)
2630{
2631 struct snd_pcm_chmap *info;
2632 struct snd_kcontrol_new knew = {
2633 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2634 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002635 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2636 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2637 .info = pcm_chmap_ctl_info,
2638 .get = pcm_chmap_ctl_get,
2639 .tlv.c = pcm_chmap_ctl_tlv,
2640 };
2641 int err;
2642
Takashi Iwai8d879be2016-05-10 16:07:40 +02002643 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2644 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002645 info = kzalloc(sizeof(*info), GFP_KERNEL);
2646 if (!info)
2647 return -ENOMEM;
2648 info->pcm = pcm;
2649 info->stream = stream;
2650 info->chmap = chmap;
2651 info->max_channels = max_channels;
2652 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2653 knew.name = "Playback Channel Map";
2654 else
2655 knew.name = "Capture Channel Map";
2656 knew.device = pcm->device;
2657 knew.count = pcm->streams[stream].substream_count;
2658 knew.private_value = private_value;
2659 info->kctl = snd_ctl_new1(&knew, info);
2660 if (!info->kctl) {
2661 kfree(info);
2662 return -ENOMEM;
2663 }
2664 info->kctl->private_free = pcm_chmap_ctl_private_free;
2665 err = snd_ctl_add(pcm->card, info->kctl);
2666 if (err < 0)
2667 return err;
2668 pcm->streams[stream].chmap_kctl = info->kctl;
2669 if (info_ret)
2670 *info_ret = info;
2671 return 0;
2672}
2673EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
Banajit Goswami55b24c52016-11-21 19:08:27 -08002674
2675/**
2676 * snd_pcm_add_volume_ctls - create volume control elements
2677 * @pcm: the assigned PCM instance
2678 * @stream: stream direction
2679 * @max_length: the max length of the volume parameter of stream
2680 * @private_value: the value passed to each kcontrol's private_value field
2681 * @info_ret: store struct snd_pcm_volume instance if non-NULL
2682 *
2683 * Create volume control elements assigned to the given PCM stream(s).
2684 * Returns zero if succeed, or a negative error value.
2685 */
2686int snd_pcm_add_volume_ctls(struct snd_pcm *pcm, int stream,
2687 const struct snd_pcm_volume_elem *volume,
2688 int max_length,
2689 unsigned long private_value,
2690 struct snd_pcm_volume **info_ret)
2691{
2692 struct snd_pcm_volume *info;
2693 struct snd_kcontrol_new knew = {
2694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2695 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2696 SNDRV_CTL_ELEM_ACCESS_READWRITE,
2697 .info = pcm_volume_ctl_info,
2698 };
2699 int err;
2700 int size;
2701
2702 info = kzalloc(sizeof(*info), GFP_KERNEL);
2703 if (!info)
2704 return -ENOMEM;
2705 info->pcm = pcm;
2706 info->stream = stream;
2707 info->volume = volume;
2708 info->max_length = max_length;
2709 size = sizeof("Playback ") + sizeof(" Volume") +
2710 STRING_LENGTH_OF_INT*sizeof(char) + 1;
2711 knew.name = kzalloc(size, GFP_KERNEL);
2712 if (!knew.name) {
2713 kfree(info);
2714 return -ENOMEM;
2715 }
2716 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2717 snprintf((char *)knew.name, size, "%s %d %s",
2718 "Playback", pcm->device, "Volume");
2719 else
2720 snprintf((char *)knew.name, size, "%s %d %s",
2721 "Capture", pcm->device, "Volume");
2722 knew.device = pcm->device;
2723 knew.count = pcm->streams[stream].substream_count;
2724 knew.private_value = private_value;
2725 info->kctl = snd_ctl_new1(&knew, info);
2726 if (!info->kctl) {
2727 kfree(info);
2728 kfree(knew.name);
2729 return -ENOMEM;
2730 }
2731 info->kctl->private_free = pcm_volume_ctl_private_free;
2732 err = snd_ctl_add(pcm->card, info->kctl);
2733 if (err < 0) {
2734 kfree(info);
2735 kfree(knew.name);
2736 return -ENOMEM;
2737 }
2738 pcm->streams[stream].vol_kctl = info->kctl;
2739 if (info_ret)
2740 *info_ret = info;
2741 kfree(knew.name);
2742 return 0;
2743}
2744EXPORT_SYMBOL(snd_pcm_add_volume_ctls);
Banajit Goswamicec03b62016-11-21 19:22:56 -08002745
2746static int pcm_usr_ctl_info(struct snd_kcontrol *kcontrol,
2747 struct snd_ctl_elem_info *uinfo)
2748{
2749 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2750 uinfo->count = MAX_USR_CTRL_CNT;
2751 uinfo->value.integer.min = 0;
2752 uinfo->value.integer.max = INT_MAX;
2753 return 0;
2754}
2755
2756static void pcm_usr_ctl_private_free(struct snd_kcontrol *kcontrol)
2757{
2758 struct snd_pcm_usr *info = snd_kcontrol_chip(kcontrol);
2759
2760 info->pcm->streams[info->stream].usr_kctl = NULL;
2761 kfree(info);
2762}
2763
2764/**
2765 * snd_pcm_add_usr_ctls - create user control elements
2766 * @pcm: the assigned PCM instance
2767 * @stream: stream direction
2768 * @max_length: the max length of the user parameter of stream
2769 * @private_value: the value passed to each kcontrol's private_value field
2770 * @info_ret: store struct snd_pcm_usr instance if non-NULL
2771 *
2772 * Create usr control elements assigned to the given PCM stream(s).
2773 * Returns zero if succeed, or a negative error value.
2774 */
2775int snd_pcm_add_usr_ctls(struct snd_pcm *pcm, int stream,
2776 const struct snd_pcm_usr_elem *usr,
2777 int max_length, int max_kctrl_str_len,
2778 unsigned long private_value,
2779 struct snd_pcm_usr **info_ret)
2780{
2781 struct snd_pcm_usr *info;
2782 struct snd_kcontrol_new knew = {
2783 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2784 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2785 .info = pcm_usr_ctl_info,
2786 };
2787 int err;
2788 char *buf;
2789
2790 info = kzalloc(sizeof(*info), GFP_KERNEL);
2791 if (!info)
2792 return -ENOMEM;
2793
2794 info->pcm = pcm;
2795 info->stream = stream;
2796 info->usr = usr;
2797 info->max_length = max_length;
2798 buf = kzalloc(max_kctrl_str_len, GFP_KERNEL);
2799 if (!buf) {
2800 pr_err("%s: buffer allocation failed\n", __func__);
2801 kfree(info);
2802 return -ENOMEM;
2803 }
2804 knew.name = buf;
2805 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2806 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2807 "Playback", pcm->device, "User kcontrol");
2808 else
2809 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2810 "Capture", pcm->device, "User kcontrol");
2811 knew.device = pcm->device;
2812 knew.count = pcm->streams[stream].substream_count;
2813 knew.private_value = private_value;
2814 info->kctl = snd_ctl_new1(&knew, info);
2815 if (!info->kctl) {
2816 kfree(info);
2817 kfree(knew.name);
2818 pr_err("%s: snd_ctl_new failed\n", __func__);
2819 return -ENOMEM;
2820 }
2821 info->kctl->private_free = pcm_usr_ctl_private_free;
2822 err = snd_ctl_add(pcm->card, info->kctl);
2823 if (err < 0) {
2824 kfree(info);
2825 kfree(knew.name);
2826 pr_err("%s: snd_ctl_add failed:%d\n", __func__,
2827 err);
2828 return -ENOMEM;
2829 }
2830 pcm->streams[stream].usr_kctl = info->kctl;
2831 if (info_ret)
2832 *info_ret = info;
2833 kfree(knew.name);
2834 return 0;
2835}
2836EXPORT_SYMBOL(snd_pcm_add_usr_ctls);