blob: 25deca4457f95fabbaaf9f674fb7d44be71e21d9 [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) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200585 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *r = 0;
587 return UINT_MAX;
588 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200589 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (n >= UINT_MAX) {
591 *r = 0;
592 return UINT_MAX;
593 }
594 return n;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/**
598 * snd_interval_refine - refine the interval value of configurator
599 * @i: the interval value to refine
600 * @v: the interval value to refer to
601 *
602 * Refines the interval value with the reference value.
603 * The interval is changed to the range satisfying both intervals.
604 * The interval status (min, max, integer, etc.) are evaluated.
605 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100606 * Return: Positive if the value is changed, zero if it's not changed, or a
607 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100609int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200612 if (snd_BUG_ON(snd_interval_empty(i)))
613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (i->min < v->min) {
615 i->min = v->min;
616 i->openmin = v->openmin;
617 changed = 1;
618 } else if (i->min == v->min && !i->openmin && v->openmin) {
619 i->openmin = 1;
620 changed = 1;
621 }
622 if (i->max > v->max) {
623 i->max = v->max;
624 i->openmax = v->openmax;
625 changed = 1;
626 } else if (i->max == v->max && !i->openmax && v->openmax) {
627 i->openmax = 1;
628 changed = 1;
629 }
630 if (!i->integer && v->integer) {
631 i->integer = 1;
632 changed = 1;
633 }
634 if (i->integer) {
635 if (i->openmin) {
636 i->min++;
637 i->openmin = 0;
638 }
639 if (i->openmax) {
640 i->max--;
641 i->openmax = 0;
642 }
643 } else if (!i->openmin && !i->openmax && i->min == i->max)
644 i->integer = 1;
645 if (snd_interval_checkempty(i)) {
646 snd_interval_none(i);
647 return -EINVAL;
648 }
649 return changed;
650}
651
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200652EXPORT_SYMBOL(snd_interval_refine);
653
Takashi Iwai877211f2005-11-17 13:59:38 +0100654static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200656 if (snd_BUG_ON(snd_interval_empty(i)))
657 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (snd_interval_single(i))
659 return 0;
660 i->max = i->min;
661 i->openmax = i->openmin;
662 if (i->openmax)
663 i->max++;
664 return 1;
665}
666
Takashi Iwai877211f2005-11-17 13:59:38 +0100667static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200669 if (snd_BUG_ON(snd_interval_empty(i)))
670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (snd_interval_single(i))
672 return 0;
673 i->min = i->max;
674 i->openmin = i->openmax;
675 if (i->openmin)
676 i->min--;
677 return 1;
678}
679
Takashi Iwai877211f2005-11-17 13:59:38 +0100680void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 if (a->empty || b->empty) {
683 snd_interval_none(c);
684 return;
685 }
686 c->empty = 0;
687 c->min = mul(a->min, b->min);
688 c->openmin = (a->openmin || b->openmin);
689 c->max = mul(a->max, b->max);
690 c->openmax = (a->openmax || b->openmax);
691 c->integer = (a->integer && b->integer);
692}
693
694/**
695 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200696 * @a: dividend
697 * @b: divisor
698 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
700 * c = a / b
701 *
702 * Returns non-zero if the value is changed, zero if not changed.
703 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100704void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 unsigned int r;
707 if (a->empty || b->empty) {
708 snd_interval_none(c);
709 return;
710 }
711 c->empty = 0;
712 c->min = div32(a->min, b->max, &r);
713 c->openmin = (r || a->openmin || b->openmax);
714 if (b->min > 0) {
715 c->max = div32(a->max, b->min, &r);
716 if (r) {
717 c->max++;
718 c->openmax = 1;
719 } else
720 c->openmax = (a->openmax || b->openmin);
721 } else {
722 c->max = UINT_MAX;
723 c->openmax = 0;
724 }
725 c->integer = 0;
726}
727
728/**
729 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200730 * @a: dividend 1
731 * @b: dividend 2
732 * @k: divisor (as integer)
733 * @c: result
734 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * c = a * b / k
736 *
737 * Returns non-zero if the value is changed, zero if not changed.
738 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100739void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
740 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 unsigned int r;
743 if (a->empty || b->empty) {
744 snd_interval_none(c);
745 return;
746 }
747 c->empty = 0;
748 c->min = muldiv32(a->min, b->min, k, &r);
749 c->openmin = (r || a->openmin || b->openmin);
750 c->max = muldiv32(a->max, b->max, k, &r);
751 if (r) {
752 c->max++;
753 c->openmax = 1;
754 } else
755 c->openmax = (a->openmax || b->openmax);
756 c->integer = 0;
757}
758
759/**
760 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200761 * @a: dividend 1
762 * @k: dividend 2 (as integer)
763 * @b: divisor
764 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * c = a * k / b
767 *
768 * Returns non-zero if the value is changed, zero if not changed.
769 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100770void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
771 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 unsigned int r;
774 if (a->empty || b->empty) {
775 snd_interval_none(c);
776 return;
777 }
778 c->empty = 0;
779 c->min = muldiv32(a->min, k, b->max, &r);
780 c->openmin = (r || a->openmin || b->openmax);
781 if (b->min > 0) {
782 c->max = muldiv32(a->max, k, b->min, &r);
783 if (r) {
784 c->max++;
785 c->openmax = 1;
786 } else
787 c->openmax = (a->openmax || b->openmin);
788 } else {
789 c->max = UINT_MAX;
790 c->openmax = 0;
791 }
792 c->integer = 0;
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/* ---- */
796
797
798/**
799 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200800 * @i: interval to refine
801 * @rats_count: number of ratnum_t
802 * @rats: ratnum_t array
803 * @nump: pointer to store the resultant numerator
804 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100806 * Return: Positive if the value is changed, zero if it's not changed, or a
807 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100809int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100810 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100811 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100813 unsigned int best_num, best_den;
814 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100816 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100818 unsigned int result_num, result_den;
819 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 best_num = best_den = best_diff = 0;
822 for (k = 0; k < rats_count; ++k) {
823 unsigned int num = rats[k].num;
824 unsigned int den;
825 unsigned int q = i->min;
826 int diff;
827 if (q == 0)
828 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100829 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (den < rats[k].den_min)
831 continue;
832 if (den > rats[k].den_max)
833 den = rats[k].den_max;
834 else {
835 unsigned int r;
836 r = (den - rats[k].den_min) % rats[k].den_step;
837 if (r != 0)
838 den -= r;
839 }
840 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100841 if (diff < 0)
842 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (best_num == 0 ||
844 diff * best_den < best_diff * den) {
845 best_diff = diff;
846 best_den = den;
847 best_num = num;
848 }
849 }
850 if (best_den == 0) {
851 i->empty = 1;
852 return -EINVAL;
853 }
854 t.min = div_down(best_num, best_den);
855 t.openmin = !!(best_num % best_den);
856
Krzysztof Helt8374e242009-12-21 17:07:08 +0100857 result_num = best_num;
858 result_diff = best_diff;
859 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 best_num = best_den = best_diff = 0;
861 for (k = 0; k < rats_count; ++k) {
862 unsigned int num = rats[k].num;
863 unsigned int den;
864 unsigned int q = i->max;
865 int diff;
866 if (q == 0) {
867 i->empty = 1;
868 return -EINVAL;
869 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100870 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (den > rats[k].den_max)
872 continue;
873 if (den < rats[k].den_min)
874 den = rats[k].den_min;
875 else {
876 unsigned int r;
877 r = (den - rats[k].den_min) % rats[k].den_step;
878 if (r != 0)
879 den += rats[k].den_step - r;
880 }
881 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100882 if (diff < 0)
883 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (best_num == 0 ||
885 diff * best_den < best_diff * den) {
886 best_diff = diff;
887 best_den = den;
888 best_num = num;
889 }
890 }
891 if (best_den == 0) {
892 i->empty = 1;
893 return -EINVAL;
894 }
895 t.max = div_up(best_num, best_den);
896 t.openmax = !!(best_num % best_den);
897 t.integer = 0;
898 err = snd_interval_refine(i, &t);
899 if (err < 0)
900 return err;
901
902 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100903 if (best_diff * result_den < result_diff * best_den) {
904 result_num = best_num;
905 result_den = best_den;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100908 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100910 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 return err;
913}
914
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200915EXPORT_SYMBOL(snd_interval_ratnum);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/**
918 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200919 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100920 * @rats_count: number of struct ratden
921 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200922 * @nump: pointer to store the resultant numerator
923 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100925 * Return: Positive if the value is changed, zero if it's not changed, or a
926 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100928static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100929 unsigned int rats_count,
930 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int *nump, unsigned int *denp)
932{
933 unsigned int best_num, best_diff, best_den;
934 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100935 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int err;
937
938 best_num = best_den = best_diff = 0;
939 for (k = 0; k < rats_count; ++k) {
940 unsigned int num;
941 unsigned int den = rats[k].den;
942 unsigned int q = i->min;
943 int diff;
944 num = mul(q, den);
945 if (num > rats[k].num_max)
946 continue;
947 if (num < rats[k].num_min)
948 num = rats[k].num_max;
949 else {
950 unsigned int r;
951 r = (num - rats[k].num_min) % rats[k].num_step;
952 if (r != 0)
953 num += rats[k].num_step - r;
954 }
955 diff = num - q * den;
956 if (best_num == 0 ||
957 diff * best_den < best_diff * den) {
958 best_diff = diff;
959 best_den = den;
960 best_num = num;
961 }
962 }
963 if (best_den == 0) {
964 i->empty = 1;
965 return -EINVAL;
966 }
967 t.min = div_down(best_num, best_den);
968 t.openmin = !!(best_num % best_den);
969
970 best_num = best_den = best_diff = 0;
971 for (k = 0; k < rats_count; ++k) {
972 unsigned int num;
973 unsigned int den = rats[k].den;
974 unsigned int q = i->max;
975 int diff;
976 num = mul(q, den);
977 if (num < rats[k].num_min)
978 continue;
979 if (num > rats[k].num_max)
980 num = rats[k].num_max;
981 else {
982 unsigned int r;
983 r = (num - rats[k].num_min) % rats[k].num_step;
984 if (r != 0)
985 num -= r;
986 }
987 diff = q * den - num;
988 if (best_num == 0 ||
989 diff * best_den < best_diff * den) {
990 best_diff = diff;
991 best_den = den;
992 best_num = num;
993 }
994 }
995 if (best_den == 0) {
996 i->empty = 1;
997 return -EINVAL;
998 }
999 t.max = div_up(best_num, best_den);
1000 t.openmax = !!(best_num % best_den);
1001 t.integer = 0;
1002 err = snd_interval_refine(i, &t);
1003 if (err < 0)
1004 return err;
1005
1006 if (snd_interval_single(i)) {
1007 if (nump)
1008 *nump = best_num;
1009 if (denp)
1010 *denp = best_den;
1011 }
1012 return err;
1013}
1014
1015/**
1016 * snd_interval_list - refine the interval value from the list
1017 * @i: the interval value to refine
1018 * @count: the number of elements in the list
1019 * @list: the value list
1020 * @mask: the bit-mask to evaluate
1021 *
1022 * Refines the interval value from the list.
1023 * When mask is non-zero, only the elements corresponding to bit 1 are
1024 * evaluated.
1025 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001026 * Return: Positive if the value is changed, zero if it's not changed, or a
1027 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
Mark Brown4af87a92012-03-14 19:48:43 +00001029int snd_interval_list(struct snd_interval *i, unsigned int count,
1030 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001033 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001034
1035 if (!count) {
1036 i->empty = 1;
1037 return -EINVAL;
1038 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001039 snd_interval_any(&list_range);
1040 list_range.min = UINT_MAX;
1041 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 for (k = 0; k < count; k++) {
1043 if (mask && !(mask & (1 << k)))
1044 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001045 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001047 list_range.min = min(list_range.min, list[k]);
1048 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001050 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001053EXPORT_SYMBOL(snd_interval_list);
1054
Peter Rosinf66f8982015-01-28 15:16:06 +01001055/**
1056 * snd_interval_ranges - refine the interval value from the list of ranges
1057 * @i: the interval value to refine
1058 * @count: the number of elements in the list of ranges
1059 * @ranges: the ranges list
1060 * @mask: the bit-mask to evaluate
1061 *
1062 * Refines the interval value from the list of ranges.
1063 * When mask is non-zero, only the elements corresponding to bit 1 are
1064 * evaluated.
1065 *
1066 * Return: Positive if the value is changed, zero if it's not changed, or a
1067 * negative error code.
1068 */
1069int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1070 const struct snd_interval *ranges, unsigned int mask)
1071{
1072 unsigned int k;
1073 struct snd_interval range_union;
1074 struct snd_interval range;
1075
1076 if (!count) {
1077 snd_interval_none(i);
1078 return -EINVAL;
1079 }
1080 snd_interval_any(&range_union);
1081 range_union.min = UINT_MAX;
1082 range_union.max = 0;
1083 for (k = 0; k < count; k++) {
1084 if (mask && !(mask & (1 << k)))
1085 continue;
1086 snd_interval_copy(&range, &ranges[k]);
1087 if (snd_interval_refine(&range, i) < 0)
1088 continue;
1089 if (snd_interval_empty(&range))
1090 continue;
1091
1092 if (range.min < range_union.min) {
1093 range_union.min = range.min;
1094 range_union.openmin = 1;
1095 }
1096 if (range.min == range_union.min && !range.openmin)
1097 range_union.openmin = 0;
1098 if (range.max > range_union.max) {
1099 range_union.max = range.max;
1100 range_union.openmax = 1;
1101 }
1102 if (range.max == range_union.max && !range.openmax)
1103 range_union.openmax = 0;
1104 }
1105 return snd_interval_refine(i, &range_union);
1106}
1107EXPORT_SYMBOL(snd_interval_ranges);
1108
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001109static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 unsigned int n;
1112 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001113 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (n != 0 || i->openmin) {
1115 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001116 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 changed = 1;
1118 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001119 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (n != 0 || i->openmax) {
1121 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001122 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 changed = 1;
1124 }
1125 if (snd_interval_checkempty(i)) {
1126 i->empty = 1;
1127 return -EINVAL;
1128 }
1129 return changed;
1130}
1131
1132/* Info constraints helpers */
1133
1134/**
1135 * snd_pcm_hw_rule_add - add the hw-constraint rule
1136 * @runtime: the pcm runtime instance
1137 * @cond: condition bits
1138 * @var: the variable to evaluate
1139 * @func: the evaluation function
1140 * @private: the private data pointer passed to function
1141 * @dep: the dependent variables
1142 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001143 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001145int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int var,
1147 snd_pcm_hw_rule_func_t func, void *private,
1148 int dep, ...)
1149{
Takashi Iwai877211f2005-11-17 13:59:38 +01001150 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1151 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 unsigned int k;
1153 va_list args;
1154 va_start(args, dep);
1155 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001156 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 unsigned int new_rules = constrs->rules_all + 16;
1158 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001159 if (!new) {
1160 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (constrs->rules) {
1164 memcpy(new, constrs->rules,
1165 constrs->rules_num * sizeof(*c));
1166 kfree(constrs->rules);
1167 }
1168 constrs->rules = new;
1169 constrs->rules_all = new_rules;
1170 }
1171 c = &constrs->rules[constrs->rules_num];
1172 c->cond = cond;
1173 c->func = func;
1174 c->var = var;
1175 c->private = private;
1176 k = 0;
1177 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001178 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1179 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001180 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 c->deps[k++] = dep;
1183 if (dep < 0)
1184 break;
1185 dep = va_arg(args, int);
1186 }
1187 constrs->rules_num++;
1188 va_end(args);
1189 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001192EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001195 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001196 * @runtime: PCM runtime instance
1197 * @var: hw_params variable to apply the mask
1198 * @mask: the bitmap mask
1199 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001200 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001201 *
1202 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001204int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 u_int32_t mask)
1206{
Takashi Iwai877211f2005-11-17 13:59:38 +01001207 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1208 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *maskp->bits &= mask;
1210 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1211 if (*maskp->bits == 0)
1212 return -EINVAL;
1213 return 0;
1214}
1215
1216/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001217 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001218 * @runtime: PCM runtime instance
1219 * @var: hw_params variable to apply the mask
1220 * @mask: the 64bit bitmap mask
1221 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001222 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001223 *
1224 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001226int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 u_int64_t mask)
1228{
Takashi Iwai877211f2005-11-17 13:59:38 +01001229 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1230 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 maskp->bits[0] &= (u_int32_t)mask;
1232 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1233 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1234 if (! maskp->bits[0] && ! maskp->bits[1])
1235 return -EINVAL;
1236 return 0;
1237}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001238EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001241 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001242 * @runtime: PCM runtime instance
1243 * @var: hw_params variable to apply the integer constraint
1244 *
1245 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001246 *
1247 * Return: Positive if the value is changed, zero if it's not changed, or a
1248 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001250int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai877211f2005-11-17 13:59:38 +01001252 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return snd_interval_setinteger(constrs_interval(constrs, var));
1254}
1255
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001256EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001259 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001260 * @runtime: PCM runtime instance
1261 * @var: hw_params variable to apply the range
1262 * @min: the minimal value
1263 * @max: the maximal value
1264 *
1265 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001266 *
1267 * Return: Positive if the value is changed, zero if it's not changed, or a
1268 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001270int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 unsigned int min, unsigned int max)
1272{
Takashi Iwai877211f2005-11-17 13:59:38 +01001273 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1274 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 t.min = min;
1276 t.max = max;
1277 t.openmin = t.openmax = 0;
1278 t.integer = 0;
1279 return snd_interval_refine(constrs_interval(constrs, var), &t);
1280}
1281
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001282EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1283
Takashi Iwai877211f2005-11-17 13:59:38 +01001284static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1285 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Takashi Iwai877211f2005-11-17 13:59:38 +01001287 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1289}
1290
1291
1292/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001293 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001294 * @runtime: PCM runtime instance
1295 * @cond: condition bits
1296 * @var: hw_params variable to apply the list constraint
1297 * @l: list
1298 *
1299 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001300 *
1301 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001303int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned int cond,
1305 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001306 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001309 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 var, -1);
1311}
1312
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001313EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1314
Peter Rosinf66f8982015-01-28 15:16:06 +01001315static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1316 struct snd_pcm_hw_rule *rule)
1317{
1318 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1319 return snd_interval_ranges(hw_param_interval(params, rule->var),
1320 r->count, r->ranges, r->mask);
1321}
1322
1323
1324/**
1325 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1326 * @runtime: PCM runtime instance
1327 * @cond: condition bits
1328 * @var: hw_params variable to apply the list of range constraints
1329 * @r: ranges
1330 *
1331 * Apply the list of range constraints to an interval parameter.
1332 *
1333 * Return: Zero if successful, or a negative error code on failure.
1334 */
1335int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1336 unsigned int cond,
1337 snd_pcm_hw_param_t var,
1338 const struct snd_pcm_hw_constraint_ranges *r)
1339{
1340 return snd_pcm_hw_rule_add(runtime, cond, var,
1341 snd_pcm_hw_rule_ranges, (void *)r,
1342 var, -1);
1343}
1344EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1345
Takashi Iwai877211f2005-11-17 13:59:38 +01001346static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1347 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001349 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned int num = 0, den = 0;
1351 int err;
1352 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1353 r->nrats, r->rats, &num, &den);
1354 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1355 params->rate_num = num;
1356 params->rate_den = den;
1357 }
1358 return err;
1359}
1360
1361/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001362 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001363 * @runtime: PCM runtime instance
1364 * @cond: condition bits
1365 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001366 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001367 *
1368 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001370int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 unsigned int cond,
1372 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001373 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001376 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 var, -1);
1378}
1379
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001380EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1381
Takashi Iwai877211f2005-11-17 13:59:38 +01001382static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1383 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001385 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 unsigned int num = 0, den = 0;
1387 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1388 r->nrats, r->rats, &num, &den);
1389 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1390 params->rate_num = num;
1391 params->rate_den = den;
1392 }
1393 return err;
1394}
1395
1396/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001397 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001398 * @runtime: PCM runtime instance
1399 * @cond: condition bits
1400 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001401 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001402 *
1403 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001405int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 unsigned int cond,
1407 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001408 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001411 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 var, -1);
1413}
1414
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001415EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1416
Takashi Iwai877211f2005-11-17 13:59:38 +01001417static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1418 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 unsigned int l = (unsigned long) rule->private;
1421 int width = l & 0xffff;
1422 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001423 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001424
1425 if (!snd_interval_single(i))
1426 return 0;
1427
1428 if ((snd_interval_value(i) == width) ||
1429 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001430 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433}
1434
1435/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001436 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001437 * @runtime: PCM runtime instance
1438 * @cond: condition bits
1439 * @width: sample bits width
1440 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001441 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001442 * This constraint will set the number of most significant bits (msbits) if a
1443 * sample format with the specified width has been select. If width is set to 0
1444 * the msbits will be set for any sample format with a width larger than the
1445 * specified msbits.
1446 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001447 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001449int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned int cond,
1451 unsigned int width,
1452 unsigned int msbits)
1453{
1454 unsigned long l = (msbits << 16) | width;
1455 return snd_pcm_hw_rule_add(runtime, cond, -1,
1456 snd_pcm_hw_rule_msbits,
1457 (void*) l,
1458 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1459}
1460
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001461EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1462
Takashi Iwai877211f2005-11-17 13:59:38 +01001463static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1464 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001467 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001471 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001472 * @runtime: PCM runtime instance
1473 * @cond: condition bits
1474 * @var: hw_params variable to apply the step constraint
1475 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001476 *
1477 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001479int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 unsigned int cond,
1481 snd_pcm_hw_param_t var,
1482 unsigned long step)
1483{
1484 return snd_pcm_hw_rule_add(runtime, cond, var,
1485 snd_pcm_hw_rule_step, (void *) step,
1486 var, -1);
1487}
1488
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001489EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1490
Takashi Iwai877211f2005-11-17 13:59:38 +01001491static 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 -07001492{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001493 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1495 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1496 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1497 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1498 };
1499 return snd_interval_list(hw_param_interval(params, rule->var),
1500 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1501}
1502
1503/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001504 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001505 * @runtime: PCM runtime instance
1506 * @cond: condition bits
1507 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001508 *
1509 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001511int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned int cond,
1513 snd_pcm_hw_param_t var)
1514{
1515 return snd_pcm_hw_rule_add(runtime, cond, var,
1516 snd_pcm_hw_rule_pow2, NULL,
1517 var, -1);
1518}
1519
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001520EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1521
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001522static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1523 struct snd_pcm_hw_rule *rule)
1524{
1525 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1526 struct snd_interval *rate;
1527
1528 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1529 return snd_interval_list(rate, 1, &base_rate, 0);
1530}
1531
1532/**
1533 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1534 * @runtime: PCM runtime instance
1535 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001536 *
1537 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001538 */
1539int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1540 unsigned int base_rate)
1541{
1542 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1543 SNDRV_PCM_HW_PARAM_RATE,
1544 snd_pcm_hw_rule_noresample_func,
1545 (void *)(uintptr_t)base_rate,
1546 SNDRV_PCM_HW_PARAM_RATE, -1);
1547}
1548EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1549
Takashi Iwai877211f2005-11-17 13:59:38 +01001550static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001551 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 if (hw_is_mask(var)) {
1554 snd_mask_any(hw_param_mask(params, var));
1555 params->cmask |= 1 << var;
1556 params->rmask |= 1 << var;
1557 return;
1558 }
1559 if (hw_is_interval(var)) {
1560 snd_interval_any(hw_param_interval(params, var));
1561 params->cmask |= 1 << var;
1562 params->rmask |= 1 << var;
1563 return;
1564 }
1565 snd_BUG();
1566}
1567
Takashi Iwai877211f2005-11-17 13:59:38 +01001568void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 unsigned int k;
1571 memset(params, 0, sizeof(*params));
1572 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1573 _snd_pcm_hw_param_any(params, k);
1574 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1575 _snd_pcm_hw_param_any(params, k);
1576 params->info = ~0U;
1577}
1578
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001579EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001582 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001583 * @params: the hw_params instance
1584 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001585 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001587 * Return: The value for field @var if it's fixed in configuration space
1588 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001590int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1591 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001594 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!snd_mask_single(mask))
1596 return -EINVAL;
1597 if (dir)
1598 *dir = 0;
1599 return snd_mask_value(mask);
1600 }
1601 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001602 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (!snd_interval_single(i))
1604 return -EINVAL;
1605 if (dir)
1606 *dir = i->openmin;
1607 return snd_interval_value(i);
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -EINVAL;
1610}
1611
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001612EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Takashi Iwai877211f2005-11-17 13:59:38 +01001614void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 snd_pcm_hw_param_t var)
1616{
1617 if (hw_is_mask(var)) {
1618 snd_mask_none(hw_param_mask(params, var));
1619 params->cmask |= 1 << var;
1620 params->rmask |= 1 << var;
1621 } else if (hw_is_interval(var)) {
1622 snd_interval_none(hw_param_interval(params, var));
1623 params->cmask |= 1 << var;
1624 params->rmask |= 1 << var;
1625 } else {
1626 snd_BUG();
1627 }
1628}
1629
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001630EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Takashi Iwai877211f2005-11-17 13:59:38 +01001632static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001633 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int changed;
1636 if (hw_is_mask(var))
1637 changed = snd_mask_refine_first(hw_param_mask(params, var));
1638 else if (hw_is_interval(var))
1639 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001640 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (changed) {
1643 params->cmask |= 1 << var;
1644 params->rmask |= 1 << var;
1645 }
1646 return changed;
1647}
1648
1649
1650/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001651 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001652 * @pcm: PCM instance
1653 * @params: the hw_params instance
1654 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001655 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001657 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001659 *
1660 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001662int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1663 struct snd_pcm_hw_params *params,
1664 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 int changed = _snd_pcm_hw_param_first(params, var);
1667 if (changed < 0)
1668 return changed;
1669 if (params->rmask) {
1670 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001671 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 return snd_pcm_hw_param_value(params, var, dir);
1675}
1676
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001677EXPORT_SYMBOL(snd_pcm_hw_param_first);
1678
Takashi Iwai877211f2005-11-17 13:59:38 +01001679static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001680 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int changed;
1683 if (hw_is_mask(var))
1684 changed = snd_mask_refine_last(hw_param_mask(params, var));
1685 else if (hw_is_interval(var))
1686 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001687 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (changed) {
1690 params->cmask |= 1 << var;
1691 params->rmask |= 1 << var;
1692 }
1693 return changed;
1694}
1695
1696
1697/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001698 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001699 * @pcm: PCM instance
1700 * @params: the hw_params instance
1701 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001702 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001704 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001706 *
1707 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001709int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1710 struct snd_pcm_hw_params *params,
1711 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int changed = _snd_pcm_hw_param_last(params, var);
1714 if (changed < 0)
1715 return changed;
1716 if (params->rmask) {
1717 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001718 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 return snd_pcm_hw_param_value(params, var, dir);
1722}
1723
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001724EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001727 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001728 * @pcm: PCM instance
1729 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001731 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * The configuration chosen is that obtained fixing in this order:
1733 * first access, first format, first subformat, min channels,
1734 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001735 *
1736 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001738int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1739 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001741 static int vars[] = {
1742 SNDRV_PCM_HW_PARAM_ACCESS,
1743 SNDRV_PCM_HW_PARAM_FORMAT,
1744 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1745 SNDRV_PCM_HW_PARAM_CHANNELS,
1746 SNDRV_PCM_HW_PARAM_RATE,
1747 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1748 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1749 SNDRV_PCM_HW_PARAM_TICK_TIME,
1750 -1
1751 };
1752 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001754 for (v = vars; *v != -1; v++) {
1755 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1756 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1757 else
1758 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001759 if (snd_BUG_ON(err < 0))
1760 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763}
1764
Takashi Iwai877211f2005-11-17 13:59:38 +01001765static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 void *arg)
1767{
Takashi Iwai877211f2005-11-17 13:59:38 +01001768 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 unsigned long flags;
1770 snd_pcm_stream_lock_irqsave(substream, flags);
1771 if (snd_pcm_running(substream) &&
1772 snd_pcm_update_hw_ptr(substream) >= 0)
1773 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001774 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001776 runtime->hw_ptr_wrap = 0;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 snd_pcm_stream_unlock_irqrestore(substream, flags);
1779 return 0;
1780}
1781
Takashi Iwai877211f2005-11-17 13:59:38 +01001782static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 void *arg)
1784{
Takashi Iwai877211f2005-11-17 13:59:38 +01001785 struct snd_pcm_channel_info *info = arg;
1786 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int width;
1788 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1789 info->offset = -1;
1790 return 0;
1791 }
1792 width = snd_pcm_format_physical_width(runtime->format);
1793 if (width < 0)
1794 return width;
1795 info->offset = 0;
1796 switch (runtime->access) {
1797 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1798 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001799 if ((UINT_MAX/width) < info->channel) {
1800 snd_printd("%s: integer overflow while multiply\n",
1801 __func__);
1802 return -EINVAL;
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 info->first = info->channel * width;
1805 info->step = runtime->channels * width;
1806 break;
1807 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1808 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1809 {
1810 size_t size = runtime->dma_bytes / runtime->channels;
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001811
1812 if ((size > 0) && ((UINT_MAX/(size * 8)) < info->channel)) {
1813 snd_printd("%s: integer overflow while multiply\n",
1814 __func__);
1815 return -EINVAL;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 info->first = info->channel * size * 8;
1818 info->step = width;
1819 break;
1820 }
1821 default:
1822 snd_BUG();
1823 break;
1824 }
1825 return 0;
1826}
1827
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001828static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1829 void *arg)
1830{
1831 struct snd_pcm_hw_params *params = arg;
1832 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001833 int channels;
1834 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001835
1836 params->fifo_size = substream->runtime->hw.fifo_size;
1837 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1838 format = params_format(params);
1839 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001840 frame_size = snd_pcm_format_size(format, channels);
1841 if (frame_size > 0)
1842 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001843 }
1844 return 0;
1845}
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/**
1848 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1849 * @substream: the pcm substream instance
1850 * @cmd: ioctl command
1851 * @arg: ioctl argument
1852 *
1853 * Processes the generic ioctl commands for PCM.
1854 * Can be passed as the ioctl callback for PCM ops.
1855 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001856 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001858int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned int cmd, void *arg)
1860{
1861 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 case SNDRV_PCM_IOCTL1_RESET:
1863 return snd_pcm_lib_ioctl_reset(substream, arg);
1864 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1865 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001866 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1867 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 return -ENXIO;
1870}
1871
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001872EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/**
1875 * snd_pcm_period_elapsed - update the pcm status for the next period
1876 * @substream: the pcm substream instance
1877 *
1878 * This function is called from the interrupt handler when the
1879 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001880 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
1882 * Even if more than one periods have elapsed since the last call, you
1883 * have to call this only once.
1884 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001885void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai877211f2005-11-17 13:59:38 +01001887 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 unsigned long flags;
1889
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001890 if (PCM_RUNTIME_CHECK(substream))
1891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 snd_pcm_stream_lock_irqsave(substream, flags);
1895 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001896 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto _end;
1898
Jie Yang90bbaf62015-10-16 17:57:46 +08001899#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (substream->timer_running)
1901 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001905 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001908EXPORT_SYMBOL(snd_pcm_period_elapsed);
1909
Takashi Iwai13075512008-01-08 18:08:14 +01001910/*
1911 * Wait until avail_min data becomes available
1912 * Returns a negative error code if any error occurs during operation.
1913 * The available space is stored on availp. When err = 0 and avail = 0
1914 * on the capture stream, it indicates the stream is in DRAINING state.
1915 */
David Dillow5daeba32010-06-27 00:13:20 +02001916static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001917 snd_pcm_uframes_t *availp)
1918{
1919 struct snd_pcm_runtime *runtime = substream->runtime;
1920 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1921 wait_queue_t wait;
1922 int err = 0;
1923 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001924 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001925
Arjan van de Ven763437a2011-09-15 08:49:25 +02001926 init_waitqueue_entry(&wait, current);
1927 set_current_state(TASK_INTERRUPTIBLE);
1928 add_wait_queue(&runtime->tsleep, &wait);
1929
Takashi Iwaif2b36142011-05-26 08:09:38 +02001930 if (runtime->no_period_wakeup)
1931 wait_time = MAX_SCHEDULE_TIMEOUT;
1932 else {
1933 wait_time = 10;
1934 if (runtime->rate) {
1935 long t = runtime->period_size * 2 / runtime->rate;
1936 wait_time = max(t, wait_time);
1937 }
1938 wait_time = msecs_to_jiffies(wait_time * 1000);
1939 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001940
Takashi Iwai13075512008-01-08 18:08:14 +01001941 for (;;) {
1942 if (signal_pending(current)) {
1943 err = -ERESTARTSYS;
1944 break;
1945 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001946
1947 /*
1948 * We need to check if space became available already
1949 * (and thus the wakeup happened already) first to close
1950 * the race of space already having become available.
1951 * This check must happen after been added to the waitqueue
1952 * and having current state be INTERRUPTIBLE.
1953 */
1954 if (is_playback)
1955 avail = snd_pcm_playback_avail(runtime);
1956 else
1957 avail = snd_pcm_capture_avail(runtime);
1958 if (avail >= runtime->twake)
1959 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001960 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001961
1962 tout = schedule_timeout(wait_time);
1963
Takashi Iwai13075512008-01-08 18:08:14 +01001964 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001965 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001966 switch (runtime->status->state) {
1967 case SNDRV_PCM_STATE_SUSPENDED:
1968 err = -ESTRPIPE;
1969 goto _endloop;
1970 case SNDRV_PCM_STATE_XRUN:
1971 err = -EPIPE;
1972 goto _endloop;
1973 case SNDRV_PCM_STATE_DRAINING:
1974 if (is_playback)
1975 err = -EPIPE;
1976 else
1977 avail = 0; /* indicate draining */
1978 goto _endloop;
1979 case SNDRV_PCM_STATE_OPEN:
1980 case SNDRV_PCM_STATE_SETUP:
1981 case SNDRV_PCM_STATE_DISCONNECTED:
1982 err = -EBADFD;
1983 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001984 case SNDRV_PCM_STATE_PAUSED:
1985 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001986 }
1987 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001988 pcm_dbg(substream->pcm,
1989 "%s write error (DMA or IRQ trouble?)\n",
1990 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001991 err = -EIO;
1992 break;
1993 }
Takashi Iwai13075512008-01-08 18:08:14 +01001994 }
1995 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001996 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001997 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001998 *availp = avail;
1999 return err;
2000}
2001
Takashi Iwai877211f2005-11-17 13:59:38 +01002002static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned int hwoff,
2004 unsigned long data, unsigned int off,
2005 snd_pcm_uframes_t frames)
2006{
Takashi Iwai877211f2005-11-17 13:59:38 +01002007 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int err;
2009 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2010 if (substream->ops->copy) {
2011 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2012 return err;
2013 } else {
2014 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2016 return -EFAULT;
2017 }
2018 return 0;
2019}
2020
Takashi Iwai877211f2005-11-17 13:59:38 +01002021typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 unsigned long data, unsigned int off,
2023 snd_pcm_uframes_t size);
2024
Takashi Iwai877211f2005-11-17 13:59:38 +01002025static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 unsigned long data,
2027 snd_pcm_uframes_t size,
2028 int nonblock,
2029 transfer_f transfer)
2030{
Takashi Iwai877211f2005-11-17 13:59:38 +01002031 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 snd_pcm_uframes_t xfer = 0;
2033 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002034 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int err = 0;
2036
2037 if (size == 0)
2038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 snd_pcm_stream_lock_irq(substream);
2041 switch (runtime->status->state) {
2042 case SNDRV_PCM_STATE_PREPARED:
2043 case SNDRV_PCM_STATE_RUNNING:
2044 case SNDRV_PCM_STATE_PAUSED:
2045 break;
2046 case SNDRV_PCM_STATE_XRUN:
2047 err = -EPIPE;
2048 goto _end_unlock;
2049 case SNDRV_PCM_STATE_SUSPENDED:
2050 err = -ESTRPIPE;
2051 goto _end_unlock;
2052 default:
2053 err = -EBADFD;
2054 goto _end_unlock;
2055 }
2056
David Dillow5daeba32010-06-27 00:13:20 +02002057 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002058 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2059 snd_pcm_update_hw_ptr(substream);
2060 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 while (size > 0) {
2062 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002064 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (nonblock) {
2066 err = -EAGAIN;
2067 goto _end_unlock;
2068 }
David Dillow5daeba32010-06-27 00:13:20 +02002069 runtime->twake = min_t(snd_pcm_uframes_t, size,
2070 runtime->control->avail_min ? : 1);
2071 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002072 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 frames = size > avail ? avail : size;
2076 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2077 if (frames > cont)
2078 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002079 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002080 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002081 snd_pcm_stream_unlock_irq(substream);
2082 return -EINVAL;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 appl_ptr = runtime->control->appl_ptr;
2085 appl_ofs = appl_ptr % runtime->buffer_size;
2086 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002087 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002089 if (err < 0)
2090 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 switch (runtime->status->state) {
2092 case SNDRV_PCM_STATE_XRUN:
2093 err = -EPIPE;
2094 goto _end_unlock;
2095 case SNDRV_PCM_STATE_SUSPENDED:
2096 err = -ESTRPIPE;
2097 goto _end_unlock;
2098 default:
2099 break;
2100 }
2101 appl_ptr += frames;
2102 if (appl_ptr >= runtime->boundary)
2103 appl_ptr -= runtime->boundary;
2104 runtime->control->appl_ptr = appl_ptr;
2105 if (substream->ops->ack)
2106 substream->ops->ack(substream);
2107
2108 offset += frames;
2109 size -= frames;
2110 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002111 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2113 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2114 err = snd_pcm_start(substream);
2115 if (err < 0)
2116 goto _end_unlock;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002120 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002121 if (xfer > 0 && err >= 0)
2122 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2125}
2126
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002127/* sanity-check for read/write methods */
2128static int pcm_sanity_check(struct snd_pcm_substream *substream)
2129{
2130 struct snd_pcm_runtime *runtime;
2131 if (PCM_RUNTIME_CHECK(substream))
2132 return -ENXIO;
Liam Girdwoodf2727332016-01-29 02:27:17 +05302133 /* TODO: consider and -EINVAL here */
2134 if (substream->hw_no_buffer)
2135 snd_printd("%s: warning this PCM is host less\n", __func__);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002136 runtime = substream->runtime;
2137 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2138 return -EINVAL;
2139 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2140 return -EBADFD;
2141 return 0;
2142}
2143
Takashi Iwai877211f2005-11-17 13:59:38 +01002144snd_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 -07002145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002148 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002150 err = pcm_sanity_check(substream);
2151 if (err < 0)
2152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002154 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2157 runtime->channels > 1)
2158 return -EINVAL;
2159 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2160 snd_pcm_lib_write_transfer);
2161}
2162
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002163EXPORT_SYMBOL(snd_pcm_lib_write);
2164
Takashi Iwai877211f2005-11-17 13:59:38 +01002165static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 unsigned int hwoff,
2167 unsigned long data, unsigned int off,
2168 snd_pcm_uframes_t frames)
2169{
Takashi Iwai877211f2005-11-17 13:59:38 +01002170 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int err;
2172 void __user **bufs = (void __user **)data;
2173 int channels = runtime->channels;
2174 int c;
2175 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002176 if (snd_BUG_ON(!substream->ops->silence))
2177 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 for (c = 0; c < channels; ++c, ++bufs) {
2179 if (*bufs == NULL) {
2180 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2181 return err;
2182 } else {
2183 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2184 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2185 return err;
2186 }
2187 }
2188 } else {
2189 /* default transfer behaviour */
2190 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 for (c = 0; c < channels; ++c, ++bufs) {
2192 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2193 if (*bufs == NULL) {
2194 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2195 } else {
2196 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2197 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2198 return -EFAULT;
2199 }
2200 }
2201 }
2202 return 0;
2203}
2204
Takashi Iwai877211f2005-11-17 13:59:38 +01002205snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 void __user **bufs,
2207 snd_pcm_uframes_t frames)
2208{
Takashi Iwai877211f2005-11-17 13:59:38 +01002209 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002213 err = pcm_sanity_check(substream);
2214 if (err < 0)
2215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002217 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2220 return -EINVAL;
2221 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2222 nonblock, snd_pcm_lib_writev_transfer);
2223}
2224
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002225EXPORT_SYMBOL(snd_pcm_lib_writev);
2226
Takashi Iwai877211f2005-11-17 13:59:38 +01002227static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 unsigned int hwoff,
2229 unsigned long data, unsigned int off,
2230 snd_pcm_uframes_t frames)
2231{
Takashi Iwai877211f2005-11-17 13:59:38 +01002232 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int err;
2234 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2235 if (substream->ops->copy) {
2236 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2237 return err;
2238 } else {
2239 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2241 return -EFAULT;
2242 }
2243 return 0;
2244}
2245
Takashi Iwai877211f2005-11-17 13:59:38 +01002246static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned long data,
2248 snd_pcm_uframes_t size,
2249 int nonblock,
2250 transfer_f transfer)
2251{
Takashi Iwai877211f2005-11-17 13:59:38 +01002252 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 snd_pcm_uframes_t xfer = 0;
2254 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002255 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int err = 0;
2257
2258 if (size == 0)
2259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 snd_pcm_stream_lock_irq(substream);
2262 switch (runtime->status->state) {
2263 case SNDRV_PCM_STATE_PREPARED:
2264 if (size >= runtime->start_threshold) {
2265 err = snd_pcm_start(substream);
2266 if (err < 0)
2267 goto _end_unlock;
2268 }
2269 break;
2270 case SNDRV_PCM_STATE_DRAINING:
2271 case SNDRV_PCM_STATE_RUNNING:
2272 case SNDRV_PCM_STATE_PAUSED:
2273 break;
2274 case SNDRV_PCM_STATE_XRUN:
2275 err = -EPIPE;
2276 goto _end_unlock;
2277 case SNDRV_PCM_STATE_SUSPENDED:
2278 err = -ESTRPIPE;
2279 goto _end_unlock;
2280 default:
2281 err = -EBADFD;
2282 goto _end_unlock;
2283 }
2284
David Dillow5daeba32010-06-27 00:13:20 +02002285 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002286 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2287 snd_pcm_update_hw_ptr(substream);
2288 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 while (size > 0) {
2290 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002292 if (!avail) {
2293 if (runtime->status->state ==
2294 SNDRV_PCM_STATE_DRAINING) {
2295 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto _end_unlock;
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (nonblock) {
2299 err = -EAGAIN;
2300 goto _end_unlock;
2301 }
David Dillow5daeba32010-06-27 00:13:20 +02002302 runtime->twake = min_t(snd_pcm_uframes_t, size,
2303 runtime->control->avail_min ? : 1);
2304 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002305 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002307 if (!avail)
2308 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 frames = size > avail ? avail : size;
2311 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2312 if (frames > cont)
2313 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002314 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002315 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002316 snd_pcm_stream_unlock_irq(substream);
2317 return -EINVAL;
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 appl_ptr = runtime->control->appl_ptr;
2320 appl_ofs = appl_ptr % runtime->buffer_size;
2321 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002322 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002324 if (err < 0)
2325 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 switch (runtime->status->state) {
2327 case SNDRV_PCM_STATE_XRUN:
2328 err = -EPIPE;
2329 goto _end_unlock;
2330 case SNDRV_PCM_STATE_SUSPENDED:
2331 err = -ESTRPIPE;
2332 goto _end_unlock;
2333 default:
2334 break;
2335 }
2336 appl_ptr += frames;
2337 if (appl_ptr >= runtime->boundary)
2338 appl_ptr -= runtime->boundary;
2339 runtime->control->appl_ptr = appl_ptr;
2340 if (substream->ops->ack)
2341 substream->ops->ack(substream);
2342
2343 offset += frames;
2344 size -= frames;
2345 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002346 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002349 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002350 if (xfer > 0 && err >= 0)
2351 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2354}
2355
Takashi Iwai877211f2005-11-17 13:59:38 +01002356snd_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 -07002357{
Takashi Iwai877211f2005-11-17 13:59:38 +01002358 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002362 err = pcm_sanity_check(substream);
2363 if (err < 0)
2364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002366 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2368 return -EINVAL;
2369 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2370}
2371
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002372EXPORT_SYMBOL(snd_pcm_lib_read);
2373
Takashi Iwai877211f2005-11-17 13:59:38 +01002374static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 unsigned int hwoff,
2376 unsigned long data, unsigned int off,
2377 snd_pcm_uframes_t frames)
2378{
Takashi Iwai877211f2005-11-17 13:59:38 +01002379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int err;
2381 void __user **bufs = (void __user **)data;
2382 int channels = runtime->channels;
2383 int c;
2384 if (substream->ops->copy) {
2385 for (c = 0; c < channels; ++c, ++bufs) {
2386 char __user *buf;
2387 if (*bufs == NULL)
2388 continue;
2389 buf = *bufs + samples_to_bytes(runtime, off);
2390 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2391 return err;
2392 }
2393 } else {
2394 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 for (c = 0; c < channels; ++c, ++bufs) {
2396 char *hwbuf;
2397 char __user *buf;
2398 if (*bufs == NULL)
2399 continue;
2400
2401 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2402 buf = *bufs + samples_to_bytes(runtime, off);
2403 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2404 return -EFAULT;
2405 }
2406 }
2407 return 0;
2408}
2409
Takashi Iwai877211f2005-11-17 13:59:38 +01002410snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 void __user **bufs,
2412 snd_pcm_uframes_t frames)
2413{
Takashi Iwai877211f2005-11-17 13:59:38 +01002414 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002418 err = pcm_sanity_check(substream);
2419 if (err < 0)
2420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2423 return -EBADFD;
2424
Takashi Iwai0df63e42006-04-28 15:13:41 +02002425 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2427 return -EINVAL;
2428 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002432
2433/*
2434 * standard channel mapping helpers
2435 */
2436
2437/* default channel maps for multi-channel playbacks, up to 8 channels */
2438const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2439 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002440 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002441 { .channels = 2,
2442 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2443 { .channels = 4,
2444 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2445 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2446 { .channels = 6,
2447 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2448 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2449 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2450 { .channels = 8,
2451 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2452 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2453 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2454 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2455 { }
2456};
2457EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2458
2459/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2460const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2461 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002462 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002463 { .channels = 2,
2464 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2465 { .channels = 4,
2466 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2467 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2468 { .channels = 6,
2469 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2470 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2471 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2472 { .channels = 8,
2473 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2474 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2475 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2476 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2477 { }
2478};
2479EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2480
2481static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2482{
2483 if (ch > info->max_channels)
2484 return false;
2485 return !info->channel_mask || (info->channel_mask & (1U << ch));
2486}
2487
2488static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_info *uinfo)
2490{
2491 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2492
2493 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2494 uinfo->count = 0;
2495 uinfo->count = info->max_channels;
2496 uinfo->value.integer.min = 0;
2497 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2498 return 0;
2499}
2500
2501/* get callback for channel map ctl element
2502 * stores the channel position firstly matching with the current channels
2503 */
2504static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_value *ucontrol)
2506{
2507 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2508 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2509 struct snd_pcm_substream *substream;
2510 const struct snd_pcm_chmap_elem *map;
2511
Takashi Iwai552a14a2017-06-14 16:20:32 +02002512 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002513 return -EINVAL;
2514 substream = snd_pcm_chmap_substream(info, idx);
2515 if (!substream)
2516 return -ENODEV;
2517 memset(ucontrol->value.integer.value, 0,
2518 sizeof(ucontrol->value.integer.value));
2519 if (!substream->runtime)
2520 return 0; /* no channels set */
2521 for (map = info->chmap; map->channels; map++) {
2522 int i;
2523 if (map->channels == substream->runtime->channels &&
2524 valid_chmap_channels(info, map->channels)) {
2525 for (i = 0; i < map->channels; i++)
2526 ucontrol->value.integer.value[i] = map->map[i];
2527 return 0;
2528 }
2529 }
2530 return -EINVAL;
2531}
2532
2533/* tlv callback for channel map ctl element
2534 * expands the pre-defined channel maps in a form of TLV
2535 */
2536static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2537 unsigned int size, unsigned int __user *tlv)
2538{
2539 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2540 const struct snd_pcm_chmap_elem *map;
2541 unsigned int __user *dst;
2542 int c, count = 0;
2543
Takashi Iwai552a14a2017-06-14 16:20:32 +02002544 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002545 return -EINVAL;
2546 if (size < 8)
2547 return -ENOMEM;
2548 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2549 return -EFAULT;
2550 size -= 8;
2551 dst = tlv + 2;
2552 for (map = info->chmap; map->channels; map++) {
2553 int chs_bytes = map->channels * 4;
2554 if (!valid_chmap_channels(info, map->channels))
2555 continue;
2556 if (size < 8)
2557 return -ENOMEM;
2558 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2559 put_user(chs_bytes, dst + 1))
2560 return -EFAULT;
2561 dst += 2;
2562 size -= 8;
2563 count += 8;
2564 if (size < chs_bytes)
2565 return -ENOMEM;
2566 size -= chs_bytes;
2567 count += chs_bytes;
2568 for (c = 0; c < map->channels; c++) {
2569 if (put_user(map->map[c], dst))
2570 return -EFAULT;
2571 dst++;
2572 }
2573 }
2574 if (put_user(count, tlv + 1))
2575 return -EFAULT;
2576 return 0;
2577}
2578
2579static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2580{
2581 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2582 info->pcm->streams[info->stream].chmap_kctl = NULL;
2583 kfree(info);
2584}
2585
Banajit Goswami55b24c52016-11-21 19:08:27 -08002586static int pcm_volume_ctl_info(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_info *uinfo)
2588{
2589 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2590 uinfo->count = 1;
2591 uinfo->value.integer.min = 0;
2592 uinfo->value.integer.max = 0x2000;
2593 return 0;
2594}
2595
2596static void pcm_volume_ctl_private_free(struct snd_kcontrol *kcontrol)
2597{
2598 struct snd_pcm_volume *info = snd_kcontrol_chip(kcontrol);
2599
2600 info->pcm->streams[info->stream].vol_kctl = NULL;
2601 kfree(info);
2602}
2603
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002604/**
2605 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2606 * @pcm: the assigned PCM instance
2607 * @stream: stream direction
2608 * @chmap: channel map elements (for query)
2609 * @max_channels: the max number of channels for the stream
2610 * @private_value: the value passed to each kcontrol's private_value field
2611 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2612 *
2613 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002614 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002615 */
2616int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2617 const struct snd_pcm_chmap_elem *chmap,
2618 int max_channels,
2619 unsigned long private_value,
2620 struct snd_pcm_chmap **info_ret)
2621{
2622 struct snd_pcm_chmap *info;
2623 struct snd_kcontrol_new knew = {
2624 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2625 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002626 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2627 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2628 .info = pcm_chmap_ctl_info,
2629 .get = pcm_chmap_ctl_get,
2630 .tlv.c = pcm_chmap_ctl_tlv,
2631 };
2632 int err;
2633
Takashi Iwai8d879be2016-05-10 16:07:40 +02002634 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2635 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002636 info = kzalloc(sizeof(*info), GFP_KERNEL);
2637 if (!info)
2638 return -ENOMEM;
2639 info->pcm = pcm;
2640 info->stream = stream;
2641 info->chmap = chmap;
2642 info->max_channels = max_channels;
2643 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2644 knew.name = "Playback Channel Map";
2645 else
2646 knew.name = "Capture Channel Map";
2647 knew.device = pcm->device;
2648 knew.count = pcm->streams[stream].substream_count;
2649 knew.private_value = private_value;
2650 info->kctl = snd_ctl_new1(&knew, info);
2651 if (!info->kctl) {
2652 kfree(info);
2653 return -ENOMEM;
2654 }
2655 info->kctl->private_free = pcm_chmap_ctl_private_free;
2656 err = snd_ctl_add(pcm->card, info->kctl);
2657 if (err < 0)
2658 return err;
2659 pcm->streams[stream].chmap_kctl = info->kctl;
2660 if (info_ret)
2661 *info_ret = info;
2662 return 0;
2663}
2664EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
Banajit Goswami55b24c52016-11-21 19:08:27 -08002665
2666/**
2667 * snd_pcm_add_volume_ctls - create volume control elements
2668 * @pcm: the assigned PCM instance
2669 * @stream: stream direction
2670 * @max_length: the max length of the volume parameter of stream
2671 * @private_value: the value passed to each kcontrol's private_value field
2672 * @info_ret: store struct snd_pcm_volume instance if non-NULL
2673 *
2674 * Create volume control elements assigned to the given PCM stream(s).
2675 * Returns zero if succeed, or a negative error value.
2676 */
2677int snd_pcm_add_volume_ctls(struct snd_pcm *pcm, int stream,
2678 const struct snd_pcm_volume_elem *volume,
2679 int max_length,
2680 unsigned long private_value,
2681 struct snd_pcm_volume **info_ret)
2682{
2683 struct snd_pcm_volume *info;
2684 struct snd_kcontrol_new knew = {
2685 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2686 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2687 SNDRV_CTL_ELEM_ACCESS_READWRITE,
2688 .info = pcm_volume_ctl_info,
2689 };
2690 int err;
2691 int size;
2692
2693 info = kzalloc(sizeof(*info), GFP_KERNEL);
2694 if (!info)
2695 return -ENOMEM;
2696 info->pcm = pcm;
2697 info->stream = stream;
2698 info->volume = volume;
2699 info->max_length = max_length;
2700 size = sizeof("Playback ") + sizeof(" Volume") +
2701 STRING_LENGTH_OF_INT*sizeof(char) + 1;
2702 knew.name = kzalloc(size, GFP_KERNEL);
2703 if (!knew.name) {
2704 kfree(info);
2705 return -ENOMEM;
2706 }
2707 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2708 snprintf((char *)knew.name, size, "%s %d %s",
2709 "Playback", pcm->device, "Volume");
2710 else
2711 snprintf((char *)knew.name, size, "%s %d %s",
2712 "Capture", pcm->device, "Volume");
2713 knew.device = pcm->device;
2714 knew.count = pcm->streams[stream].substream_count;
2715 knew.private_value = private_value;
2716 info->kctl = snd_ctl_new1(&knew, info);
2717 if (!info->kctl) {
2718 kfree(info);
2719 kfree(knew.name);
2720 return -ENOMEM;
2721 }
2722 info->kctl->private_free = pcm_volume_ctl_private_free;
2723 err = snd_ctl_add(pcm->card, info->kctl);
2724 if (err < 0) {
2725 kfree(info);
2726 kfree(knew.name);
2727 return -ENOMEM;
2728 }
2729 pcm->streams[stream].vol_kctl = info->kctl;
2730 if (info_ret)
2731 *info_ret = info;
2732 kfree(knew.name);
2733 return 0;
2734}
2735EXPORT_SYMBOL(snd_pcm_add_volume_ctls);
Banajit Goswamicec03b62016-11-21 19:22:56 -08002736
2737static int pcm_usr_ctl_info(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_info *uinfo)
2739{
2740 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2741 uinfo->count = MAX_USR_CTRL_CNT;
2742 uinfo->value.integer.min = 0;
2743 uinfo->value.integer.max = INT_MAX;
2744 return 0;
2745}
2746
2747static void pcm_usr_ctl_private_free(struct snd_kcontrol *kcontrol)
2748{
2749 struct snd_pcm_usr *info = snd_kcontrol_chip(kcontrol);
2750
2751 info->pcm->streams[info->stream].usr_kctl = NULL;
2752 kfree(info);
2753}
2754
2755/**
2756 * snd_pcm_add_usr_ctls - create user control elements
2757 * @pcm: the assigned PCM instance
2758 * @stream: stream direction
2759 * @max_length: the max length of the user parameter of stream
2760 * @private_value: the value passed to each kcontrol's private_value field
2761 * @info_ret: store struct snd_pcm_usr instance if non-NULL
2762 *
2763 * Create usr control elements assigned to the given PCM stream(s).
2764 * Returns zero if succeed, or a negative error value.
2765 */
2766int snd_pcm_add_usr_ctls(struct snd_pcm *pcm, int stream,
2767 const struct snd_pcm_usr_elem *usr,
2768 int max_length, int max_kctrl_str_len,
2769 unsigned long private_value,
2770 struct snd_pcm_usr **info_ret)
2771{
2772 struct snd_pcm_usr *info;
2773 struct snd_kcontrol_new knew = {
2774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2775 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2776 .info = pcm_usr_ctl_info,
2777 };
2778 int err;
2779 char *buf;
2780
2781 info = kzalloc(sizeof(*info), GFP_KERNEL);
2782 if (!info)
2783 return -ENOMEM;
2784
2785 info->pcm = pcm;
2786 info->stream = stream;
2787 info->usr = usr;
2788 info->max_length = max_length;
2789 buf = kzalloc(max_kctrl_str_len, GFP_KERNEL);
2790 if (!buf) {
2791 pr_err("%s: buffer allocation failed\n", __func__);
2792 kfree(info);
2793 return -ENOMEM;
2794 }
2795 knew.name = buf;
2796 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2797 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2798 "Playback", pcm->device, "User kcontrol");
2799 else
2800 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2801 "Capture", pcm->device, "User kcontrol");
2802 knew.device = pcm->device;
2803 knew.count = pcm->streams[stream].substream_count;
2804 knew.private_value = private_value;
2805 info->kctl = snd_ctl_new1(&knew, info);
2806 if (!info->kctl) {
2807 kfree(info);
2808 kfree(knew.name);
2809 pr_err("%s: snd_ctl_new failed\n", __func__);
2810 return -ENOMEM;
2811 }
2812 info->kctl->private_free = pcm_usr_ctl_private_free;
2813 err = snd_ctl_add(pcm->card, info->kctl);
2814 if (err < 0) {
2815 kfree(info);
2816 kfree(knew.name);
2817 pr_err("%s: snd_ctl_add failed:%d\n", __func__,
2818 err);
2819 return -ENOMEM;
2820 }
2821 pcm->streams[stream].usr_kctl = info->kctl;
2822 if (info_ret)
2823 *info_ret = info;
2824 kfree(knew.name);
2825 return 0;
2826}
2827EXPORT_SYMBOL(snd_pcm_add_usr_ctls);