blob: 9ccf6a54fa8897b384d8ef1eed688a2b3e24d025 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/time.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020025#include <linux/math64.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020029#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/info.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/timer.h>
34
Takashi Iwaif5914902014-11-04 12:45:59 +010035#ifdef CONFIG_SND_PCM_XRUN_DEBUG
36#define CREATE_TRACE_POINTS
37#include "pcm_trace.h"
38#else
39#define trace_hwptr(substream, pos, in_interrupt)
40#define trace_xrun(substream)
41#define trace_hw_ptr_error(substream, reason)
42#endif
43
Banajit Goswami55b24c52016-11-21 19:08:27 -080044#define STRING_LENGTH_OF_INT 12
Banajit Goswamicec03b62016-11-21 19:22:56 -080045#define MAX_USR_CTRL_CNT 128
Banajit Goswami55b24c52016-11-21 19:08:27 -080046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * fill ring buffer with silence
49 * runtime->silence_start: starting pointer to silence area
50 * runtime->silence_filled: size filled with silence
51 * runtime->silence_threshold: threshold from application
52 * runtime->silence_size: maximal size from application
53 *
54 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
55 */
Takashi Iwai877211f2005-11-17 13:59:38 +010056void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Takashi Iwai877211f2005-11-17 13:59:38 +010058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 snd_pcm_uframes_t frames, ofs, transfer;
60
61 if (runtime->silence_size < runtime->boundary) {
62 snd_pcm_sframes_t noise_dist, n;
63 if (runtime->silence_start != runtime->control->appl_ptr) {
64 n = runtime->control->appl_ptr - runtime->silence_start;
65 if (n < 0)
66 n += runtime->boundary;
67 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
68 runtime->silence_filled -= n;
69 else
70 runtime->silence_filled = 0;
71 runtime->silence_start = runtime->control->appl_ptr;
72 }
Takashi Iwai235475c2005-12-07 15:28:07 +010073 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
76 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
77 return;
78 frames = runtime->silence_threshold - noise_dist;
79 if (frames > runtime->silence_size)
80 frames = runtime->silence_size;
81 } else {
82 if (new_hw_ptr == ULONG_MAX) { /* initialization */
83 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020084 if (avail > runtime->buffer_size)
85 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 runtime->silence_filled = avail > 0 ? avail : 0;
87 runtime->silence_start = (runtime->status->hw_ptr +
88 runtime->silence_filled) %
89 runtime->boundary;
90 } else {
91 ofs = runtime->status->hw_ptr;
92 frames = new_hw_ptr - ofs;
93 if ((snd_pcm_sframes_t)frames < 0)
94 frames += runtime->boundary;
95 runtime->silence_filled -= frames;
96 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
97 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020098 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200100 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 frames = runtime->buffer_size - runtime->silence_filled;
104 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200105 if (snd_BUG_ON(frames > runtime->buffer_size))
106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (frames == 0)
108 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200109 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 while (frames > 0) {
111 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
112 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
113 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
114 if (substream->ops->silence) {
115 int err;
116 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200117 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } else {
119 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
120 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
121 }
122 } else {
123 unsigned int c;
124 unsigned int channels = runtime->channels;
125 if (substream->ops->silence) {
126 for (c = 0; c < channels; ++c) {
127 int err;
128 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200129 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 } else {
132 size_t dma_csize = runtime->dma_bytes / channels;
133 for (c = 0; c < channels; ++c) {
134 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
135 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
136 }
137 }
138 }
139 runtime->silence_filled += transfer;
140 frames -= transfer;
141 ofs = 0;
142 }
143}
144
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200145#ifdef CONFIG_SND_DEBUG
146void snd_pcm_debug_name(struct snd_pcm_substream *substream,
Takashi Iwaic0070112009-06-08 15:58:48 +0200147 char *name, size_t len)
148{
149 snprintf(name, len, "pcmC%dD%d%c:%d",
150 substream->pcm->card->number,
151 substream->pcm->device,
152 substream->stream ? 'c' : 'p',
153 substream->number);
154}
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200155EXPORT_SYMBOL(snd_pcm_debug_name);
156#endif
Takashi Iwaic0070112009-06-08 15:58:48 +0200157
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100158#define XRUN_DEBUG_BASIC (1<<0)
159#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
160#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100161
162#ifdef CONFIG_SND_PCM_XRUN_DEBUG
163
164#define xrun_debug(substream, mask) \
165 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200166#else
167#define xrun_debug(substream, mask) 0
168#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100169
170#define dump_stack_on_xrun(substream) do { \
171 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
172 dump_stack(); \
173 } while (0)
174
Takashi Iwai877211f2005-11-17 13:59:38 +0100175static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200177 struct snd_pcm_runtime *runtime = substream->runtime;
178
Takashi Iwaif5914902014-11-04 12:45:59 +0100179 trace_xrun(substream);
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200180 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
181 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100183 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200184 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200185 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100186 pcm_warn(substream->pcm, "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100187 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Jarkko Nikula0f170142010-03-26 16:07:25 +0200191#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwaif5914902014-11-04 12:45:59 +0100192#define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100193 do { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100194 trace_hw_ptr_error(substream, reason); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100195 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100196 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
197 (in_interrupt) ? 'Q' : 'P', ##args); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100198 dump_stack_on_xrun(substream); \
199 } \
200 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100202#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
203
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100204#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100205
206#endif
207
Jaroslav Kysela12509322010-01-07 15:36:31 +0100208int snd_pcm_update_state(struct snd_pcm_substream *substream,
209 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 snd_pcm_uframes_t avail;
212
213 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
214 avail = snd_pcm_playback_avail(runtime);
215 else
216 avail = snd_pcm_capture_avail(runtime);
217 if (avail > runtime->avail_max)
218 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200219 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
220 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200222 return -EPIPE;
223 }
224 } else {
225 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200227 return -EPIPE;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
David Dillow5daeba32010-06-27 00:13:20 +0200230 if (runtime->twake) {
231 if (avail >= runtime->twake)
232 wake_up(&runtime->tsleep);
233 } else if (avail >= runtime->control->avail_min)
234 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600238static void update_audio_tstamp(struct snd_pcm_substream *substream,
239 struct timespec *curr_tstamp,
240 struct timespec *audio_tstamp)
241{
242 struct snd_pcm_runtime *runtime = substream->runtime;
243 u64 audio_frames, audio_nsecs;
244 struct timespec driver_tstamp;
245
246 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
247 return;
248
249 if (!(substream->ops->get_time_info) ||
250 (runtime->audio_tstamp_report.actual_type ==
251 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
252
253 /*
254 * provide audio timestamp derived from pointer position
255 * add delay only if requested
256 */
257
258 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
259
260 if (runtime->audio_tstamp_config.report_delay) {
261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
262 audio_frames -= runtime->delay;
263 else
264 audio_frames += runtime->delay;
265 }
266 audio_nsecs = div_u64(audio_frames * 1000000000LL,
267 runtime->rate);
268 *audio_tstamp = ns_to_timespec(audio_nsecs);
269 }
Henrik Eriksson14eb4542017-11-21 09:29:28 +0100270 if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
271 runtime->status->audio_tstamp = *audio_tstamp;
272 runtime->status->tstamp = *curr_tstamp;
273 }
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600274
275 /*
276 * re-take a driver timestamp to let apps detect if the reference tstamp
277 * read by low-level hardware was provided with a delay
278 */
279 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
280 runtime->driver_tstamp = driver_tstamp;
281}
282
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100283static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
284 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Takashi Iwai877211f2005-11-17 13:59:38 +0100286 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100288 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200289 snd_pcm_sframes_t hdelta, delta;
290 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500291 unsigned long curr_jiffies;
292 struct timespec curr_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500293 struct timespec audio_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500294 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200296 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500297
298 /*
299 * group pointer, time and jiffies reads to allow for more
300 * accurate correlations/corrections.
301 * The values are stored at the end of this routine after
302 * corrections for hw_ptr position
303 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100304 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500305 curr_jiffies = jiffies;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500306 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600307 if ((substream->ops->get_time_info) &&
308 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
309 substream->ops->get_time_info(substream, &curr_tstamp,
310 &audio_tstamp,
311 &runtime->audio_tstamp_config,
312 &runtime->audio_tstamp_report);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500313
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600314 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
315 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
316 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
317 } else
318 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (pos == SNDRV_PCM_POS_XRUN) {
322 xrun(substream);
323 return -EPIPE;
324 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100325 if (pos >= runtime->buffer_size) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100326 if (printk_ratelimit()) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100327 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200328 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 pcm_err(substream->pcm,
Takashi Iwai0ab1ace2016-03-10 20:56:20 +0100330 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
Takashi Iwai09e56df2014-02-04 18:19:48 +0100331 name, pos, runtime->buffer_size,
332 runtime->period_size);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100333 }
334 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200335 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100336 pos -= pos % runtime->min_align;
Takashi Iwaif5914902014-11-04 12:45:59 +0100337 trace_hwptr(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100338 hw_base = runtime->hw_ptr_base;
339 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100340 if (in_interrupt) {
341 /* we know that one period was processed */
342 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100343 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100344 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200345 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500346 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Koro Chen13a98832015-05-13 22:39:03 +0800347 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200348 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500349 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200350 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500351 crossed_boundary++;
352 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200353 new_hw_ptr = hw_base + pos;
354 goto __delta;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100358 /* new_hw_ptr might be lower than old_hw_ptr in case when */
359 /* pointer crosses the end of the ring buffer */
360 if (new_hw_ptr < old_hw_ptr) {
361 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500362 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100363 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500364 crossed_boundary++;
365 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100366 new_hw_ptr = hw_base + pos;
367 }
368 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200369 delta = new_hw_ptr - old_hw_ptr;
370 if (delta < 0)
371 delta += runtime->boundary;
Clemens Ladischab69a492010-11-15 10:46:23 +0100372
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100373 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200374 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100375 /*
376 * Without regular period interrupts, we have to check
377 * the elapsed time to detect xruns.
378 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500379 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Preetam Singh Ranawate12ae3c2017-01-17 16:24:40 +0530380 if ((jdelta < runtime->hw_ptr_buffer_jiffies / 2) ||
381 (runtime->hw_ptr_buffer_jiffies <= 0))
Clemens Ladisch47228e42010-11-18 09:53:07 +0100382 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100383 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200384 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
385 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100386 delta += runtime->buffer_size;
387 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500388 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100389 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500390 crossed_boundary++;
391 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100392 new_hw_ptr = hw_base + pos;
393 hdelta -= runtime->hw_ptr_buffer_jiffies;
394 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100395 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100396 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100397
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100398 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100399 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100400 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
401 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
402 substream->stream, (long)pos,
403 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100404 return 0;
405 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200406
407 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100408 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200409 goto no_jiffies_check;
410
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200411 /* Skip the jiffies check for hardwares with BATCH flag.
412 * Such hardware usually just increases the position at each IRQ,
413 * thus it can't give any strange position.
414 */
415 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
416 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100417 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200418 if (hdelta < runtime->delay)
419 goto no_jiffies_check;
420 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500421 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200422 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
423 delta = jdelta /
424 (((runtime->period_size * HZ) / runtime->rate)
425 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100426 /* move new_hw_ptr according jiffies not pos variable */
427 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100428 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100429 /* use loop to avoid checks for delta overflows */
430 /* the delta value is small or zero in most cases */
431 while (delta > 0) {
432 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500433 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100434 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500435 crossed_boundary--;
436 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100437 delta--;
438 }
439 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100440 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
441 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200442 (long)pos, (long)hdelta,
443 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100444 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100445 (unsigned long)old_hw_ptr,
446 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100447 /* reset values to proper state */
448 delta = 0;
449 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200450 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200451 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200452 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100453 hw_ptr_error(substream, in_interrupt,
454 "Lost interrupts?",
455 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100456 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100457 (long)new_hw_ptr,
458 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100459 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100460
Clemens Ladischab69a492010-11-15 10:46:23 +0100461 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600462 if (runtime->status->hw_ptr == new_hw_ptr) {
463 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100464 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600465 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
468 runtime->silence_size > 0)
469 snd_pcm_playback_silence(substream, new_hw_ptr);
470
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100471 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200472 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
473 if (delta < 0)
474 delta += runtime->boundary;
475 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
476 runtime->hw_ptr_interrupt += delta;
477 if (runtime->hw_ptr_interrupt >= runtime->boundary)
478 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100479 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100480 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500482 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500483 if (crossed_boundary) {
484 snd_BUG_ON(crossed_boundary != 1);
485 runtime->hw_ptr_wrap += runtime->boundary;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600488 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500489
Jaroslav Kysela12509322010-01-07 15:36:31 +0100490 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100494int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100496 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * snd_pcm_set_ops - set the PCM operators
501 * @pcm: the pcm instance
502 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
503 * @ops: the operator table
504 *
505 * Sets the given PCM operators to the pcm instance.
506 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200507void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
508 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Takashi Iwai877211f2005-11-17 13:59:38 +0100510 struct snd_pcm_str *stream = &pcm->streams[direction];
511 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 for (substream = stream->substream; substream != NULL; substream = substream->next)
514 substream->ops = ops;
515}
516
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200517EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/**
520 * snd_pcm_sync - set the PCM sync id
521 * @substream: the pcm substream
522 *
523 * Sets the PCM sync identifier for the card.
524 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100525void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwai877211f2005-11-17 13:59:38 +0100527 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 runtime->sync.id32[0] = substream->pcm->card->number;
530 runtime->sync.id32[1] = -1;
531 runtime->sync.id32[2] = -1;
532 runtime->sync.id32[3] = -1;
533}
534
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200535EXPORT_SYMBOL(snd_pcm_set_sync);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Standard ioctl routine
539 */
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static inline unsigned int div32(unsigned int a, unsigned int b,
542 unsigned int *r)
543{
544 if (b == 0) {
545 *r = 0;
546 return UINT_MAX;
547 }
548 *r = a % b;
549 return a / b;
550}
551
552static inline unsigned int div_down(unsigned int a, unsigned int b)
553{
554 if (b == 0)
555 return UINT_MAX;
556 return a / b;
557}
558
559static inline unsigned int div_up(unsigned int a, unsigned int b)
560{
561 unsigned int r;
562 unsigned int q;
563 if (b == 0)
564 return UINT_MAX;
565 q = div32(a, b, &r);
566 if (r)
567 ++q;
568 return q;
569}
570
571static inline unsigned int mul(unsigned int a, unsigned int b)
572{
573 if (a == 0)
574 return 0;
575 if (div_down(UINT_MAX, a) < b)
576 return UINT_MAX;
577 return a * b;
578}
579
580static inline unsigned int muldiv32(unsigned int a, unsigned int b,
581 unsigned int c, unsigned int *r)
582{
583 u_int64_t n = (u_int64_t) a * b;
584 if (c == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *r = 0;
586 return UINT_MAX;
587 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200588 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (n >= UINT_MAX) {
590 *r = 0;
591 return UINT_MAX;
592 }
593 return n;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
597 * snd_interval_refine - refine the interval value of configurator
598 * @i: the interval value to refine
599 * @v: the interval value to refer to
600 *
601 * Refines the interval value with the reference value.
602 * The interval is changed to the range satisfying both intervals.
603 * The interval status (min, max, integer, etc.) are evaluated.
604 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100605 * Return: Positive if the value is changed, zero if it's not changed, or a
606 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100608int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200611 if (snd_BUG_ON(snd_interval_empty(i)))
612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (i->min < v->min) {
614 i->min = v->min;
615 i->openmin = v->openmin;
616 changed = 1;
617 } else if (i->min == v->min && !i->openmin && v->openmin) {
618 i->openmin = 1;
619 changed = 1;
620 }
621 if (i->max > v->max) {
622 i->max = v->max;
623 i->openmax = v->openmax;
624 changed = 1;
625 } else if (i->max == v->max && !i->openmax && v->openmax) {
626 i->openmax = 1;
627 changed = 1;
628 }
629 if (!i->integer && v->integer) {
630 i->integer = 1;
631 changed = 1;
632 }
633 if (i->integer) {
634 if (i->openmin) {
635 i->min++;
636 i->openmin = 0;
637 }
638 if (i->openmax) {
639 i->max--;
640 i->openmax = 0;
641 }
642 } else if (!i->openmin && !i->openmax && i->min == i->max)
643 i->integer = 1;
644 if (snd_interval_checkempty(i)) {
645 snd_interval_none(i);
646 return -EINVAL;
647 }
648 return changed;
649}
650
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200651EXPORT_SYMBOL(snd_interval_refine);
652
Takashi Iwai877211f2005-11-17 13:59:38 +0100653static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200655 if (snd_BUG_ON(snd_interval_empty(i)))
656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (snd_interval_single(i))
658 return 0;
659 i->max = i->min;
660 i->openmax = i->openmin;
661 if (i->openmax)
662 i->max++;
663 return 1;
664}
665
Takashi Iwai877211f2005-11-17 13:59:38 +0100666static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200668 if (snd_BUG_ON(snd_interval_empty(i)))
669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (snd_interval_single(i))
671 return 0;
672 i->min = i->max;
673 i->openmin = i->openmax;
674 if (i->openmin)
675 i->min--;
676 return 1;
677}
678
Takashi Iwai877211f2005-11-17 13:59:38 +0100679void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 if (a->empty || b->empty) {
682 snd_interval_none(c);
683 return;
684 }
685 c->empty = 0;
686 c->min = mul(a->min, b->min);
687 c->openmin = (a->openmin || b->openmin);
688 c->max = mul(a->max, b->max);
689 c->openmax = (a->openmax || b->openmax);
690 c->integer = (a->integer && b->integer);
691}
692
693/**
694 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200695 * @a: dividend
696 * @b: divisor
697 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
699 * c = a / b
700 *
701 * Returns non-zero if the value is changed, zero if not changed.
702 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100703void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 unsigned int r;
706 if (a->empty || b->empty) {
707 snd_interval_none(c);
708 return;
709 }
710 c->empty = 0;
711 c->min = div32(a->min, b->max, &r);
712 c->openmin = (r || a->openmin || b->openmax);
713 if (b->min > 0) {
714 c->max = div32(a->max, b->min, &r);
715 if (r) {
716 c->max++;
717 c->openmax = 1;
718 } else
719 c->openmax = (a->openmax || b->openmin);
720 } else {
721 c->max = UINT_MAX;
722 c->openmax = 0;
723 }
724 c->integer = 0;
725}
726
727/**
728 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200729 * @a: dividend 1
730 * @b: dividend 2
731 * @k: divisor (as integer)
732 * @c: result
733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * c = a * b / k
735 *
736 * Returns non-zero if the value is changed, zero if not changed.
737 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100738void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
739 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 unsigned int r;
742 if (a->empty || b->empty) {
743 snd_interval_none(c);
744 return;
745 }
746 c->empty = 0;
747 c->min = muldiv32(a->min, b->min, k, &r);
748 c->openmin = (r || a->openmin || b->openmin);
749 c->max = muldiv32(a->max, b->max, k, &r);
750 if (r) {
751 c->max++;
752 c->openmax = 1;
753 } else
754 c->openmax = (a->openmax || b->openmax);
755 c->integer = 0;
756}
757
758/**
759 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200760 * @a: dividend 1
761 * @k: dividend 2 (as integer)
762 * @b: divisor
763 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
765 * c = a * k / b
766 *
767 * Returns non-zero if the value is changed, zero if not changed.
768 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100769void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
770 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned int r;
773 if (a->empty || b->empty) {
774 snd_interval_none(c);
775 return;
776 }
777 c->empty = 0;
778 c->min = muldiv32(a->min, k, b->max, &r);
779 c->openmin = (r || a->openmin || b->openmax);
780 if (b->min > 0) {
781 c->max = muldiv32(a->max, k, b->min, &r);
782 if (r) {
783 c->max++;
784 c->openmax = 1;
785 } else
786 c->openmax = (a->openmax || b->openmin);
787 } else {
788 c->max = UINT_MAX;
789 c->openmax = 0;
790 }
791 c->integer = 0;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/* ---- */
795
796
797/**
798 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200799 * @i: interval to refine
800 * @rats_count: number of ratnum_t
801 * @rats: ratnum_t array
802 * @nump: pointer to store the resultant numerator
803 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100805 * Return: Positive if the value is changed, zero if it's not changed, or a
806 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100808int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100809 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100810 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100812 unsigned int best_num, best_den;
813 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100815 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100817 unsigned int result_num, result_den;
818 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 best_num = best_den = best_diff = 0;
821 for (k = 0; k < rats_count; ++k) {
822 unsigned int num = rats[k].num;
823 unsigned int den;
824 unsigned int q = i->min;
825 int diff;
826 if (q == 0)
827 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100828 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (den < rats[k].den_min)
830 continue;
831 if (den > rats[k].den_max)
832 den = rats[k].den_max;
833 else {
834 unsigned int r;
835 r = (den - rats[k].den_min) % rats[k].den_step;
836 if (r != 0)
837 den -= r;
838 }
839 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100840 if (diff < 0)
841 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (best_num == 0 ||
843 diff * best_den < best_diff * den) {
844 best_diff = diff;
845 best_den = den;
846 best_num = num;
847 }
848 }
849 if (best_den == 0) {
850 i->empty = 1;
851 return -EINVAL;
852 }
853 t.min = div_down(best_num, best_den);
854 t.openmin = !!(best_num % best_den);
855
Krzysztof Helt8374e242009-12-21 17:07:08 +0100856 result_num = best_num;
857 result_diff = best_diff;
858 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 best_num = best_den = best_diff = 0;
860 for (k = 0; k < rats_count; ++k) {
861 unsigned int num = rats[k].num;
862 unsigned int den;
863 unsigned int q = i->max;
864 int diff;
865 if (q == 0) {
866 i->empty = 1;
867 return -EINVAL;
868 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100869 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (den > rats[k].den_max)
871 continue;
872 if (den < rats[k].den_min)
873 den = rats[k].den_min;
874 else {
875 unsigned int r;
876 r = (den - rats[k].den_min) % rats[k].den_step;
877 if (r != 0)
878 den += rats[k].den_step - r;
879 }
880 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100881 if (diff < 0)
882 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (best_num == 0 ||
884 diff * best_den < best_diff * den) {
885 best_diff = diff;
886 best_den = den;
887 best_num = num;
888 }
889 }
890 if (best_den == 0) {
891 i->empty = 1;
892 return -EINVAL;
893 }
894 t.max = div_up(best_num, best_den);
895 t.openmax = !!(best_num % best_den);
896 t.integer = 0;
897 err = snd_interval_refine(i, &t);
898 if (err < 0)
899 return err;
900
901 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100902 if (best_diff * result_den < result_diff * best_den) {
903 result_num = best_num;
904 result_den = best_den;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100907 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100909 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 return err;
912}
913
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200914EXPORT_SYMBOL(snd_interval_ratnum);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/**
917 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200918 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100919 * @rats_count: number of struct ratden
920 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200921 * @nump: pointer to store the resultant numerator
922 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100924 * Return: Positive if the value is changed, zero if it's not changed, or a
925 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100927static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100928 unsigned int rats_count,
929 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int *nump, unsigned int *denp)
931{
932 unsigned int best_num, best_diff, best_den;
933 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100934 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int err;
936
937 best_num = best_den = best_diff = 0;
938 for (k = 0; k < rats_count; ++k) {
939 unsigned int num;
940 unsigned int den = rats[k].den;
941 unsigned int q = i->min;
942 int diff;
943 num = mul(q, den);
944 if (num > rats[k].num_max)
945 continue;
946 if (num < rats[k].num_min)
947 num = rats[k].num_max;
948 else {
949 unsigned int r;
950 r = (num - rats[k].num_min) % rats[k].num_step;
951 if (r != 0)
952 num += rats[k].num_step - r;
953 }
954 diff = num - q * den;
955 if (best_num == 0 ||
956 diff * best_den < best_diff * den) {
957 best_diff = diff;
958 best_den = den;
959 best_num = num;
960 }
961 }
962 if (best_den == 0) {
963 i->empty = 1;
964 return -EINVAL;
965 }
966 t.min = div_down(best_num, best_den);
967 t.openmin = !!(best_num % best_den);
968
969 best_num = best_den = best_diff = 0;
970 for (k = 0; k < rats_count; ++k) {
971 unsigned int num;
972 unsigned int den = rats[k].den;
973 unsigned int q = i->max;
974 int diff;
975 num = mul(q, den);
976 if (num < rats[k].num_min)
977 continue;
978 if (num > rats[k].num_max)
979 num = rats[k].num_max;
980 else {
981 unsigned int r;
982 r = (num - rats[k].num_min) % rats[k].num_step;
983 if (r != 0)
984 num -= r;
985 }
986 diff = q * den - num;
987 if (best_num == 0 ||
988 diff * best_den < best_diff * den) {
989 best_diff = diff;
990 best_den = den;
991 best_num = num;
992 }
993 }
994 if (best_den == 0) {
995 i->empty = 1;
996 return -EINVAL;
997 }
998 t.max = div_up(best_num, best_den);
999 t.openmax = !!(best_num % best_den);
1000 t.integer = 0;
1001 err = snd_interval_refine(i, &t);
1002 if (err < 0)
1003 return err;
1004
1005 if (snd_interval_single(i)) {
1006 if (nump)
1007 *nump = best_num;
1008 if (denp)
1009 *denp = best_den;
1010 }
1011 return err;
1012}
1013
1014/**
1015 * snd_interval_list - refine the interval value from the list
1016 * @i: the interval value to refine
1017 * @count: the number of elements in the list
1018 * @list: the value list
1019 * @mask: the bit-mask to evaluate
1020 *
1021 * Refines the interval value from the list.
1022 * When mask is non-zero, only the elements corresponding to bit 1 are
1023 * evaluated.
1024 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001025 * Return: Positive if the value is changed, zero if it's not changed, or a
1026 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Mark Brown4af87a92012-03-14 19:48:43 +00001028int snd_interval_list(struct snd_interval *i, unsigned int count,
1029 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001032 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001033
1034 if (!count) {
1035 i->empty = 1;
1036 return -EINVAL;
1037 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001038 snd_interval_any(&list_range);
1039 list_range.min = UINT_MAX;
1040 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 for (k = 0; k < count; k++) {
1042 if (mask && !(mask & (1 << k)))
1043 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001044 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001046 list_range.min = min(list_range.min, list[k]);
1047 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001049 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001052EXPORT_SYMBOL(snd_interval_list);
1053
Peter Rosinf66f8982015-01-28 15:16:06 +01001054/**
1055 * snd_interval_ranges - refine the interval value from the list of ranges
1056 * @i: the interval value to refine
1057 * @count: the number of elements in the list of ranges
1058 * @ranges: the ranges list
1059 * @mask: the bit-mask to evaluate
1060 *
1061 * Refines the interval value from the list of ranges.
1062 * When mask is non-zero, only the elements corresponding to bit 1 are
1063 * evaluated.
1064 *
1065 * Return: Positive if the value is changed, zero if it's not changed, or a
1066 * negative error code.
1067 */
1068int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1069 const struct snd_interval *ranges, unsigned int mask)
1070{
1071 unsigned int k;
1072 struct snd_interval range_union;
1073 struct snd_interval range;
1074
1075 if (!count) {
1076 snd_interval_none(i);
1077 return -EINVAL;
1078 }
1079 snd_interval_any(&range_union);
1080 range_union.min = UINT_MAX;
1081 range_union.max = 0;
1082 for (k = 0; k < count; k++) {
1083 if (mask && !(mask & (1 << k)))
1084 continue;
1085 snd_interval_copy(&range, &ranges[k]);
1086 if (snd_interval_refine(&range, i) < 0)
1087 continue;
1088 if (snd_interval_empty(&range))
1089 continue;
1090
1091 if (range.min < range_union.min) {
1092 range_union.min = range.min;
1093 range_union.openmin = 1;
1094 }
1095 if (range.min == range_union.min && !range.openmin)
1096 range_union.openmin = 0;
1097 if (range.max > range_union.max) {
1098 range_union.max = range.max;
1099 range_union.openmax = 1;
1100 }
1101 if (range.max == range_union.max && !range.openmax)
1102 range_union.openmax = 0;
1103 }
1104 return snd_interval_refine(i, &range_union);
1105}
1106EXPORT_SYMBOL(snd_interval_ranges);
1107
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001108static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 unsigned int n;
1111 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001112 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (n != 0 || i->openmin) {
1114 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001115 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 changed = 1;
1117 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001118 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (n != 0 || i->openmax) {
1120 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001121 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 changed = 1;
1123 }
1124 if (snd_interval_checkempty(i)) {
1125 i->empty = 1;
1126 return -EINVAL;
1127 }
1128 return changed;
1129}
1130
1131/* Info constraints helpers */
1132
1133/**
1134 * snd_pcm_hw_rule_add - add the hw-constraint rule
1135 * @runtime: the pcm runtime instance
1136 * @cond: condition bits
1137 * @var: the variable to evaluate
1138 * @func: the evaluation function
1139 * @private: the private data pointer passed to function
1140 * @dep: the dependent variables
1141 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001142 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001144int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int var,
1146 snd_pcm_hw_rule_func_t func, void *private,
1147 int dep, ...)
1148{
Takashi Iwai877211f2005-11-17 13:59:38 +01001149 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1150 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned int k;
1152 va_list args;
1153 va_start(args, dep);
1154 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001155 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 unsigned int new_rules = constrs->rules_all + 16;
1157 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001158 if (!new) {
1159 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (constrs->rules) {
1163 memcpy(new, constrs->rules,
1164 constrs->rules_num * sizeof(*c));
1165 kfree(constrs->rules);
1166 }
1167 constrs->rules = new;
1168 constrs->rules_all = new_rules;
1169 }
1170 c = &constrs->rules[constrs->rules_num];
1171 c->cond = cond;
1172 c->func = func;
1173 c->var = var;
1174 c->private = private;
1175 k = 0;
1176 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001177 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1178 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001179 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 c->deps[k++] = dep;
1182 if (dep < 0)
1183 break;
1184 dep = va_arg(args, int);
1185 }
1186 constrs->rules_num++;
1187 va_end(args);
1188 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001191EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001194 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001195 * @runtime: PCM runtime instance
1196 * @var: hw_params variable to apply the mask
1197 * @mask: the bitmap mask
1198 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001199 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001200 *
1201 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001203int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 u_int32_t mask)
1205{
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1207 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *maskp->bits &= mask;
1209 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1210 if (*maskp->bits == 0)
1211 return -EINVAL;
1212 return 0;
1213}
1214
1215/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001216 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001217 * @runtime: PCM runtime instance
1218 * @var: hw_params variable to apply the mask
1219 * @mask: the 64bit bitmap mask
1220 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001221 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001222 *
1223 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001225int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 u_int64_t mask)
1227{
Takashi Iwai877211f2005-11-17 13:59:38 +01001228 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1229 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 maskp->bits[0] &= (u_int32_t)mask;
1231 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1232 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1233 if (! maskp->bits[0] && ! maskp->bits[1])
1234 return -EINVAL;
1235 return 0;
1236}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001237EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001240 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001241 * @runtime: PCM runtime instance
1242 * @var: hw_params variable to apply the integer constraint
1243 *
1244 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001245 *
1246 * Return: Positive if the value is changed, zero if it's not changed, or a
1247 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001249int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Takashi Iwai877211f2005-11-17 13:59:38 +01001251 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return snd_interval_setinteger(constrs_interval(constrs, var));
1253}
1254
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001255EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001258 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001259 * @runtime: PCM runtime instance
1260 * @var: hw_params variable to apply the range
1261 * @min: the minimal value
1262 * @max: the maximal value
1263 *
1264 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001265 *
1266 * Return: Positive if the value is changed, zero if it's not changed, or a
1267 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001269int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned int min, unsigned int max)
1271{
Takashi Iwai877211f2005-11-17 13:59:38 +01001272 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1273 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 t.min = min;
1275 t.max = max;
1276 t.openmin = t.openmax = 0;
1277 t.integer = 0;
1278 return snd_interval_refine(constrs_interval(constrs, var), &t);
1279}
1280
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001281EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1282
Takashi Iwai877211f2005-11-17 13:59:38 +01001283static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1284 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Takashi Iwai877211f2005-11-17 13:59:38 +01001286 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1288}
1289
1290
1291/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001292 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001293 * @runtime: PCM runtime instance
1294 * @cond: condition bits
1295 * @var: hw_params variable to apply the list constraint
1296 * @l: list
1297 *
1298 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001299 *
1300 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001302int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 unsigned int cond,
1304 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001305 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001308 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 var, -1);
1310}
1311
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001312EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1313
Peter Rosinf66f8982015-01-28 15:16:06 +01001314static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1315 struct snd_pcm_hw_rule *rule)
1316{
1317 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1318 return snd_interval_ranges(hw_param_interval(params, rule->var),
1319 r->count, r->ranges, r->mask);
1320}
1321
1322
1323/**
1324 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1325 * @runtime: PCM runtime instance
1326 * @cond: condition bits
1327 * @var: hw_params variable to apply the list of range constraints
1328 * @r: ranges
1329 *
1330 * Apply the list of range constraints to an interval parameter.
1331 *
1332 * Return: Zero if successful, or a negative error code on failure.
1333 */
1334int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1335 unsigned int cond,
1336 snd_pcm_hw_param_t var,
1337 const struct snd_pcm_hw_constraint_ranges *r)
1338{
1339 return snd_pcm_hw_rule_add(runtime, cond, var,
1340 snd_pcm_hw_rule_ranges, (void *)r,
1341 var, -1);
1342}
1343EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1344
Takashi Iwai877211f2005-11-17 13:59:38 +01001345static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1346 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001348 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 unsigned int num = 0, den = 0;
1350 int err;
1351 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1352 r->nrats, r->rats, &num, &den);
1353 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1354 params->rate_num = num;
1355 params->rate_den = den;
1356 }
1357 return err;
1358}
1359
1360/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001361 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001362 * @runtime: PCM runtime instance
1363 * @cond: condition bits
1364 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001365 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001366 *
1367 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001369int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unsigned int cond,
1371 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001372 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001375 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 var, -1);
1377}
1378
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001379EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1380
Takashi Iwai877211f2005-11-17 13:59:38 +01001381static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1382 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001384 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 unsigned int num = 0, den = 0;
1386 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1387 r->nrats, r->rats, &num, &den);
1388 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1389 params->rate_num = num;
1390 params->rate_den = den;
1391 }
1392 return err;
1393}
1394
1395/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001396 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001397 * @runtime: PCM runtime instance
1398 * @cond: condition bits
1399 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001400 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001401 *
1402 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001404int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 unsigned int cond,
1406 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001407 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001410 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 var, -1);
1412}
1413
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001414EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1415
Takashi Iwai877211f2005-11-17 13:59:38 +01001416static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1417 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 unsigned int l = (unsigned long) rule->private;
1420 int width = l & 0xffff;
1421 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001422 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001423
1424 if (!snd_interval_single(i))
1425 return 0;
1426
1427 if ((snd_interval_value(i) == width) ||
1428 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001429 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
1432}
1433
1434/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001435 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001436 * @runtime: PCM runtime instance
1437 * @cond: condition bits
1438 * @width: sample bits width
1439 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001440 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001441 * This constraint will set the number of most significant bits (msbits) if a
1442 * sample format with the specified width has been select. If width is set to 0
1443 * the msbits will be set for any sample format with a width larger than the
1444 * specified msbits.
1445 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001446 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001448int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 unsigned int cond,
1450 unsigned int width,
1451 unsigned int msbits)
1452{
1453 unsigned long l = (msbits << 16) | width;
1454 return snd_pcm_hw_rule_add(runtime, cond, -1,
1455 snd_pcm_hw_rule_msbits,
1456 (void*) l,
1457 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1458}
1459
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001460EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1461
Takashi Iwai877211f2005-11-17 13:59:38 +01001462static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1463 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001466 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001470 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001471 * @runtime: PCM runtime instance
1472 * @cond: condition bits
1473 * @var: hw_params variable to apply the step constraint
1474 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001475 *
1476 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001478int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 unsigned int cond,
1480 snd_pcm_hw_param_t var,
1481 unsigned long step)
1482{
1483 return snd_pcm_hw_rule_add(runtime, cond, var,
1484 snd_pcm_hw_rule_step, (void *) step,
1485 var, -1);
1486}
1487
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001488EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1489
Takashi Iwai877211f2005-11-17 13:59:38 +01001490static 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 -07001491{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001492 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1494 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1495 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1496 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1497 };
1498 return snd_interval_list(hw_param_interval(params, rule->var),
1499 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1500}
1501
1502/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001503 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001504 * @runtime: PCM runtime instance
1505 * @cond: condition bits
1506 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001507 *
1508 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001510int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned int cond,
1512 snd_pcm_hw_param_t var)
1513{
1514 return snd_pcm_hw_rule_add(runtime, cond, var,
1515 snd_pcm_hw_rule_pow2, NULL,
1516 var, -1);
1517}
1518
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001519EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1520
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001521static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1522 struct snd_pcm_hw_rule *rule)
1523{
1524 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1525 struct snd_interval *rate;
1526
1527 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1528 return snd_interval_list(rate, 1, &base_rate, 0);
1529}
1530
1531/**
1532 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1533 * @runtime: PCM runtime instance
1534 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001535 *
1536 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001537 */
1538int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1539 unsigned int base_rate)
1540{
1541 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1542 SNDRV_PCM_HW_PARAM_RATE,
1543 snd_pcm_hw_rule_noresample_func,
1544 (void *)(uintptr_t)base_rate,
1545 SNDRV_PCM_HW_PARAM_RATE, -1);
1546}
1547EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1548
Takashi Iwai877211f2005-11-17 13:59:38 +01001549static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001550 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 if (hw_is_mask(var)) {
1553 snd_mask_any(hw_param_mask(params, var));
1554 params->cmask |= 1 << var;
1555 params->rmask |= 1 << var;
1556 return;
1557 }
1558 if (hw_is_interval(var)) {
1559 snd_interval_any(hw_param_interval(params, var));
1560 params->cmask |= 1 << var;
1561 params->rmask |= 1 << var;
1562 return;
1563 }
1564 snd_BUG();
1565}
1566
Takashi Iwai877211f2005-11-17 13:59:38 +01001567void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 unsigned int k;
1570 memset(params, 0, sizeof(*params));
1571 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1572 _snd_pcm_hw_param_any(params, k);
1573 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1574 _snd_pcm_hw_param_any(params, k);
1575 params->info = ~0U;
1576}
1577
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001578EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001581 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001582 * @params: the hw_params instance
1583 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001584 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001586 * Return: The value for field @var if it's fixed in configuration space
1587 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001589int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1590 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001593 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!snd_mask_single(mask))
1595 return -EINVAL;
1596 if (dir)
1597 *dir = 0;
1598 return snd_mask_value(mask);
1599 }
1600 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001601 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!snd_interval_single(i))
1603 return -EINVAL;
1604 if (dir)
1605 *dir = i->openmin;
1606 return snd_interval_value(i);
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EINVAL;
1609}
1610
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001611EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Takashi Iwai877211f2005-11-17 13:59:38 +01001613void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 snd_pcm_hw_param_t var)
1615{
1616 if (hw_is_mask(var)) {
1617 snd_mask_none(hw_param_mask(params, var));
1618 params->cmask |= 1 << var;
1619 params->rmask |= 1 << var;
1620 } else if (hw_is_interval(var)) {
1621 snd_interval_none(hw_param_interval(params, var));
1622 params->cmask |= 1 << var;
1623 params->rmask |= 1 << var;
1624 } else {
1625 snd_BUG();
1626 }
1627}
1628
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001629EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001632 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 int changed;
1635 if (hw_is_mask(var))
1636 changed = snd_mask_refine_first(hw_param_mask(params, var));
1637 else if (hw_is_interval(var))
1638 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001639 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (changed) {
1642 params->cmask |= 1 << var;
1643 params->rmask |= 1 << var;
1644 }
1645 return changed;
1646}
1647
1648
1649/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001650 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001651 * @pcm: PCM instance
1652 * @params: the hw_params instance
1653 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001654 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001656 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001658 *
1659 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001661int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1662 struct snd_pcm_hw_params *params,
1663 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 int changed = _snd_pcm_hw_param_first(params, var);
1666 if (changed < 0)
1667 return changed;
1668 if (params->rmask) {
1669 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001670 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 return snd_pcm_hw_param_value(params, var, dir);
1674}
1675
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001676EXPORT_SYMBOL(snd_pcm_hw_param_first);
1677
Takashi Iwai877211f2005-11-17 13:59:38 +01001678static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001679 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 int changed;
1682 if (hw_is_mask(var))
1683 changed = snd_mask_refine_last(hw_param_mask(params, var));
1684 else if (hw_is_interval(var))
1685 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001686 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (changed) {
1689 params->cmask |= 1 << var;
1690 params->rmask |= 1 << var;
1691 }
1692 return changed;
1693}
1694
1695
1696/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001697 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001698 * @pcm: PCM instance
1699 * @params: the hw_params instance
1700 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001701 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001703 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001705 *
1706 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001708int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1709 struct snd_pcm_hw_params *params,
1710 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int changed = _snd_pcm_hw_param_last(params, var);
1713 if (changed < 0)
1714 return changed;
1715 if (params->rmask) {
1716 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001717 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 return snd_pcm_hw_param_value(params, var, dir);
1721}
1722
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001723EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001726 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001727 * @pcm: PCM instance
1728 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001730 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * The configuration chosen is that obtained fixing in this order:
1732 * first access, first format, first subformat, min channels,
1733 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001734 *
1735 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001737int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1738 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001740 static int vars[] = {
1741 SNDRV_PCM_HW_PARAM_ACCESS,
1742 SNDRV_PCM_HW_PARAM_FORMAT,
1743 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1744 SNDRV_PCM_HW_PARAM_CHANNELS,
1745 SNDRV_PCM_HW_PARAM_RATE,
1746 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1747 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1748 SNDRV_PCM_HW_PARAM_TICK_TIME,
1749 -1
1750 };
1751 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001753 for (v = vars; *v != -1; v++) {
1754 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1755 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1756 else
1757 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001758 if (snd_BUG_ON(err < 0))
1759 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
Takashi Iwai877211f2005-11-17 13:59:38 +01001764static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 void *arg)
1766{
Takashi Iwai877211f2005-11-17 13:59:38 +01001767 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned long flags;
1769 snd_pcm_stream_lock_irqsave(substream, flags);
1770 if (snd_pcm_running(substream) &&
1771 snd_pcm_update_hw_ptr(substream) >= 0)
1772 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001773 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001775 runtime->hw_ptr_wrap = 0;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 snd_pcm_stream_unlock_irqrestore(substream, flags);
1778 return 0;
1779}
1780
Takashi Iwai877211f2005-11-17 13:59:38 +01001781static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 void *arg)
1783{
Takashi Iwai877211f2005-11-17 13:59:38 +01001784 struct snd_pcm_channel_info *info = arg;
1785 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int width;
1787 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1788 info->offset = -1;
1789 return 0;
1790 }
1791 width = snd_pcm_format_physical_width(runtime->format);
1792 if (width < 0)
1793 return width;
1794 info->offset = 0;
1795 switch (runtime->access) {
1796 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1797 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001798 if ((UINT_MAX/width) < info->channel) {
1799 snd_printd("%s: integer overflow while multiply\n",
1800 __func__);
1801 return -EINVAL;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 info->first = info->channel * width;
1804 info->step = runtime->channels * width;
1805 break;
1806 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1807 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1808 {
1809 size_t size = runtime->dma_bytes / runtime->channels;
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001810
1811 if ((size > 0) && ((UINT_MAX/(size * 8)) < info->channel)) {
1812 snd_printd("%s: integer overflow while multiply\n",
1813 __func__);
1814 return -EINVAL;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 info->first = info->channel * size * 8;
1817 info->step = width;
1818 break;
1819 }
1820 default:
1821 snd_BUG();
1822 break;
1823 }
1824 return 0;
1825}
1826
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001827static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1828 void *arg)
1829{
1830 struct snd_pcm_hw_params *params = arg;
1831 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001832 int channels;
1833 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001834
1835 params->fifo_size = substream->runtime->hw.fifo_size;
1836 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1837 format = params_format(params);
1838 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001839 frame_size = snd_pcm_format_size(format, channels);
1840 if (frame_size > 0)
1841 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001842 }
1843 return 0;
1844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846/**
1847 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1848 * @substream: the pcm substream instance
1849 * @cmd: ioctl command
1850 * @arg: ioctl argument
1851 *
1852 * Processes the generic ioctl commands for PCM.
1853 * Can be passed as the ioctl callback for PCM ops.
1854 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001855 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001857int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned int cmd, void *arg)
1859{
1860 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 case SNDRV_PCM_IOCTL1_RESET:
1862 return snd_pcm_lib_ioctl_reset(substream, arg);
1863 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1864 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001865 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1866 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 return -ENXIO;
1869}
1870
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001871EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/**
1874 * snd_pcm_period_elapsed - update the pcm status for the next period
1875 * @substream: the pcm substream instance
1876 *
1877 * This function is called from the interrupt handler when the
1878 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001879 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 *
1881 * Even if more than one periods have elapsed since the last call, you
1882 * have to call this only once.
1883 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001884void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Takashi Iwai877211f2005-11-17 13:59:38 +01001886 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 unsigned long flags;
1888
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001889 if (PCM_RUNTIME_CHECK(substream))
1890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 snd_pcm_stream_lock_irqsave(substream, flags);
1894 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001895 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto _end;
1897
Jie Yang90bbaf62015-10-16 17:57:46 +08001898#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (substream->timer_running)
1900 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001904 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001907EXPORT_SYMBOL(snd_pcm_period_elapsed);
1908
Takashi Iwai13075512008-01-08 18:08:14 +01001909/*
1910 * Wait until avail_min data becomes available
1911 * Returns a negative error code if any error occurs during operation.
1912 * The available space is stored on availp. When err = 0 and avail = 0
1913 * on the capture stream, it indicates the stream is in DRAINING state.
1914 */
David Dillow5daeba32010-06-27 00:13:20 +02001915static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001916 snd_pcm_uframes_t *availp)
1917{
1918 struct snd_pcm_runtime *runtime = substream->runtime;
1919 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1920 wait_queue_t wait;
1921 int err = 0;
1922 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001923 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001924
Arjan van de Ven763437a2011-09-15 08:49:25 +02001925 init_waitqueue_entry(&wait, current);
1926 set_current_state(TASK_INTERRUPTIBLE);
1927 add_wait_queue(&runtime->tsleep, &wait);
1928
Takashi Iwaif2b36142011-05-26 08:09:38 +02001929 if (runtime->no_period_wakeup)
1930 wait_time = MAX_SCHEDULE_TIMEOUT;
1931 else {
1932 wait_time = 10;
1933 if (runtime->rate) {
1934 long t = runtime->period_size * 2 / runtime->rate;
1935 wait_time = max(t, wait_time);
1936 }
1937 wait_time = msecs_to_jiffies(wait_time * 1000);
1938 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001939
Takashi Iwai13075512008-01-08 18:08:14 +01001940 for (;;) {
1941 if (signal_pending(current)) {
1942 err = -ERESTARTSYS;
1943 break;
1944 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001945
1946 /*
1947 * We need to check if space became available already
1948 * (and thus the wakeup happened already) first to close
1949 * the race of space already having become available.
1950 * This check must happen after been added to the waitqueue
1951 * and having current state be INTERRUPTIBLE.
1952 */
1953 if (is_playback)
1954 avail = snd_pcm_playback_avail(runtime);
1955 else
1956 avail = snd_pcm_capture_avail(runtime);
1957 if (avail >= runtime->twake)
1958 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001959 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001960
1961 tout = schedule_timeout(wait_time);
1962
Takashi Iwai13075512008-01-08 18:08:14 +01001963 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001964 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001965 switch (runtime->status->state) {
1966 case SNDRV_PCM_STATE_SUSPENDED:
1967 err = -ESTRPIPE;
1968 goto _endloop;
1969 case SNDRV_PCM_STATE_XRUN:
1970 err = -EPIPE;
1971 goto _endloop;
1972 case SNDRV_PCM_STATE_DRAINING:
1973 if (is_playback)
1974 err = -EPIPE;
1975 else
1976 avail = 0; /* indicate draining */
1977 goto _endloop;
1978 case SNDRV_PCM_STATE_OPEN:
1979 case SNDRV_PCM_STATE_SETUP:
1980 case SNDRV_PCM_STATE_DISCONNECTED:
1981 err = -EBADFD;
1982 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001983 case SNDRV_PCM_STATE_PAUSED:
1984 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001985 }
1986 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001987 pcm_dbg(substream->pcm,
1988 "%s write error (DMA or IRQ trouble?)\n",
1989 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001990 err = -EIO;
1991 break;
1992 }
Takashi Iwai13075512008-01-08 18:08:14 +01001993 }
1994 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001995 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001996 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001997 *availp = avail;
1998 return err;
1999}
2000
Takashi Iwai877211f2005-11-17 13:59:38 +01002001static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unsigned int hwoff,
2003 unsigned long data, unsigned int off,
2004 snd_pcm_uframes_t frames)
2005{
Takashi Iwai877211f2005-11-17 13:59:38 +01002006 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int err;
2008 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2009 if (substream->ops->copy) {
2010 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2011 return err;
2012 } else {
2013 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2015 return -EFAULT;
2016 }
2017 return 0;
2018}
2019
Takashi Iwai877211f2005-11-17 13:59:38 +01002020typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 unsigned long data, unsigned int off,
2022 snd_pcm_uframes_t size);
2023
Takashi Iwai877211f2005-11-17 13:59:38 +01002024static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 unsigned long data,
2026 snd_pcm_uframes_t size,
2027 int nonblock,
2028 transfer_f transfer)
2029{
Takashi Iwai877211f2005-11-17 13:59:38 +01002030 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 snd_pcm_uframes_t xfer = 0;
2032 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002033 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int err = 0;
2035
2036 if (size == 0)
2037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 snd_pcm_stream_lock_irq(substream);
2040 switch (runtime->status->state) {
2041 case SNDRV_PCM_STATE_PREPARED:
2042 case SNDRV_PCM_STATE_RUNNING:
2043 case SNDRV_PCM_STATE_PAUSED:
2044 break;
2045 case SNDRV_PCM_STATE_XRUN:
2046 err = -EPIPE;
2047 goto _end_unlock;
2048 case SNDRV_PCM_STATE_SUSPENDED:
2049 err = -ESTRPIPE;
2050 goto _end_unlock;
2051 default:
2052 err = -EBADFD;
2053 goto _end_unlock;
2054 }
2055
David Dillow5daeba32010-06-27 00:13:20 +02002056 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002057 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2058 snd_pcm_update_hw_ptr(substream);
2059 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 while (size > 0) {
2061 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002063 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (nonblock) {
2065 err = -EAGAIN;
2066 goto _end_unlock;
2067 }
David Dillow5daeba32010-06-27 00:13:20 +02002068 runtime->twake = min_t(snd_pcm_uframes_t, size,
2069 runtime->control->avail_min ? : 1);
2070 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002071 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 frames = size > avail ? avail : size;
2075 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2076 if (frames > cont)
2077 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002078 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002079 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002080 snd_pcm_stream_unlock_irq(substream);
2081 return -EINVAL;
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 appl_ptr = runtime->control->appl_ptr;
2084 appl_ofs = appl_ptr % runtime->buffer_size;
2085 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002086 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002088 if (err < 0)
2089 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 switch (runtime->status->state) {
2091 case SNDRV_PCM_STATE_XRUN:
2092 err = -EPIPE;
2093 goto _end_unlock;
2094 case SNDRV_PCM_STATE_SUSPENDED:
2095 err = -ESTRPIPE;
2096 goto _end_unlock;
2097 default:
2098 break;
2099 }
2100 appl_ptr += frames;
2101 if (appl_ptr >= runtime->boundary)
2102 appl_ptr -= runtime->boundary;
2103 runtime->control->appl_ptr = appl_ptr;
2104 if (substream->ops->ack)
2105 substream->ops->ack(substream);
2106
2107 offset += frames;
2108 size -= frames;
2109 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002110 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2112 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2113 err = snd_pcm_start(substream);
2114 if (err < 0)
2115 goto _end_unlock;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002119 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002120 if (xfer > 0 && err >= 0)
2121 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2124}
2125
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002126/* sanity-check for read/write methods */
2127static int pcm_sanity_check(struct snd_pcm_substream *substream)
2128{
2129 struct snd_pcm_runtime *runtime;
2130 if (PCM_RUNTIME_CHECK(substream))
2131 return -ENXIO;
Liam Girdwoodf2727332016-01-29 02:27:17 +05302132 /* TODO: consider and -EINVAL here */
2133 if (substream->hw_no_buffer)
2134 snd_printd("%s: warning this PCM is host less\n", __func__);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002135 runtime = substream->runtime;
2136 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2137 return -EINVAL;
2138 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2139 return -EBADFD;
2140 return 0;
2141}
2142
Takashi Iwai877211f2005-11-17 13:59:38 +01002143snd_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 -07002144{
Takashi Iwai877211f2005-11-17 13:59:38 +01002145 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002147 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002149 err = pcm_sanity_check(substream);
2150 if (err < 0)
2151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002153 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2156 runtime->channels > 1)
2157 return -EINVAL;
2158 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2159 snd_pcm_lib_write_transfer);
2160}
2161
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002162EXPORT_SYMBOL(snd_pcm_lib_write);
2163
Takashi Iwai877211f2005-11-17 13:59:38 +01002164static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 unsigned int hwoff,
2166 unsigned long data, unsigned int off,
2167 snd_pcm_uframes_t frames)
2168{
Takashi Iwai877211f2005-11-17 13:59:38 +01002169 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 int err;
2171 void __user **bufs = (void __user **)data;
2172 int channels = runtime->channels;
2173 int c;
2174 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002175 if (snd_BUG_ON(!substream->ops->silence))
2176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 for (c = 0; c < channels; ++c, ++bufs) {
2178 if (*bufs == NULL) {
2179 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2180 return err;
2181 } else {
2182 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2183 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2184 return err;
2185 }
2186 }
2187 } else {
2188 /* default transfer behaviour */
2189 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 for (c = 0; c < channels; ++c, ++bufs) {
2191 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2192 if (*bufs == NULL) {
2193 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2194 } else {
2195 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2196 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2197 return -EFAULT;
2198 }
2199 }
2200 }
2201 return 0;
2202}
2203
Takashi Iwai877211f2005-11-17 13:59:38 +01002204snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 void __user **bufs,
2206 snd_pcm_uframes_t frames)
2207{
Takashi Iwai877211f2005-11-17 13:59:38 +01002208 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002210 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002212 err = pcm_sanity_check(substream);
2213 if (err < 0)
2214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002216 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2219 return -EINVAL;
2220 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2221 nonblock, snd_pcm_lib_writev_transfer);
2222}
2223
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002224EXPORT_SYMBOL(snd_pcm_lib_writev);
2225
Takashi Iwai877211f2005-11-17 13:59:38 +01002226static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 unsigned int hwoff,
2228 unsigned long data, unsigned int off,
2229 snd_pcm_uframes_t frames)
2230{
Takashi Iwai877211f2005-11-17 13:59:38 +01002231 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int err;
2233 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2234 if (substream->ops->copy) {
2235 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2236 return err;
2237 } else {
2238 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2240 return -EFAULT;
2241 }
2242 return 0;
2243}
2244
Takashi Iwai877211f2005-11-17 13:59:38 +01002245static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 unsigned long data,
2247 snd_pcm_uframes_t size,
2248 int nonblock,
2249 transfer_f transfer)
2250{
Takashi Iwai877211f2005-11-17 13:59:38 +01002251 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 snd_pcm_uframes_t xfer = 0;
2253 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002254 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int err = 0;
2256
2257 if (size == 0)
2258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 snd_pcm_stream_lock_irq(substream);
2261 switch (runtime->status->state) {
2262 case SNDRV_PCM_STATE_PREPARED:
2263 if (size >= runtime->start_threshold) {
2264 err = snd_pcm_start(substream);
2265 if (err < 0)
2266 goto _end_unlock;
2267 }
2268 break;
2269 case SNDRV_PCM_STATE_DRAINING:
2270 case SNDRV_PCM_STATE_RUNNING:
2271 case SNDRV_PCM_STATE_PAUSED:
2272 break;
2273 case SNDRV_PCM_STATE_XRUN:
2274 err = -EPIPE;
2275 goto _end_unlock;
2276 case SNDRV_PCM_STATE_SUSPENDED:
2277 err = -ESTRPIPE;
2278 goto _end_unlock;
2279 default:
2280 err = -EBADFD;
2281 goto _end_unlock;
2282 }
2283
David Dillow5daeba32010-06-27 00:13:20 +02002284 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002285 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2286 snd_pcm_update_hw_ptr(substream);
2287 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 while (size > 0) {
2289 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002291 if (!avail) {
2292 if (runtime->status->state ==
2293 SNDRV_PCM_STATE_DRAINING) {
2294 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto _end_unlock;
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (nonblock) {
2298 err = -EAGAIN;
2299 goto _end_unlock;
2300 }
David Dillow5daeba32010-06-27 00:13:20 +02002301 runtime->twake = min_t(snd_pcm_uframes_t, size,
2302 runtime->control->avail_min ? : 1);
2303 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002304 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002306 if (!avail)
2307 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 frames = size > avail ? avail : size;
2310 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2311 if (frames > cont)
2312 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002313 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002314 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002315 snd_pcm_stream_unlock_irq(substream);
2316 return -EINVAL;
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 appl_ptr = runtime->control->appl_ptr;
2319 appl_ofs = appl_ptr % runtime->buffer_size;
2320 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002321 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002323 if (err < 0)
2324 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 switch (runtime->status->state) {
2326 case SNDRV_PCM_STATE_XRUN:
2327 err = -EPIPE;
2328 goto _end_unlock;
2329 case SNDRV_PCM_STATE_SUSPENDED:
2330 err = -ESTRPIPE;
2331 goto _end_unlock;
2332 default:
2333 break;
2334 }
2335 appl_ptr += frames;
2336 if (appl_ptr >= runtime->boundary)
2337 appl_ptr -= runtime->boundary;
2338 runtime->control->appl_ptr = appl_ptr;
2339 if (substream->ops->ack)
2340 substream->ops->ack(substream);
2341
2342 offset += frames;
2343 size -= frames;
2344 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002345 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002348 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002349 if (xfer > 0 && err >= 0)
2350 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2353}
2354
Takashi Iwai877211f2005-11-17 13:59:38 +01002355snd_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 -07002356{
Takashi Iwai877211f2005-11-17 13:59:38 +01002357 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002359 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002361 err = pcm_sanity_check(substream);
2362 if (err < 0)
2363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002365 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2367 return -EINVAL;
2368 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2369}
2370
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002371EXPORT_SYMBOL(snd_pcm_lib_read);
2372
Takashi Iwai877211f2005-11-17 13:59:38 +01002373static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 unsigned int hwoff,
2375 unsigned long data, unsigned int off,
2376 snd_pcm_uframes_t frames)
2377{
Takashi Iwai877211f2005-11-17 13:59:38 +01002378 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 int err;
2380 void __user **bufs = (void __user **)data;
2381 int channels = runtime->channels;
2382 int c;
2383 if (substream->ops->copy) {
2384 for (c = 0; c < channels; ++c, ++bufs) {
2385 char __user *buf;
2386 if (*bufs == NULL)
2387 continue;
2388 buf = *bufs + samples_to_bytes(runtime, off);
2389 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2390 return err;
2391 }
2392 } else {
2393 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 for (c = 0; c < channels; ++c, ++bufs) {
2395 char *hwbuf;
2396 char __user *buf;
2397 if (*bufs == NULL)
2398 continue;
2399
2400 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2401 buf = *bufs + samples_to_bytes(runtime, off);
2402 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2403 return -EFAULT;
2404 }
2405 }
2406 return 0;
2407}
2408
Takashi Iwai877211f2005-11-17 13:59:38 +01002409snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 void __user **bufs,
2411 snd_pcm_uframes_t frames)
2412{
Takashi Iwai877211f2005-11-17 13:59:38 +01002413 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002415 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002417 err = pcm_sanity_check(substream);
2418 if (err < 0)
2419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2422 return -EBADFD;
2423
Takashi Iwai0df63e42006-04-28 15:13:41 +02002424 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2426 return -EINVAL;
2427 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2428}
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002431
2432/*
2433 * standard channel mapping helpers
2434 */
2435
2436/* default channel maps for multi-channel playbacks, up to 8 channels */
2437const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2438 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002439 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002440 { .channels = 2,
2441 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2442 { .channels = 4,
2443 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2444 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2445 { .channels = 6,
2446 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2447 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2448 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2449 { .channels = 8,
2450 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2451 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2452 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2453 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2454 { }
2455};
2456EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2457
2458/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2459const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2460 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002461 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002462 { .channels = 2,
2463 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2464 { .channels = 4,
2465 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2466 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2467 { .channels = 6,
2468 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2469 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2470 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2471 { .channels = 8,
2472 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2473 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2474 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2475 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2476 { }
2477};
2478EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2479
2480static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2481{
2482 if (ch > info->max_channels)
2483 return false;
2484 return !info->channel_mask || (info->channel_mask & (1U << ch));
2485}
2486
2487static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_info *uinfo)
2489{
2490 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2491
2492 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2493 uinfo->count = 0;
2494 uinfo->count = info->max_channels;
2495 uinfo->value.integer.min = 0;
2496 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2497 return 0;
2498}
2499
2500/* get callback for channel map ctl element
2501 * stores the channel position firstly matching with the current channels
2502 */
2503static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2504 struct snd_ctl_elem_value *ucontrol)
2505{
2506 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2507 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2508 struct snd_pcm_substream *substream;
2509 const struct snd_pcm_chmap_elem *map;
2510
Takashi Iwai552a14a2017-06-14 16:20:32 +02002511 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002512 return -EINVAL;
2513 substream = snd_pcm_chmap_substream(info, idx);
2514 if (!substream)
2515 return -ENODEV;
2516 memset(ucontrol->value.integer.value, 0,
2517 sizeof(ucontrol->value.integer.value));
2518 if (!substream->runtime)
2519 return 0; /* no channels set */
2520 for (map = info->chmap; map->channels; map++) {
2521 int i;
2522 if (map->channels == substream->runtime->channels &&
2523 valid_chmap_channels(info, map->channels)) {
2524 for (i = 0; i < map->channels; i++)
2525 ucontrol->value.integer.value[i] = map->map[i];
2526 return 0;
2527 }
2528 }
2529 return -EINVAL;
2530}
2531
2532/* tlv callback for channel map ctl element
2533 * expands the pre-defined channel maps in a form of TLV
2534 */
2535static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2536 unsigned int size, unsigned int __user *tlv)
2537{
2538 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2539 const struct snd_pcm_chmap_elem *map;
2540 unsigned int __user *dst;
2541 int c, count = 0;
2542
Takashi Iwai552a14a2017-06-14 16:20:32 +02002543 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002544 return -EINVAL;
2545 if (size < 8)
2546 return -ENOMEM;
2547 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2548 return -EFAULT;
2549 size -= 8;
2550 dst = tlv + 2;
2551 for (map = info->chmap; map->channels; map++) {
2552 int chs_bytes = map->channels * 4;
2553 if (!valid_chmap_channels(info, map->channels))
2554 continue;
2555 if (size < 8)
2556 return -ENOMEM;
2557 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2558 put_user(chs_bytes, dst + 1))
2559 return -EFAULT;
2560 dst += 2;
2561 size -= 8;
2562 count += 8;
2563 if (size < chs_bytes)
2564 return -ENOMEM;
2565 size -= chs_bytes;
2566 count += chs_bytes;
2567 for (c = 0; c < map->channels; c++) {
2568 if (put_user(map->map[c], dst))
2569 return -EFAULT;
2570 dst++;
2571 }
2572 }
2573 if (put_user(count, tlv + 1))
2574 return -EFAULT;
2575 return 0;
2576}
2577
2578static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2579{
2580 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2581 info->pcm->streams[info->stream].chmap_kctl = NULL;
2582 kfree(info);
2583}
2584
Banajit Goswami55b24c52016-11-21 19:08:27 -08002585static int pcm_volume_ctl_info(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_info *uinfo)
2587{
2588 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2589 uinfo->count = 1;
2590 uinfo->value.integer.min = 0;
2591 uinfo->value.integer.max = 0x2000;
2592 return 0;
2593}
2594
2595static void pcm_volume_ctl_private_free(struct snd_kcontrol *kcontrol)
2596{
2597 struct snd_pcm_volume *info = snd_kcontrol_chip(kcontrol);
2598
2599 info->pcm->streams[info->stream].vol_kctl = NULL;
2600 kfree(info);
2601}
2602
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002603/**
2604 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2605 * @pcm: the assigned PCM instance
2606 * @stream: stream direction
2607 * @chmap: channel map elements (for query)
2608 * @max_channels: the max number of channels for the stream
2609 * @private_value: the value passed to each kcontrol's private_value field
2610 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2611 *
2612 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002613 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002614 */
2615int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2616 const struct snd_pcm_chmap_elem *chmap,
2617 int max_channels,
2618 unsigned long private_value,
2619 struct snd_pcm_chmap **info_ret)
2620{
2621 struct snd_pcm_chmap *info;
2622 struct snd_kcontrol_new knew = {
2623 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2624 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002625 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2626 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2627 .info = pcm_chmap_ctl_info,
2628 .get = pcm_chmap_ctl_get,
2629 .tlv.c = pcm_chmap_ctl_tlv,
2630 };
2631 int err;
2632
Takashi Iwai8d879be2016-05-10 16:07:40 +02002633 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2634 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002635 info = kzalloc(sizeof(*info), GFP_KERNEL);
2636 if (!info)
2637 return -ENOMEM;
2638 info->pcm = pcm;
2639 info->stream = stream;
2640 info->chmap = chmap;
2641 info->max_channels = max_channels;
2642 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2643 knew.name = "Playback Channel Map";
2644 else
2645 knew.name = "Capture Channel Map";
2646 knew.device = pcm->device;
2647 knew.count = pcm->streams[stream].substream_count;
2648 knew.private_value = private_value;
2649 info->kctl = snd_ctl_new1(&knew, info);
2650 if (!info->kctl) {
2651 kfree(info);
2652 return -ENOMEM;
2653 }
2654 info->kctl->private_free = pcm_chmap_ctl_private_free;
2655 err = snd_ctl_add(pcm->card, info->kctl);
2656 if (err < 0)
2657 return err;
2658 pcm->streams[stream].chmap_kctl = info->kctl;
2659 if (info_ret)
2660 *info_ret = info;
2661 return 0;
2662}
2663EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
Banajit Goswami55b24c52016-11-21 19:08:27 -08002664
2665/**
2666 * snd_pcm_add_volume_ctls - create volume control elements
2667 * @pcm: the assigned PCM instance
2668 * @stream: stream direction
2669 * @max_length: the max length of the volume parameter of stream
2670 * @private_value: the value passed to each kcontrol's private_value field
2671 * @info_ret: store struct snd_pcm_volume instance if non-NULL
2672 *
2673 * Create volume control elements assigned to the given PCM stream(s).
2674 * Returns zero if succeed, or a negative error value.
2675 */
2676int snd_pcm_add_volume_ctls(struct snd_pcm *pcm, int stream,
2677 const struct snd_pcm_volume_elem *volume,
2678 int max_length,
2679 unsigned long private_value,
2680 struct snd_pcm_volume **info_ret)
2681{
2682 struct snd_pcm_volume *info;
2683 struct snd_kcontrol_new knew = {
2684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2685 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2686 SNDRV_CTL_ELEM_ACCESS_READWRITE,
2687 .info = pcm_volume_ctl_info,
2688 };
2689 int err;
2690 int size;
2691
2692 info = kzalloc(sizeof(*info), GFP_KERNEL);
2693 if (!info)
2694 return -ENOMEM;
2695 info->pcm = pcm;
2696 info->stream = stream;
2697 info->volume = volume;
2698 info->max_length = max_length;
2699 size = sizeof("Playback ") + sizeof(" Volume") +
2700 STRING_LENGTH_OF_INT*sizeof(char) + 1;
2701 knew.name = kzalloc(size, GFP_KERNEL);
2702 if (!knew.name) {
2703 kfree(info);
2704 return -ENOMEM;
2705 }
2706 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2707 snprintf((char *)knew.name, size, "%s %d %s",
2708 "Playback", pcm->device, "Volume");
2709 else
2710 snprintf((char *)knew.name, size, "%s %d %s",
2711 "Capture", pcm->device, "Volume");
2712 knew.device = pcm->device;
2713 knew.count = pcm->streams[stream].substream_count;
2714 knew.private_value = private_value;
2715 info->kctl = snd_ctl_new1(&knew, info);
2716 if (!info->kctl) {
2717 kfree(info);
2718 kfree(knew.name);
2719 return -ENOMEM;
2720 }
2721 info->kctl->private_free = pcm_volume_ctl_private_free;
2722 err = snd_ctl_add(pcm->card, info->kctl);
2723 if (err < 0) {
2724 kfree(info);
2725 kfree(knew.name);
2726 return -ENOMEM;
2727 }
2728 pcm->streams[stream].vol_kctl = info->kctl;
2729 if (info_ret)
2730 *info_ret = info;
2731 kfree(knew.name);
2732 return 0;
2733}
2734EXPORT_SYMBOL(snd_pcm_add_volume_ctls);
Banajit Goswamicec03b62016-11-21 19:22:56 -08002735
2736static int pcm_usr_ctl_info(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2738{
2739 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2740 uinfo->count = MAX_USR_CTRL_CNT;
2741 uinfo->value.integer.min = 0;
2742 uinfo->value.integer.max = INT_MAX;
2743 return 0;
2744}
2745
2746static void pcm_usr_ctl_private_free(struct snd_kcontrol *kcontrol)
2747{
2748 struct snd_pcm_usr *info = snd_kcontrol_chip(kcontrol);
2749
2750 info->pcm->streams[info->stream].usr_kctl = NULL;
2751 kfree(info);
2752}
2753
2754/**
2755 * snd_pcm_add_usr_ctls - create user control elements
2756 * @pcm: the assigned PCM instance
2757 * @stream: stream direction
2758 * @max_length: the max length of the user parameter of stream
2759 * @private_value: the value passed to each kcontrol's private_value field
2760 * @info_ret: store struct snd_pcm_usr instance if non-NULL
2761 *
2762 * Create usr control elements assigned to the given PCM stream(s).
2763 * Returns zero if succeed, or a negative error value.
2764 */
2765int snd_pcm_add_usr_ctls(struct snd_pcm *pcm, int stream,
2766 const struct snd_pcm_usr_elem *usr,
2767 int max_length, int max_kctrl_str_len,
2768 unsigned long private_value,
2769 struct snd_pcm_usr **info_ret)
2770{
2771 struct snd_pcm_usr *info;
2772 struct snd_kcontrol_new knew = {
2773 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2774 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2775 .info = pcm_usr_ctl_info,
2776 };
2777 int err;
2778 char *buf;
2779
2780 info = kzalloc(sizeof(*info), GFP_KERNEL);
2781 if (!info)
2782 return -ENOMEM;
2783
2784 info->pcm = pcm;
2785 info->stream = stream;
2786 info->usr = usr;
2787 info->max_length = max_length;
2788 buf = kzalloc(max_kctrl_str_len, GFP_KERNEL);
2789 if (!buf) {
2790 pr_err("%s: buffer allocation failed\n", __func__);
2791 kfree(info);
2792 return -ENOMEM;
2793 }
2794 knew.name = buf;
2795 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2796 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2797 "Playback", pcm->device, "User kcontrol");
2798 else
2799 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2800 "Capture", pcm->device, "User kcontrol");
2801 knew.device = pcm->device;
2802 knew.count = pcm->streams[stream].substream_count;
2803 knew.private_value = private_value;
2804 info->kctl = snd_ctl_new1(&knew, info);
2805 if (!info->kctl) {
2806 kfree(info);
2807 kfree(knew.name);
2808 pr_err("%s: snd_ctl_new failed\n", __func__);
2809 return -ENOMEM;
2810 }
2811 info->kctl->private_free = pcm_usr_ctl_private_free;
2812 err = snd_ctl_add(pcm->card, info->kctl);
2813 if (err < 0) {
2814 kfree(info);
2815 kfree(knew.name);
2816 pr_err("%s: snd_ctl_add failed:%d\n", __func__,
2817 err);
2818 return -ENOMEM;
2819 }
2820 pcm->streams[stream].usr_kctl = info->kctl;
2821 if (info_ret)
2822 *info_ret = info;
2823 kfree(knew.name);
2824 return 0;
2825}
2826EXPORT_SYMBOL(snd_pcm_add_usr_ctls);