blob: bbba7be09112562b4772eac7751eaf53d44a4fcf [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 }
270 runtime->status->audio_tstamp = *audio_tstamp;
271 runtime->status->tstamp = *curr_tstamp;
272
273 /*
274 * re-take a driver timestamp to let apps detect if the reference tstamp
275 * read by low-level hardware was provided with a delay
276 */
277 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
278 runtime->driver_tstamp = driver_tstamp;
279}
280
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100281static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
282 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Takashi Iwai877211f2005-11-17 13:59:38 +0100284 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100286 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200287 snd_pcm_sframes_t hdelta, delta;
288 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500289 unsigned long curr_jiffies;
290 struct timespec curr_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500291 struct timespec audio_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500292 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200294 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500295
296 /*
297 * group pointer, time and jiffies reads to allow for more
298 * accurate correlations/corrections.
299 * The values are stored at the end of this routine after
300 * corrections for hw_ptr position
301 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100302 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500303 curr_jiffies = jiffies;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500304 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600305 if ((substream->ops->get_time_info) &&
306 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
307 substream->ops->get_time_info(substream, &curr_tstamp,
308 &audio_tstamp,
309 &runtime->audio_tstamp_config,
310 &runtime->audio_tstamp_report);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500311
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600312 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
313 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
314 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
315 } else
316 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (pos == SNDRV_PCM_POS_XRUN) {
320 xrun(substream);
321 return -EPIPE;
322 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100323 if (pos >= runtime->buffer_size) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100324 if (printk_ratelimit()) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100325 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200326 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100327 pcm_err(substream->pcm,
Takashi Iwai0ab1ace2016-03-10 20:56:20 +0100328 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 name, pos, runtime->buffer_size,
330 runtime->period_size);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100331 }
332 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200333 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100334 pos -= pos % runtime->min_align;
Takashi Iwaif5914902014-11-04 12:45:59 +0100335 trace_hwptr(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100336 hw_base = runtime->hw_ptr_base;
337 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100338 if (in_interrupt) {
339 /* we know that one period was processed */
340 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100341 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100342 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200343 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500344 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Koro Chen13a98832015-05-13 22:39:03 +0800345 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200346 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500347 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200348 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500349 crossed_boundary++;
350 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200351 new_hw_ptr = hw_base + pos;
352 goto __delta;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100356 /* new_hw_ptr might be lower than old_hw_ptr in case when */
357 /* pointer crosses the end of the ring buffer */
358 if (new_hw_ptr < old_hw_ptr) {
359 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500360 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100361 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500362 crossed_boundary++;
363 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100364 new_hw_ptr = hw_base + pos;
365 }
366 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200367 delta = new_hw_ptr - old_hw_ptr;
368 if (delta < 0)
369 delta += runtime->boundary;
Clemens Ladischab69a492010-11-15 10:46:23 +0100370
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100371 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200372 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100373 /*
374 * Without regular period interrupts, we have to check
375 * the elapsed time to detect xruns.
376 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500377 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Preetam Singh Ranawate12ae3c2017-01-17 16:24:40 +0530378 if ((jdelta < runtime->hw_ptr_buffer_jiffies / 2) ||
379 (runtime->hw_ptr_buffer_jiffies <= 0))
Clemens Ladisch47228e42010-11-18 09:53:07 +0100380 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100381 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200382 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
383 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100384 delta += runtime->buffer_size;
385 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500386 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100387 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500388 crossed_boundary++;
389 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100390 new_hw_ptr = hw_base + pos;
391 hdelta -= runtime->hw_ptr_buffer_jiffies;
392 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100393 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100394 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100395
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100396 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100397 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100398 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
399 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
400 substream->stream, (long)pos,
401 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100402 return 0;
403 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200404
405 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100406 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200407 goto no_jiffies_check;
408
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200409 /* Skip the jiffies check for hardwares with BATCH flag.
410 * Such hardware usually just increases the position at each IRQ,
411 * thus it can't give any strange position.
412 */
413 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
414 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100415 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200416 if (hdelta < runtime->delay)
417 goto no_jiffies_check;
418 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500419 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200420 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
421 delta = jdelta /
422 (((runtime->period_size * HZ) / runtime->rate)
423 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100424 /* move new_hw_ptr according jiffies not pos variable */
425 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100426 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100427 /* use loop to avoid checks for delta overflows */
428 /* the delta value is small or zero in most cases */
429 while (delta > 0) {
430 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500431 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100432 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500433 crossed_boundary--;
434 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100435 delta--;
436 }
437 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100438 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
439 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200440 (long)pos, (long)hdelta,
441 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100442 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100443 (unsigned long)old_hw_ptr,
444 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100445 /* reset values to proper state */
446 delta = 0;
447 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200448 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200449 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200450 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100451 hw_ptr_error(substream, in_interrupt,
452 "Lost interrupts?",
453 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100454 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100455 (long)new_hw_ptr,
456 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100457 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100458
Clemens Ladischab69a492010-11-15 10:46:23 +0100459 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600460 if (runtime->status->hw_ptr == new_hw_ptr) {
461 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100462 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600463 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
466 runtime->silence_size > 0)
467 snd_pcm_playback_silence(substream, new_hw_ptr);
468
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100469 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200470 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
471 if (delta < 0)
472 delta += runtime->boundary;
473 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
474 runtime->hw_ptr_interrupt += delta;
475 if (runtime->hw_ptr_interrupt >= runtime->boundary)
476 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100477 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100478 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500480 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500481 if (crossed_boundary) {
482 snd_BUG_ON(crossed_boundary != 1);
483 runtime->hw_ptr_wrap += runtime->boundary;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600486 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500487
Jaroslav Kysela12509322010-01-07 15:36:31 +0100488 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100492int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100494 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/**
498 * snd_pcm_set_ops - set the PCM operators
499 * @pcm: the pcm instance
500 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
501 * @ops: the operator table
502 *
503 * Sets the given PCM operators to the pcm instance.
504 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200505void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
506 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwai877211f2005-11-17 13:59:38 +0100508 struct snd_pcm_str *stream = &pcm->streams[direction];
509 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 for (substream = stream->substream; substream != NULL; substream = substream->next)
512 substream->ops = ops;
513}
514
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200515EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517/**
518 * snd_pcm_sync - set the PCM sync id
519 * @substream: the pcm substream
520 *
521 * Sets the PCM sync identifier for the card.
522 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100523void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Takashi Iwai877211f2005-11-17 13:59:38 +0100525 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 runtime->sync.id32[0] = substream->pcm->card->number;
528 runtime->sync.id32[1] = -1;
529 runtime->sync.id32[2] = -1;
530 runtime->sync.id32[3] = -1;
531}
532
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200533EXPORT_SYMBOL(snd_pcm_set_sync);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * Standard ioctl routine
537 */
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static inline unsigned int div32(unsigned int a, unsigned int b,
540 unsigned int *r)
541{
542 if (b == 0) {
543 *r = 0;
544 return UINT_MAX;
545 }
546 *r = a % b;
547 return a / b;
548}
549
550static inline unsigned int div_down(unsigned int a, unsigned int b)
551{
552 if (b == 0)
553 return UINT_MAX;
554 return a / b;
555}
556
557static inline unsigned int div_up(unsigned int a, unsigned int b)
558{
559 unsigned int r;
560 unsigned int q;
561 if (b == 0)
562 return UINT_MAX;
563 q = div32(a, b, &r);
564 if (r)
565 ++q;
566 return q;
567}
568
569static inline unsigned int mul(unsigned int a, unsigned int b)
570{
571 if (a == 0)
572 return 0;
573 if (div_down(UINT_MAX, a) < b)
574 return UINT_MAX;
575 return a * b;
576}
577
578static inline unsigned int muldiv32(unsigned int a, unsigned int b,
579 unsigned int c, unsigned int *r)
580{
581 u_int64_t n = (u_int64_t) a * b;
582 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200583 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *r = 0;
585 return UINT_MAX;
586 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200587 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (n >= UINT_MAX) {
589 *r = 0;
590 return UINT_MAX;
591 }
592 return n;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
596 * snd_interval_refine - refine the interval value of configurator
597 * @i: the interval value to refine
598 * @v: the interval value to refer to
599 *
600 * Refines the interval value with the reference value.
601 * The interval is changed to the range satisfying both intervals.
602 * The interval status (min, max, integer, etc.) are evaluated.
603 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100604 * Return: Positive if the value is changed, zero if it's not changed, or a
605 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100607int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200610 if (snd_BUG_ON(snd_interval_empty(i)))
611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (i->min < v->min) {
613 i->min = v->min;
614 i->openmin = v->openmin;
615 changed = 1;
616 } else if (i->min == v->min && !i->openmin && v->openmin) {
617 i->openmin = 1;
618 changed = 1;
619 }
620 if (i->max > v->max) {
621 i->max = v->max;
622 i->openmax = v->openmax;
623 changed = 1;
624 } else if (i->max == v->max && !i->openmax && v->openmax) {
625 i->openmax = 1;
626 changed = 1;
627 }
628 if (!i->integer && v->integer) {
629 i->integer = 1;
630 changed = 1;
631 }
632 if (i->integer) {
633 if (i->openmin) {
634 i->min++;
635 i->openmin = 0;
636 }
637 if (i->openmax) {
638 i->max--;
639 i->openmax = 0;
640 }
641 } else if (!i->openmin && !i->openmax && i->min == i->max)
642 i->integer = 1;
643 if (snd_interval_checkempty(i)) {
644 snd_interval_none(i);
645 return -EINVAL;
646 }
647 return changed;
648}
649
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200650EXPORT_SYMBOL(snd_interval_refine);
651
Takashi Iwai877211f2005-11-17 13:59:38 +0100652static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200654 if (snd_BUG_ON(snd_interval_empty(i)))
655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (snd_interval_single(i))
657 return 0;
658 i->max = i->min;
659 i->openmax = i->openmin;
660 if (i->openmax)
661 i->max++;
662 return 1;
663}
664
Takashi Iwai877211f2005-11-17 13:59:38 +0100665static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200667 if (snd_BUG_ON(snd_interval_empty(i)))
668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (snd_interval_single(i))
670 return 0;
671 i->min = i->max;
672 i->openmin = i->openmax;
673 if (i->openmin)
674 i->min--;
675 return 1;
676}
677
Takashi Iwai877211f2005-11-17 13:59:38 +0100678void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 if (a->empty || b->empty) {
681 snd_interval_none(c);
682 return;
683 }
684 c->empty = 0;
685 c->min = mul(a->min, b->min);
686 c->openmin = (a->openmin || b->openmin);
687 c->max = mul(a->max, b->max);
688 c->openmax = (a->openmax || b->openmax);
689 c->integer = (a->integer && b->integer);
690}
691
692/**
693 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200694 * @a: dividend
695 * @b: divisor
696 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
698 * c = a / b
699 *
700 * Returns non-zero if the value is changed, zero if not changed.
701 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100702void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 unsigned int r;
705 if (a->empty || b->empty) {
706 snd_interval_none(c);
707 return;
708 }
709 c->empty = 0;
710 c->min = div32(a->min, b->max, &r);
711 c->openmin = (r || a->openmin || b->openmax);
712 if (b->min > 0) {
713 c->max = div32(a->max, b->min, &r);
714 if (r) {
715 c->max++;
716 c->openmax = 1;
717 } else
718 c->openmax = (a->openmax || b->openmin);
719 } else {
720 c->max = UINT_MAX;
721 c->openmax = 0;
722 }
723 c->integer = 0;
724}
725
726/**
727 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200728 * @a: dividend 1
729 * @b: dividend 2
730 * @k: divisor (as integer)
731 * @c: result
732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * c = a * b / k
734 *
735 * Returns non-zero if the value is changed, zero if not changed.
736 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100737void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
738 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned int r;
741 if (a->empty || b->empty) {
742 snd_interval_none(c);
743 return;
744 }
745 c->empty = 0;
746 c->min = muldiv32(a->min, b->min, k, &r);
747 c->openmin = (r || a->openmin || b->openmin);
748 c->max = muldiv32(a->max, b->max, k, &r);
749 if (r) {
750 c->max++;
751 c->openmax = 1;
752 } else
753 c->openmax = (a->openmax || b->openmax);
754 c->integer = 0;
755}
756
757/**
758 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200759 * @a: dividend 1
760 * @k: dividend 2 (as integer)
761 * @b: divisor
762 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *
764 * c = a * k / b
765 *
766 * Returns non-zero if the value is changed, zero if not changed.
767 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100768void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
769 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 unsigned int r;
772 if (a->empty || b->empty) {
773 snd_interval_none(c);
774 return;
775 }
776 c->empty = 0;
777 c->min = muldiv32(a->min, k, b->max, &r);
778 c->openmin = (r || a->openmin || b->openmax);
779 if (b->min > 0) {
780 c->max = muldiv32(a->max, k, b->min, &r);
781 if (r) {
782 c->max++;
783 c->openmax = 1;
784 } else
785 c->openmax = (a->openmax || b->openmin);
786 } else {
787 c->max = UINT_MAX;
788 c->openmax = 0;
789 }
790 c->integer = 0;
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/* ---- */
794
795
796/**
797 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200798 * @i: interval to refine
799 * @rats_count: number of ratnum_t
800 * @rats: ratnum_t array
801 * @nump: pointer to store the resultant numerator
802 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100804 * Return: Positive if the value is changed, zero if it's not changed, or a
805 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100807int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100808 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100809 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100811 unsigned int best_num, best_den;
812 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100814 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100816 unsigned int result_num, result_den;
817 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 best_num = best_den = best_diff = 0;
820 for (k = 0; k < rats_count; ++k) {
821 unsigned int num = rats[k].num;
822 unsigned int den;
823 unsigned int q = i->min;
824 int diff;
825 if (q == 0)
826 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100827 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (den < rats[k].den_min)
829 continue;
830 if (den > rats[k].den_max)
831 den = rats[k].den_max;
832 else {
833 unsigned int r;
834 r = (den - rats[k].den_min) % rats[k].den_step;
835 if (r != 0)
836 den -= r;
837 }
838 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100839 if (diff < 0)
840 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (best_num == 0 ||
842 diff * best_den < best_diff * den) {
843 best_diff = diff;
844 best_den = den;
845 best_num = num;
846 }
847 }
848 if (best_den == 0) {
849 i->empty = 1;
850 return -EINVAL;
851 }
852 t.min = div_down(best_num, best_den);
853 t.openmin = !!(best_num % best_den);
854
Krzysztof Helt8374e242009-12-21 17:07:08 +0100855 result_num = best_num;
856 result_diff = best_diff;
857 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 best_num = best_den = best_diff = 0;
859 for (k = 0; k < rats_count; ++k) {
860 unsigned int num = rats[k].num;
861 unsigned int den;
862 unsigned int q = i->max;
863 int diff;
864 if (q == 0) {
865 i->empty = 1;
866 return -EINVAL;
867 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100868 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (den > rats[k].den_max)
870 continue;
871 if (den < rats[k].den_min)
872 den = rats[k].den_min;
873 else {
874 unsigned int r;
875 r = (den - rats[k].den_min) % rats[k].den_step;
876 if (r != 0)
877 den += rats[k].den_step - r;
878 }
879 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100880 if (diff < 0)
881 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (best_num == 0 ||
883 diff * best_den < best_diff * den) {
884 best_diff = diff;
885 best_den = den;
886 best_num = num;
887 }
888 }
889 if (best_den == 0) {
890 i->empty = 1;
891 return -EINVAL;
892 }
893 t.max = div_up(best_num, best_den);
894 t.openmax = !!(best_num % best_den);
895 t.integer = 0;
896 err = snd_interval_refine(i, &t);
897 if (err < 0)
898 return err;
899
900 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100901 if (best_diff * result_den < result_diff * best_den) {
902 result_num = best_num;
903 result_den = best_den;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100906 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100908 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 return err;
911}
912
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200913EXPORT_SYMBOL(snd_interval_ratnum);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/**
916 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200917 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100918 * @rats_count: number of struct ratden
919 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200920 * @nump: pointer to store the resultant numerator
921 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100923 * Return: Positive if the value is changed, zero if it's not changed, or a
924 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100926static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100927 unsigned int rats_count,
928 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int *nump, unsigned int *denp)
930{
931 unsigned int best_num, best_diff, best_den;
932 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100933 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int err;
935
936 best_num = best_den = best_diff = 0;
937 for (k = 0; k < rats_count; ++k) {
938 unsigned int num;
939 unsigned int den = rats[k].den;
940 unsigned int q = i->min;
941 int diff;
942 num = mul(q, den);
943 if (num > rats[k].num_max)
944 continue;
945 if (num < rats[k].num_min)
946 num = rats[k].num_max;
947 else {
948 unsigned int r;
949 r = (num - rats[k].num_min) % rats[k].num_step;
950 if (r != 0)
951 num += rats[k].num_step - r;
952 }
953 diff = num - q * den;
954 if (best_num == 0 ||
955 diff * best_den < best_diff * den) {
956 best_diff = diff;
957 best_den = den;
958 best_num = num;
959 }
960 }
961 if (best_den == 0) {
962 i->empty = 1;
963 return -EINVAL;
964 }
965 t.min = div_down(best_num, best_den);
966 t.openmin = !!(best_num % best_den);
967
968 best_num = best_den = best_diff = 0;
969 for (k = 0; k < rats_count; ++k) {
970 unsigned int num;
971 unsigned int den = rats[k].den;
972 unsigned int q = i->max;
973 int diff;
974 num = mul(q, den);
975 if (num < rats[k].num_min)
976 continue;
977 if (num > rats[k].num_max)
978 num = rats[k].num_max;
979 else {
980 unsigned int r;
981 r = (num - rats[k].num_min) % rats[k].num_step;
982 if (r != 0)
983 num -= r;
984 }
985 diff = q * den - num;
986 if (best_num == 0 ||
987 diff * best_den < best_diff * den) {
988 best_diff = diff;
989 best_den = den;
990 best_num = num;
991 }
992 }
993 if (best_den == 0) {
994 i->empty = 1;
995 return -EINVAL;
996 }
997 t.max = div_up(best_num, best_den);
998 t.openmax = !!(best_num % best_den);
999 t.integer = 0;
1000 err = snd_interval_refine(i, &t);
1001 if (err < 0)
1002 return err;
1003
1004 if (snd_interval_single(i)) {
1005 if (nump)
1006 *nump = best_num;
1007 if (denp)
1008 *denp = best_den;
1009 }
1010 return err;
1011}
1012
1013/**
1014 * snd_interval_list - refine the interval value from the list
1015 * @i: the interval value to refine
1016 * @count: the number of elements in the list
1017 * @list: the value list
1018 * @mask: the bit-mask to evaluate
1019 *
1020 * Refines the interval value from the list.
1021 * When mask is non-zero, only the elements corresponding to bit 1 are
1022 * evaluated.
1023 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001024 * Return: Positive if the value is changed, zero if it's not changed, or a
1025 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
Mark Brown4af87a92012-03-14 19:48:43 +00001027int snd_interval_list(struct snd_interval *i, unsigned int count,
1028 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001031 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001032
1033 if (!count) {
1034 i->empty = 1;
1035 return -EINVAL;
1036 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001037 snd_interval_any(&list_range);
1038 list_range.min = UINT_MAX;
1039 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for (k = 0; k < count; k++) {
1041 if (mask && !(mask & (1 << k)))
1042 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001043 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001045 list_range.min = min(list_range.min, list[k]);
1046 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001048 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001051EXPORT_SYMBOL(snd_interval_list);
1052
Peter Rosinf66f8982015-01-28 15:16:06 +01001053/**
1054 * snd_interval_ranges - refine the interval value from the list of ranges
1055 * @i: the interval value to refine
1056 * @count: the number of elements in the list of ranges
1057 * @ranges: the ranges list
1058 * @mask: the bit-mask to evaluate
1059 *
1060 * Refines the interval value from the list of ranges.
1061 * When mask is non-zero, only the elements corresponding to bit 1 are
1062 * evaluated.
1063 *
1064 * Return: Positive if the value is changed, zero if it's not changed, or a
1065 * negative error code.
1066 */
1067int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1068 const struct snd_interval *ranges, unsigned int mask)
1069{
1070 unsigned int k;
1071 struct snd_interval range_union;
1072 struct snd_interval range;
1073
1074 if (!count) {
1075 snd_interval_none(i);
1076 return -EINVAL;
1077 }
1078 snd_interval_any(&range_union);
1079 range_union.min = UINT_MAX;
1080 range_union.max = 0;
1081 for (k = 0; k < count; k++) {
1082 if (mask && !(mask & (1 << k)))
1083 continue;
1084 snd_interval_copy(&range, &ranges[k]);
1085 if (snd_interval_refine(&range, i) < 0)
1086 continue;
1087 if (snd_interval_empty(&range))
1088 continue;
1089
1090 if (range.min < range_union.min) {
1091 range_union.min = range.min;
1092 range_union.openmin = 1;
1093 }
1094 if (range.min == range_union.min && !range.openmin)
1095 range_union.openmin = 0;
1096 if (range.max > range_union.max) {
1097 range_union.max = range.max;
1098 range_union.openmax = 1;
1099 }
1100 if (range.max == range_union.max && !range.openmax)
1101 range_union.openmax = 0;
1102 }
1103 return snd_interval_refine(i, &range_union);
1104}
1105EXPORT_SYMBOL(snd_interval_ranges);
1106
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001107static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 unsigned int n;
1110 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001111 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (n != 0 || i->openmin) {
1113 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001114 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 changed = 1;
1116 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001117 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (n != 0 || i->openmax) {
1119 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001120 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 changed = 1;
1122 }
1123 if (snd_interval_checkempty(i)) {
1124 i->empty = 1;
1125 return -EINVAL;
1126 }
1127 return changed;
1128}
1129
1130/* Info constraints helpers */
1131
1132/**
1133 * snd_pcm_hw_rule_add - add the hw-constraint rule
1134 * @runtime: the pcm runtime instance
1135 * @cond: condition bits
1136 * @var: the variable to evaluate
1137 * @func: the evaluation function
1138 * @private: the private data pointer passed to function
1139 * @dep: the dependent variables
1140 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001141 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001143int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int var,
1145 snd_pcm_hw_rule_func_t func, void *private,
1146 int dep, ...)
1147{
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1149 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 unsigned int k;
1151 va_list args;
1152 va_start(args, dep);
1153 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001154 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned int new_rules = constrs->rules_all + 16;
1156 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001157 if (!new) {
1158 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (constrs->rules) {
1162 memcpy(new, constrs->rules,
1163 constrs->rules_num * sizeof(*c));
1164 kfree(constrs->rules);
1165 }
1166 constrs->rules = new;
1167 constrs->rules_all = new_rules;
1168 }
1169 c = &constrs->rules[constrs->rules_num];
1170 c->cond = cond;
1171 c->func = func;
1172 c->var = var;
1173 c->private = private;
1174 k = 0;
1175 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001176 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1177 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001178 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 c->deps[k++] = dep;
1181 if (dep < 0)
1182 break;
1183 dep = va_arg(args, int);
1184 }
1185 constrs->rules_num++;
1186 va_end(args);
1187 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001190EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001193 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001194 * @runtime: PCM runtime instance
1195 * @var: hw_params variable to apply the mask
1196 * @mask: the bitmap mask
1197 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001198 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001199 *
1200 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001202int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 u_int32_t mask)
1204{
Takashi Iwai877211f2005-11-17 13:59:38 +01001205 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1206 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *maskp->bits &= mask;
1208 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1209 if (*maskp->bits == 0)
1210 return -EINVAL;
1211 return 0;
1212}
1213
1214/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001215 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001216 * @runtime: PCM runtime instance
1217 * @var: hw_params variable to apply the mask
1218 * @mask: the 64bit bitmap mask
1219 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001220 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001221 *
1222 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001224int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 u_int64_t mask)
1226{
Takashi Iwai877211f2005-11-17 13:59:38 +01001227 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1228 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 maskp->bits[0] &= (u_int32_t)mask;
1230 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1231 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1232 if (! maskp->bits[0] && ! maskp->bits[1])
1233 return -EINVAL;
1234 return 0;
1235}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001236EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001239 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001240 * @runtime: PCM runtime instance
1241 * @var: hw_params variable to apply the integer constraint
1242 *
1243 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001244 *
1245 * Return: Positive if the value is changed, zero if it's not changed, or a
1246 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001248int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return snd_interval_setinteger(constrs_interval(constrs, var));
1252}
1253
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001254EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001257 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001258 * @runtime: PCM runtime instance
1259 * @var: hw_params variable to apply the range
1260 * @min: the minimal value
1261 * @max: the maximal value
1262 *
1263 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001264 *
1265 * Return: Positive if the value is changed, zero if it's not changed, or a
1266 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001268int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unsigned int min, unsigned int max)
1270{
Takashi Iwai877211f2005-11-17 13:59:38 +01001271 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1272 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 t.min = min;
1274 t.max = max;
1275 t.openmin = t.openmax = 0;
1276 t.integer = 0;
1277 return snd_interval_refine(constrs_interval(constrs, var), &t);
1278}
1279
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001280EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1281
Takashi Iwai877211f2005-11-17 13:59:38 +01001282static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1283 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwai877211f2005-11-17 13:59:38 +01001285 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1287}
1288
1289
1290/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001291 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001292 * @runtime: PCM runtime instance
1293 * @cond: condition bits
1294 * @var: hw_params variable to apply the list constraint
1295 * @l: list
1296 *
1297 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001298 *
1299 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001301int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unsigned int cond,
1303 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001304 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001307 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 var, -1);
1309}
1310
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001311EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1312
Peter Rosinf66f8982015-01-28 15:16:06 +01001313static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1314 struct snd_pcm_hw_rule *rule)
1315{
1316 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1317 return snd_interval_ranges(hw_param_interval(params, rule->var),
1318 r->count, r->ranges, r->mask);
1319}
1320
1321
1322/**
1323 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1324 * @runtime: PCM runtime instance
1325 * @cond: condition bits
1326 * @var: hw_params variable to apply the list of range constraints
1327 * @r: ranges
1328 *
1329 * Apply the list of range constraints to an interval parameter.
1330 *
1331 * Return: Zero if successful, or a negative error code on failure.
1332 */
1333int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1334 unsigned int cond,
1335 snd_pcm_hw_param_t var,
1336 const struct snd_pcm_hw_constraint_ranges *r)
1337{
1338 return snd_pcm_hw_rule_add(runtime, cond, var,
1339 snd_pcm_hw_rule_ranges, (void *)r,
1340 var, -1);
1341}
1342EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1345 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001347 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned int num = 0, den = 0;
1349 int err;
1350 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1351 r->nrats, r->rats, &num, &den);
1352 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1353 params->rate_num = num;
1354 params->rate_den = den;
1355 }
1356 return err;
1357}
1358
1359/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001360 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001361 * @runtime: PCM runtime instance
1362 * @cond: condition bits
1363 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001364 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001365 *
1366 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001368int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 unsigned int cond,
1370 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001371 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001374 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 var, -1);
1376}
1377
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001378EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1379
Takashi Iwai877211f2005-11-17 13:59:38 +01001380static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1381 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001383 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 unsigned int num = 0, den = 0;
1385 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1386 r->nrats, r->rats, &num, &den);
1387 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1388 params->rate_num = num;
1389 params->rate_den = den;
1390 }
1391 return err;
1392}
1393
1394/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001395 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001396 * @runtime: PCM runtime instance
1397 * @cond: condition bits
1398 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001399 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001400 *
1401 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001403int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned int cond,
1405 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001406 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001409 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 var, -1);
1411}
1412
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001413EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1414
Takashi Iwai877211f2005-11-17 13:59:38 +01001415static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1416 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 unsigned int l = (unsigned long) rule->private;
1419 int width = l & 0xffff;
1420 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001421 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001422
1423 if (!snd_interval_single(i))
1424 return 0;
1425
1426 if ((snd_interval_value(i) == width) ||
1427 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001428 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431}
1432
1433/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001435 * @runtime: PCM runtime instance
1436 * @cond: condition bits
1437 * @width: sample bits width
1438 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001439 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001440 * This constraint will set the number of most significant bits (msbits) if a
1441 * sample format with the specified width has been select. If width is set to 0
1442 * the msbits will be set for any sample format with a width larger than the
1443 * specified msbits.
1444 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001445 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001447int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unsigned int cond,
1449 unsigned int width,
1450 unsigned int msbits)
1451{
1452 unsigned long l = (msbits << 16) | width;
1453 return snd_pcm_hw_rule_add(runtime, cond, -1,
1454 snd_pcm_hw_rule_msbits,
1455 (void*) l,
1456 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1457}
1458
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001459EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1460
Takashi Iwai877211f2005-11-17 13:59:38 +01001461static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1462 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001465 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001469 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001470 * @runtime: PCM runtime instance
1471 * @cond: condition bits
1472 * @var: hw_params variable to apply the step constraint
1473 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001474 *
1475 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001477int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 unsigned int cond,
1479 snd_pcm_hw_param_t var,
1480 unsigned long step)
1481{
1482 return snd_pcm_hw_rule_add(runtime, cond, var,
1483 snd_pcm_hw_rule_step, (void *) step,
1484 var, -1);
1485}
1486
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001487EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1488
Takashi Iwai877211f2005-11-17 13:59:38 +01001489static 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 -07001490{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001491 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1493 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1494 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1495 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1496 };
1497 return snd_interval_list(hw_param_interval(params, rule->var),
1498 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1499}
1500
1501/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001502 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001503 * @runtime: PCM runtime instance
1504 * @cond: condition bits
1505 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001506 *
1507 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001509int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unsigned int cond,
1511 snd_pcm_hw_param_t var)
1512{
1513 return snd_pcm_hw_rule_add(runtime, cond, var,
1514 snd_pcm_hw_rule_pow2, NULL,
1515 var, -1);
1516}
1517
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001518EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1519
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001520static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1521 struct snd_pcm_hw_rule *rule)
1522{
1523 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1524 struct snd_interval *rate;
1525
1526 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1527 return snd_interval_list(rate, 1, &base_rate, 0);
1528}
1529
1530/**
1531 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1532 * @runtime: PCM runtime instance
1533 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001534 *
1535 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001536 */
1537int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1538 unsigned int base_rate)
1539{
1540 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1541 SNDRV_PCM_HW_PARAM_RATE,
1542 snd_pcm_hw_rule_noresample_func,
1543 (void *)(uintptr_t)base_rate,
1544 SNDRV_PCM_HW_PARAM_RATE, -1);
1545}
1546EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1547
Takashi Iwai877211f2005-11-17 13:59:38 +01001548static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001549 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 if (hw_is_mask(var)) {
1552 snd_mask_any(hw_param_mask(params, var));
1553 params->cmask |= 1 << var;
1554 params->rmask |= 1 << var;
1555 return;
1556 }
1557 if (hw_is_interval(var)) {
1558 snd_interval_any(hw_param_interval(params, var));
1559 params->cmask |= 1 << var;
1560 params->rmask |= 1 << var;
1561 return;
1562 }
1563 snd_BUG();
1564}
1565
Takashi Iwai877211f2005-11-17 13:59:38 +01001566void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 unsigned int k;
1569 memset(params, 0, sizeof(*params));
1570 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1571 _snd_pcm_hw_param_any(params, k);
1572 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1573 _snd_pcm_hw_param_any(params, k);
1574 params->info = ~0U;
1575}
1576
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001577EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001580 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001581 * @params: the hw_params instance
1582 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001583 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001585 * Return: The value for field @var if it's fixed in configuration space
1586 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001588int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1589 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001592 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!snd_mask_single(mask))
1594 return -EINVAL;
1595 if (dir)
1596 *dir = 0;
1597 return snd_mask_value(mask);
1598 }
1599 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001600 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!snd_interval_single(i))
1602 return -EINVAL;
1603 if (dir)
1604 *dir = i->openmin;
1605 return snd_interval_value(i);
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return -EINVAL;
1608}
1609
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001610EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Takashi Iwai877211f2005-11-17 13:59:38 +01001612void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 snd_pcm_hw_param_t var)
1614{
1615 if (hw_is_mask(var)) {
1616 snd_mask_none(hw_param_mask(params, var));
1617 params->cmask |= 1 << var;
1618 params->rmask |= 1 << var;
1619 } else if (hw_is_interval(var)) {
1620 snd_interval_none(hw_param_interval(params, var));
1621 params->cmask |= 1 << var;
1622 params->rmask |= 1 << var;
1623 } else {
1624 snd_BUG();
1625 }
1626}
1627
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001628EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Takashi Iwai877211f2005-11-17 13:59:38 +01001630static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001631 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 int changed;
1634 if (hw_is_mask(var))
1635 changed = snd_mask_refine_first(hw_param_mask(params, var));
1636 else if (hw_is_interval(var))
1637 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001638 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (changed) {
1641 params->cmask |= 1 << var;
1642 params->rmask |= 1 << var;
1643 }
1644 return changed;
1645}
1646
1647
1648/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001649 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001650 * @pcm: PCM instance
1651 * @params: the hw_params instance
1652 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001653 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001655 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001657 *
1658 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001660int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1661 struct snd_pcm_hw_params *params,
1662 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 int changed = _snd_pcm_hw_param_first(params, var);
1665 if (changed < 0)
1666 return changed;
1667 if (params->rmask) {
1668 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001669 if (snd_BUG_ON(err < 0))
1670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 return snd_pcm_hw_param_value(params, var, dir);
1673}
1674
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001675EXPORT_SYMBOL(snd_pcm_hw_param_first);
1676
Takashi Iwai877211f2005-11-17 13:59:38 +01001677static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001678 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 int changed;
1681 if (hw_is_mask(var))
1682 changed = snd_mask_refine_last(hw_param_mask(params, var));
1683 else if (hw_is_interval(var))
1684 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001685 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (changed) {
1688 params->cmask |= 1 << var;
1689 params->rmask |= 1 << var;
1690 }
1691 return changed;
1692}
1693
1694
1695/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001696 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001697 * @pcm: PCM instance
1698 * @params: the hw_params instance
1699 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001700 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001702 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001704 *
1705 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001707int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1708 struct snd_pcm_hw_params *params,
1709 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 int changed = _snd_pcm_hw_param_last(params, var);
1712 if (changed < 0)
1713 return changed;
1714 if (params->rmask) {
1715 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001716 if (snd_BUG_ON(err < 0))
1717 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 return snd_pcm_hw_param_value(params, var, dir);
1720}
1721
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001722EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001725 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001726 * @pcm: PCM instance
1727 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001729 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * The configuration chosen is that obtained fixing in this order:
1731 * first access, first format, first subformat, min channels,
1732 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001733 *
1734 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001736int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1737 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001739 static int vars[] = {
1740 SNDRV_PCM_HW_PARAM_ACCESS,
1741 SNDRV_PCM_HW_PARAM_FORMAT,
1742 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1743 SNDRV_PCM_HW_PARAM_CHANNELS,
1744 SNDRV_PCM_HW_PARAM_RATE,
1745 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1746 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1747 SNDRV_PCM_HW_PARAM_TICK_TIME,
1748 -1
1749 };
1750 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001752 for (v = vars; *v != -1; v++) {
1753 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1754 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1755 else
1756 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001757 if (snd_BUG_ON(err < 0))
1758 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761}
1762
Takashi Iwai877211f2005-11-17 13:59:38 +01001763static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 void *arg)
1765{
Takashi Iwai877211f2005-11-17 13:59:38 +01001766 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 unsigned long flags;
1768 snd_pcm_stream_lock_irqsave(substream, flags);
1769 if (snd_pcm_running(substream) &&
1770 snd_pcm_update_hw_ptr(substream) >= 0)
1771 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001772 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001774 runtime->hw_ptr_wrap = 0;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 snd_pcm_stream_unlock_irqrestore(substream, flags);
1777 return 0;
1778}
1779
Takashi Iwai877211f2005-11-17 13:59:38 +01001780static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 void *arg)
1782{
Takashi Iwai877211f2005-11-17 13:59:38 +01001783 struct snd_pcm_channel_info *info = arg;
1784 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int width;
1786 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1787 info->offset = -1;
1788 return 0;
1789 }
1790 width = snd_pcm_format_physical_width(runtime->format);
1791 if (width < 0)
1792 return width;
1793 info->offset = 0;
1794 switch (runtime->access) {
1795 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1796 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001797 if ((UINT_MAX/width) < info->channel) {
1798 snd_printd("%s: integer overflow while multiply\n",
1799 __func__);
1800 return -EINVAL;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 info->first = info->channel * width;
1803 info->step = runtime->channels * width;
1804 break;
1805 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1806 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1807 {
1808 size_t size = runtime->dma_bytes / runtime->channels;
Phani Kumar Uppalapati1515ec32013-09-26 12:27:56 -07001809
1810 if ((size > 0) && ((UINT_MAX/(size * 8)) < info->channel)) {
1811 snd_printd("%s: integer overflow while multiply\n",
1812 __func__);
1813 return -EINVAL;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 info->first = info->channel * size * 8;
1816 info->step = width;
1817 break;
1818 }
1819 default:
1820 snd_BUG();
1821 break;
1822 }
1823 return 0;
1824}
1825
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001826static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1827 void *arg)
1828{
1829 struct snd_pcm_hw_params *params = arg;
1830 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001831 int channels;
1832 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001833
1834 params->fifo_size = substream->runtime->hw.fifo_size;
1835 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1836 format = params_format(params);
1837 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001838 frame_size = snd_pcm_format_size(format, channels);
1839 if (frame_size > 0)
1840 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001841 }
1842 return 0;
1843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845/**
1846 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1847 * @substream: the pcm substream instance
1848 * @cmd: ioctl command
1849 * @arg: ioctl argument
1850 *
1851 * Processes the generic ioctl commands for PCM.
1852 * Can be passed as the ioctl callback for PCM ops.
1853 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001854 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001856int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 unsigned int cmd, void *arg)
1858{
1859 switch (cmd) {
1860 case SNDRV_PCM_IOCTL1_INFO:
1861 return 0;
1862 case SNDRV_PCM_IOCTL1_RESET:
1863 return snd_pcm_lib_ioctl_reset(substream, arg);
1864 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1865 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001866 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1867 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 return -ENXIO;
1870}
1871
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001872EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/**
1875 * snd_pcm_period_elapsed - update the pcm status for the next period
1876 * @substream: the pcm substream instance
1877 *
1878 * This function is called from the interrupt handler when the
1879 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001880 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
1882 * Even if more than one periods have elapsed since the last call, you
1883 * have to call this only once.
1884 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001885void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai877211f2005-11-17 13:59:38 +01001887 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 unsigned long flags;
1889
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001890 if (PCM_RUNTIME_CHECK(substream))
1891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 snd_pcm_stream_lock_irqsave(substream, flags);
1895 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001896 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto _end;
1898
Jie Yang90bbaf62015-10-16 17:57:46 +08001899#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (substream->timer_running)
1901 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001905 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001908EXPORT_SYMBOL(snd_pcm_period_elapsed);
1909
Takashi Iwai13075512008-01-08 18:08:14 +01001910/*
1911 * Wait until avail_min data becomes available
1912 * Returns a negative error code if any error occurs during operation.
1913 * The available space is stored on availp. When err = 0 and avail = 0
1914 * on the capture stream, it indicates the stream is in DRAINING state.
1915 */
David Dillow5daeba32010-06-27 00:13:20 +02001916static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001917 snd_pcm_uframes_t *availp)
1918{
1919 struct snd_pcm_runtime *runtime = substream->runtime;
1920 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1921 wait_queue_t wait;
1922 int err = 0;
1923 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001924 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001925
Arjan van de Ven763437a2011-09-15 08:49:25 +02001926 init_waitqueue_entry(&wait, current);
1927 set_current_state(TASK_INTERRUPTIBLE);
1928 add_wait_queue(&runtime->tsleep, &wait);
1929
Takashi Iwaif2b36142011-05-26 08:09:38 +02001930 if (runtime->no_period_wakeup)
1931 wait_time = MAX_SCHEDULE_TIMEOUT;
1932 else {
1933 wait_time = 10;
1934 if (runtime->rate) {
1935 long t = runtime->period_size * 2 / runtime->rate;
1936 wait_time = max(t, wait_time);
1937 }
1938 wait_time = msecs_to_jiffies(wait_time * 1000);
1939 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001940
Takashi Iwai13075512008-01-08 18:08:14 +01001941 for (;;) {
1942 if (signal_pending(current)) {
1943 err = -ERESTARTSYS;
1944 break;
1945 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001946
1947 /*
1948 * We need to check if space became available already
1949 * (and thus the wakeup happened already) first to close
1950 * the race of space already having become available.
1951 * This check must happen after been added to the waitqueue
1952 * and having current state be INTERRUPTIBLE.
1953 */
1954 if (is_playback)
1955 avail = snd_pcm_playback_avail(runtime);
1956 else
1957 avail = snd_pcm_capture_avail(runtime);
1958 if (avail >= runtime->twake)
1959 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001960 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001961
1962 tout = schedule_timeout(wait_time);
1963
Takashi Iwai13075512008-01-08 18:08:14 +01001964 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001965 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001966 switch (runtime->status->state) {
1967 case SNDRV_PCM_STATE_SUSPENDED:
1968 err = -ESTRPIPE;
1969 goto _endloop;
1970 case SNDRV_PCM_STATE_XRUN:
1971 err = -EPIPE;
1972 goto _endloop;
1973 case SNDRV_PCM_STATE_DRAINING:
1974 if (is_playback)
1975 err = -EPIPE;
1976 else
1977 avail = 0; /* indicate draining */
1978 goto _endloop;
1979 case SNDRV_PCM_STATE_OPEN:
1980 case SNDRV_PCM_STATE_SETUP:
1981 case SNDRV_PCM_STATE_DISCONNECTED:
1982 err = -EBADFD;
1983 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001984 case SNDRV_PCM_STATE_PAUSED:
1985 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001986 }
1987 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001988 pcm_dbg(substream->pcm,
1989 "%s write error (DMA or IRQ trouble?)\n",
1990 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001991 err = -EIO;
1992 break;
1993 }
Takashi Iwai13075512008-01-08 18:08:14 +01001994 }
1995 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001996 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001997 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001998 *availp = avail;
1999 return err;
2000}
2001
Takashi Iwai877211f2005-11-17 13:59:38 +01002002static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned int hwoff,
2004 unsigned long data, unsigned int off,
2005 snd_pcm_uframes_t frames)
2006{
Takashi Iwai877211f2005-11-17 13:59:38 +01002007 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int err;
2009 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2010 if (substream->ops->copy) {
2011 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2012 return err;
2013 } else {
2014 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2016 return -EFAULT;
2017 }
2018 return 0;
2019}
2020
Takashi Iwai877211f2005-11-17 13:59:38 +01002021typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 unsigned long data, unsigned int off,
2023 snd_pcm_uframes_t size);
2024
Takashi Iwai877211f2005-11-17 13:59:38 +01002025static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 unsigned long data,
2027 snd_pcm_uframes_t size,
2028 int nonblock,
2029 transfer_f transfer)
2030{
Takashi Iwai877211f2005-11-17 13:59:38 +01002031 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 snd_pcm_uframes_t xfer = 0;
2033 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002034 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int err = 0;
2036
2037 if (size == 0)
2038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 snd_pcm_stream_lock_irq(substream);
2041 switch (runtime->status->state) {
2042 case SNDRV_PCM_STATE_PREPARED:
2043 case SNDRV_PCM_STATE_RUNNING:
2044 case SNDRV_PCM_STATE_PAUSED:
2045 break;
2046 case SNDRV_PCM_STATE_XRUN:
2047 err = -EPIPE;
2048 goto _end_unlock;
2049 case SNDRV_PCM_STATE_SUSPENDED:
2050 err = -ESTRPIPE;
2051 goto _end_unlock;
2052 default:
2053 err = -EBADFD;
2054 goto _end_unlock;
2055 }
2056
David Dillow5daeba32010-06-27 00:13:20 +02002057 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002058 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2059 snd_pcm_update_hw_ptr(substream);
2060 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 while (size > 0) {
2062 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002064 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (nonblock) {
2066 err = -EAGAIN;
2067 goto _end_unlock;
2068 }
David Dillow5daeba32010-06-27 00:13:20 +02002069 runtime->twake = min_t(snd_pcm_uframes_t, size,
2070 runtime->control->avail_min ? : 1);
2071 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002072 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 frames = size > avail ? avail : size;
2076 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2077 if (frames > cont)
2078 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002079 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002080 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002081 snd_pcm_stream_unlock_irq(substream);
2082 return -EINVAL;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 appl_ptr = runtime->control->appl_ptr;
2085 appl_ofs = appl_ptr % runtime->buffer_size;
2086 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002087 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002089 if (err < 0)
2090 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 switch (runtime->status->state) {
2092 case SNDRV_PCM_STATE_XRUN:
2093 err = -EPIPE;
2094 goto _end_unlock;
2095 case SNDRV_PCM_STATE_SUSPENDED:
2096 err = -ESTRPIPE;
2097 goto _end_unlock;
2098 default:
2099 break;
2100 }
2101 appl_ptr += frames;
2102 if (appl_ptr >= runtime->boundary)
2103 appl_ptr -= runtime->boundary;
2104 runtime->control->appl_ptr = appl_ptr;
2105 if (substream->ops->ack)
2106 substream->ops->ack(substream);
2107
2108 offset += frames;
2109 size -= frames;
2110 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002111 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2113 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2114 err = snd_pcm_start(substream);
2115 if (err < 0)
2116 goto _end_unlock;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002120 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002121 if (xfer > 0 && err >= 0)
2122 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2125}
2126
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002127/* sanity-check for read/write methods */
2128static int pcm_sanity_check(struct snd_pcm_substream *substream)
2129{
2130 struct snd_pcm_runtime *runtime;
2131 if (PCM_RUNTIME_CHECK(substream))
2132 return -ENXIO;
Liam Girdwoodf2727332016-01-29 02:27:17 +05302133 /* TODO: consider and -EINVAL here */
2134 if (substream->hw_no_buffer)
2135 snd_printd("%s: warning this PCM is host less\n", __func__);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002136 runtime = substream->runtime;
2137 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2138 return -EINVAL;
2139 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2140 return -EBADFD;
2141 return 0;
2142}
2143
Takashi Iwai877211f2005-11-17 13:59:38 +01002144snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002148 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002150 err = pcm_sanity_check(substream);
2151 if (err < 0)
2152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002154 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2157 runtime->channels > 1)
2158 return -EINVAL;
2159 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2160 snd_pcm_lib_write_transfer);
2161}
2162
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002163EXPORT_SYMBOL(snd_pcm_lib_write);
2164
Takashi Iwai877211f2005-11-17 13:59:38 +01002165static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 unsigned int hwoff,
2167 unsigned long data, unsigned int off,
2168 snd_pcm_uframes_t frames)
2169{
Takashi Iwai877211f2005-11-17 13:59:38 +01002170 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int err;
2172 void __user **bufs = (void __user **)data;
2173 int channels = runtime->channels;
2174 int c;
2175 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002176 if (snd_BUG_ON(!substream->ops->silence))
2177 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 for (c = 0; c < channels; ++c, ++bufs) {
2179 if (*bufs == NULL) {
2180 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2181 return err;
2182 } else {
2183 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2184 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2185 return err;
2186 }
2187 }
2188 } else {
2189 /* default transfer behaviour */
2190 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 for (c = 0; c < channels; ++c, ++bufs) {
2192 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2193 if (*bufs == NULL) {
2194 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2195 } else {
2196 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2197 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2198 return -EFAULT;
2199 }
2200 }
2201 }
2202 return 0;
2203}
2204
Takashi Iwai877211f2005-11-17 13:59:38 +01002205snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 void __user **bufs,
2207 snd_pcm_uframes_t frames)
2208{
Takashi Iwai877211f2005-11-17 13:59:38 +01002209 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002213 err = pcm_sanity_check(substream);
2214 if (err < 0)
2215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002217 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2220 return -EINVAL;
2221 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2222 nonblock, snd_pcm_lib_writev_transfer);
2223}
2224
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002225EXPORT_SYMBOL(snd_pcm_lib_writev);
2226
Takashi Iwai877211f2005-11-17 13:59:38 +01002227static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 unsigned int hwoff,
2229 unsigned long data, unsigned int off,
2230 snd_pcm_uframes_t frames)
2231{
Takashi Iwai877211f2005-11-17 13:59:38 +01002232 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int err;
2234 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2235 if (substream->ops->copy) {
2236 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2237 return err;
2238 } else {
2239 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2241 return -EFAULT;
2242 }
2243 return 0;
2244}
2245
Takashi Iwai877211f2005-11-17 13:59:38 +01002246static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned long data,
2248 snd_pcm_uframes_t size,
2249 int nonblock,
2250 transfer_f transfer)
2251{
Takashi Iwai877211f2005-11-17 13:59:38 +01002252 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 snd_pcm_uframes_t xfer = 0;
2254 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002255 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int err = 0;
2257
2258 if (size == 0)
2259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 snd_pcm_stream_lock_irq(substream);
2262 switch (runtime->status->state) {
2263 case SNDRV_PCM_STATE_PREPARED:
2264 if (size >= runtime->start_threshold) {
2265 err = snd_pcm_start(substream);
2266 if (err < 0)
2267 goto _end_unlock;
2268 }
2269 break;
2270 case SNDRV_PCM_STATE_DRAINING:
2271 case SNDRV_PCM_STATE_RUNNING:
2272 case SNDRV_PCM_STATE_PAUSED:
2273 break;
2274 case SNDRV_PCM_STATE_XRUN:
2275 err = -EPIPE;
2276 goto _end_unlock;
2277 case SNDRV_PCM_STATE_SUSPENDED:
2278 err = -ESTRPIPE;
2279 goto _end_unlock;
2280 default:
2281 err = -EBADFD;
2282 goto _end_unlock;
2283 }
2284
David Dillow5daeba32010-06-27 00:13:20 +02002285 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002286 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2287 snd_pcm_update_hw_ptr(substream);
2288 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 while (size > 0) {
2290 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002292 if (!avail) {
2293 if (runtime->status->state ==
2294 SNDRV_PCM_STATE_DRAINING) {
2295 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto _end_unlock;
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (nonblock) {
2299 err = -EAGAIN;
2300 goto _end_unlock;
2301 }
David Dillow5daeba32010-06-27 00:13:20 +02002302 runtime->twake = min_t(snd_pcm_uframes_t, size,
2303 runtime->control->avail_min ? : 1);
2304 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002305 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002307 if (!avail)
2308 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 frames = size > avail ? avail : size;
2311 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2312 if (frames > cont)
2313 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002314 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002315 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002316 snd_pcm_stream_unlock_irq(substream);
2317 return -EINVAL;
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 appl_ptr = runtime->control->appl_ptr;
2320 appl_ofs = appl_ptr % runtime->buffer_size;
2321 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002322 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002324 if (err < 0)
2325 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 switch (runtime->status->state) {
2327 case SNDRV_PCM_STATE_XRUN:
2328 err = -EPIPE;
2329 goto _end_unlock;
2330 case SNDRV_PCM_STATE_SUSPENDED:
2331 err = -ESTRPIPE;
2332 goto _end_unlock;
2333 default:
2334 break;
2335 }
2336 appl_ptr += frames;
2337 if (appl_ptr >= runtime->boundary)
2338 appl_ptr -= runtime->boundary;
2339 runtime->control->appl_ptr = appl_ptr;
2340 if (substream->ops->ack)
2341 substream->ops->ack(substream);
2342
2343 offset += frames;
2344 size -= frames;
2345 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002346 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002349 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002350 if (xfer > 0 && err >= 0)
2351 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2354}
2355
Takashi Iwai877211f2005-11-17 13:59:38 +01002356snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Takashi Iwai877211f2005-11-17 13:59:38 +01002358 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002362 err = pcm_sanity_check(substream);
2363 if (err < 0)
2364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002366 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2368 return -EINVAL;
2369 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2370}
2371
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002372EXPORT_SYMBOL(snd_pcm_lib_read);
2373
Takashi Iwai877211f2005-11-17 13:59:38 +01002374static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 unsigned int hwoff,
2376 unsigned long data, unsigned int off,
2377 snd_pcm_uframes_t frames)
2378{
Takashi Iwai877211f2005-11-17 13:59:38 +01002379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int err;
2381 void __user **bufs = (void __user **)data;
2382 int channels = runtime->channels;
2383 int c;
2384 if (substream->ops->copy) {
2385 for (c = 0; c < channels; ++c, ++bufs) {
2386 char __user *buf;
2387 if (*bufs == NULL)
2388 continue;
2389 buf = *bufs + samples_to_bytes(runtime, off);
2390 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2391 return err;
2392 }
2393 } else {
2394 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 for (c = 0; c < channels; ++c, ++bufs) {
2396 char *hwbuf;
2397 char __user *buf;
2398 if (*bufs == NULL)
2399 continue;
2400
2401 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2402 buf = *bufs + samples_to_bytes(runtime, off);
2403 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2404 return -EFAULT;
2405 }
2406 }
2407 return 0;
2408}
2409
Takashi Iwai877211f2005-11-17 13:59:38 +01002410snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 void __user **bufs,
2412 snd_pcm_uframes_t frames)
2413{
Takashi Iwai877211f2005-11-17 13:59:38 +01002414 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002418 err = pcm_sanity_check(substream);
2419 if (err < 0)
2420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2423 return -EBADFD;
2424
Takashi Iwai0df63e42006-04-28 15:13:41 +02002425 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2427 return -EINVAL;
2428 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002432
2433/*
2434 * standard channel mapping helpers
2435 */
2436
2437/* default channel maps for multi-channel playbacks, up to 8 channels */
2438const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2439 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002440 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002441 { .channels = 2,
2442 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2443 { .channels = 4,
2444 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2445 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2446 { .channels = 6,
2447 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2448 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2449 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2450 { .channels = 8,
2451 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2452 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2453 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2454 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2455 { }
2456};
2457EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2458
2459/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2460const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2461 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002462 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002463 { .channels = 2,
2464 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2465 { .channels = 4,
2466 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2467 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2468 { .channels = 6,
2469 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2470 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2471 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2472 { .channels = 8,
2473 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2474 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2475 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2476 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2477 { }
2478};
2479EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2480
2481static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2482{
2483 if (ch > info->max_channels)
2484 return false;
2485 return !info->channel_mask || (info->channel_mask & (1U << ch));
2486}
2487
2488static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_info *uinfo)
2490{
2491 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2492
2493 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2494 uinfo->count = 0;
2495 uinfo->count = info->max_channels;
2496 uinfo->value.integer.min = 0;
2497 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2498 return 0;
2499}
2500
2501/* get callback for channel map ctl element
2502 * stores the channel position firstly matching with the current channels
2503 */
2504static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_value *ucontrol)
2506{
2507 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2508 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2509 struct snd_pcm_substream *substream;
2510 const struct snd_pcm_chmap_elem *map;
2511
2512 if (snd_BUG_ON(!info->chmap))
2513 return -EINVAL;
2514 substream = snd_pcm_chmap_substream(info, idx);
2515 if (!substream)
2516 return -ENODEV;
2517 memset(ucontrol->value.integer.value, 0,
2518 sizeof(ucontrol->value.integer.value));
2519 if (!substream->runtime)
2520 return 0; /* no channels set */
2521 for (map = info->chmap; map->channels; map++) {
2522 int i;
2523 if (map->channels == substream->runtime->channels &&
2524 valid_chmap_channels(info, map->channels)) {
2525 for (i = 0; i < map->channels; i++)
2526 ucontrol->value.integer.value[i] = map->map[i];
2527 return 0;
2528 }
2529 }
2530 return -EINVAL;
2531}
2532
2533/* tlv callback for channel map ctl element
2534 * expands the pre-defined channel maps in a form of TLV
2535 */
2536static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2537 unsigned int size, unsigned int __user *tlv)
2538{
2539 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2540 const struct snd_pcm_chmap_elem *map;
2541 unsigned int __user *dst;
2542 int c, count = 0;
2543
2544 if (snd_BUG_ON(!info->chmap))
2545 return -EINVAL;
2546 if (size < 8)
2547 return -ENOMEM;
2548 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2549 return -EFAULT;
2550 size -= 8;
2551 dst = tlv + 2;
2552 for (map = info->chmap; map->channels; map++) {
2553 int chs_bytes = map->channels * 4;
2554 if (!valid_chmap_channels(info, map->channels))
2555 continue;
2556 if (size < 8)
2557 return -ENOMEM;
2558 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2559 put_user(chs_bytes, dst + 1))
2560 return -EFAULT;
2561 dst += 2;
2562 size -= 8;
2563 count += 8;
2564 if (size < chs_bytes)
2565 return -ENOMEM;
2566 size -= chs_bytes;
2567 count += chs_bytes;
2568 for (c = 0; c < map->channels; c++) {
2569 if (put_user(map->map[c], dst))
2570 return -EFAULT;
2571 dst++;
2572 }
2573 }
2574 if (put_user(count, tlv + 1))
2575 return -EFAULT;
2576 return 0;
2577}
2578
2579static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2580{
2581 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2582 info->pcm->streams[info->stream].chmap_kctl = NULL;
2583 kfree(info);
2584}
2585
Banajit Goswami55b24c52016-11-21 19:08:27 -08002586static int pcm_volume_ctl_info(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_info *uinfo)
2588{
2589 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2590 uinfo->count = 1;
2591 uinfo->value.integer.min = 0;
2592 uinfo->value.integer.max = 0x2000;
2593 return 0;
2594}
2595
2596static void pcm_volume_ctl_private_free(struct snd_kcontrol *kcontrol)
2597{
2598 struct snd_pcm_volume *info = snd_kcontrol_chip(kcontrol);
2599
2600 info->pcm->streams[info->stream].vol_kctl = NULL;
2601 kfree(info);
2602}
2603
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002604/**
2605 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2606 * @pcm: the assigned PCM instance
2607 * @stream: stream direction
2608 * @chmap: channel map elements (for query)
2609 * @max_channels: the max number of channels for the stream
2610 * @private_value: the value passed to each kcontrol's private_value field
2611 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2612 *
2613 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002614 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002615 */
2616int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2617 const struct snd_pcm_chmap_elem *chmap,
2618 int max_channels,
2619 unsigned long private_value,
2620 struct snd_pcm_chmap **info_ret)
2621{
2622 struct snd_pcm_chmap *info;
2623 struct snd_kcontrol_new knew = {
2624 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2625 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002626 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2627 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2628 .info = pcm_chmap_ctl_info,
2629 .get = pcm_chmap_ctl_get,
2630 .tlv.c = pcm_chmap_ctl_tlv,
2631 };
2632 int err;
2633
Takashi Iwai8d879be2016-05-10 16:07:40 +02002634 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2635 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002636 info = kzalloc(sizeof(*info), GFP_KERNEL);
2637 if (!info)
2638 return -ENOMEM;
2639 info->pcm = pcm;
2640 info->stream = stream;
2641 info->chmap = chmap;
2642 info->max_channels = max_channels;
2643 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2644 knew.name = "Playback Channel Map";
2645 else
2646 knew.name = "Capture Channel Map";
2647 knew.device = pcm->device;
2648 knew.count = pcm->streams[stream].substream_count;
2649 knew.private_value = private_value;
2650 info->kctl = snd_ctl_new1(&knew, info);
2651 if (!info->kctl) {
2652 kfree(info);
2653 return -ENOMEM;
2654 }
2655 info->kctl->private_free = pcm_chmap_ctl_private_free;
2656 err = snd_ctl_add(pcm->card, info->kctl);
2657 if (err < 0)
2658 return err;
2659 pcm->streams[stream].chmap_kctl = info->kctl;
2660 if (info_ret)
2661 *info_ret = info;
2662 return 0;
2663}
2664EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);
Banajit Goswami55b24c52016-11-21 19:08:27 -08002665
2666/**
2667 * snd_pcm_add_volume_ctls - create volume control elements
2668 * @pcm: the assigned PCM instance
2669 * @stream: stream direction
2670 * @max_length: the max length of the volume parameter of stream
2671 * @private_value: the value passed to each kcontrol's private_value field
2672 * @info_ret: store struct snd_pcm_volume instance if non-NULL
2673 *
2674 * Create volume control elements assigned to the given PCM stream(s).
2675 * Returns zero if succeed, or a negative error value.
2676 */
2677int snd_pcm_add_volume_ctls(struct snd_pcm *pcm, int stream,
2678 const struct snd_pcm_volume_elem *volume,
2679 int max_length,
2680 unsigned long private_value,
2681 struct snd_pcm_volume **info_ret)
2682{
2683 struct snd_pcm_volume *info;
2684 struct snd_kcontrol_new knew = {
2685 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2686 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2687 SNDRV_CTL_ELEM_ACCESS_READWRITE,
2688 .info = pcm_volume_ctl_info,
2689 };
2690 int err;
2691 int size;
2692
2693 info = kzalloc(sizeof(*info), GFP_KERNEL);
2694 if (!info)
2695 return -ENOMEM;
2696 info->pcm = pcm;
2697 info->stream = stream;
2698 info->volume = volume;
2699 info->max_length = max_length;
2700 size = sizeof("Playback ") + sizeof(" Volume") +
2701 STRING_LENGTH_OF_INT*sizeof(char) + 1;
2702 knew.name = kzalloc(size, GFP_KERNEL);
2703 if (!knew.name) {
2704 kfree(info);
2705 return -ENOMEM;
2706 }
2707 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2708 snprintf((char *)knew.name, size, "%s %d %s",
2709 "Playback", pcm->device, "Volume");
2710 else
2711 snprintf((char *)knew.name, size, "%s %d %s",
2712 "Capture", pcm->device, "Volume");
2713 knew.device = pcm->device;
2714 knew.count = pcm->streams[stream].substream_count;
2715 knew.private_value = private_value;
2716 info->kctl = snd_ctl_new1(&knew, info);
2717 if (!info->kctl) {
2718 kfree(info);
2719 kfree(knew.name);
2720 return -ENOMEM;
2721 }
2722 info->kctl->private_free = pcm_volume_ctl_private_free;
2723 err = snd_ctl_add(pcm->card, info->kctl);
2724 if (err < 0) {
2725 kfree(info);
2726 kfree(knew.name);
2727 return -ENOMEM;
2728 }
2729 pcm->streams[stream].vol_kctl = info->kctl;
2730 if (info_ret)
2731 *info_ret = info;
2732 kfree(knew.name);
2733 return 0;
2734}
2735EXPORT_SYMBOL(snd_pcm_add_volume_ctls);
Banajit Goswamicec03b62016-11-21 19:22:56 -08002736
2737static int pcm_usr_ctl_info(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_info *uinfo)
2739{
2740 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2741 uinfo->count = MAX_USR_CTRL_CNT;
2742 uinfo->value.integer.min = 0;
2743 uinfo->value.integer.max = INT_MAX;
2744 return 0;
2745}
2746
2747static void pcm_usr_ctl_private_free(struct snd_kcontrol *kcontrol)
2748{
2749 struct snd_pcm_usr *info = snd_kcontrol_chip(kcontrol);
2750
2751 info->pcm->streams[info->stream].usr_kctl = NULL;
2752 kfree(info);
2753}
2754
2755/**
2756 * snd_pcm_add_usr_ctls - create user control elements
2757 * @pcm: the assigned PCM instance
2758 * @stream: stream direction
2759 * @max_length: the max length of the user parameter of stream
2760 * @private_value: the value passed to each kcontrol's private_value field
2761 * @info_ret: store struct snd_pcm_usr instance if non-NULL
2762 *
2763 * Create usr control elements assigned to the given PCM stream(s).
2764 * Returns zero if succeed, or a negative error value.
2765 */
2766int snd_pcm_add_usr_ctls(struct snd_pcm *pcm, int stream,
2767 const struct snd_pcm_usr_elem *usr,
2768 int max_length, int max_kctrl_str_len,
2769 unsigned long private_value,
2770 struct snd_pcm_usr **info_ret)
2771{
2772 struct snd_pcm_usr *info;
2773 struct snd_kcontrol_new knew = {
2774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2775 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2776 .info = pcm_usr_ctl_info,
2777 };
2778 int err;
2779 char *buf;
2780
2781 info = kzalloc(sizeof(*info), GFP_KERNEL);
2782 if (!info)
2783 return -ENOMEM;
2784
2785 info->pcm = pcm;
2786 info->stream = stream;
2787 info->usr = usr;
2788 info->max_length = max_length;
2789 buf = kzalloc(max_kctrl_str_len, GFP_KERNEL);
2790 if (!buf) {
2791 pr_err("%s: buffer allocation failed\n", __func__);
2792 kfree(info);
2793 return -ENOMEM;
2794 }
2795 knew.name = buf;
2796 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2797 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2798 "Playback", pcm->device, "User kcontrol");
2799 else
2800 snprintf(buf, max_kctrl_str_len, "%s %d %s",
2801 "Capture", pcm->device, "User kcontrol");
2802 knew.device = pcm->device;
2803 knew.count = pcm->streams[stream].substream_count;
2804 knew.private_value = private_value;
2805 info->kctl = snd_ctl_new1(&knew, info);
2806 if (!info->kctl) {
2807 kfree(info);
2808 kfree(knew.name);
2809 pr_err("%s: snd_ctl_new failed\n", __func__);
2810 return -ENOMEM;
2811 }
2812 info->kctl->private_free = pcm_usr_ctl_private_free;
2813 err = snd_ctl_add(pcm->card, info->kctl);
2814 if (err < 0) {
2815 kfree(info);
2816 kfree(knew.name);
2817 pr_err("%s: snd_ctl_add failed:%d\n", __func__,
2818 err);
2819 return -ENOMEM;
2820 }
2821 pcm->streams[stream].usr_kctl = info->kctl;
2822 if (info_ret)
2823 *info_ret = info;
2824 kfree(knew.name);
2825 return 0;
2826}
2827EXPORT_SYMBOL(snd_pcm_add_usr_ctls);