blob: e50548af400461b1b8d132eacc749d5d83f37f12 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/time.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020026#include <linux/math64.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090036#include "pcm_local.h"
37
Takashi Iwaif5914902014-11-04 12:45:59 +010038#ifdef CONFIG_SND_PCM_XRUN_DEBUG
39#define CREATE_TRACE_POINTS
40#include "pcm_trace.h"
41#else
42#define trace_hwptr(substream, pos, in_interrupt)
43#define trace_xrun(substream)
44#define trace_hw_ptr_error(substream, reason)
45#endif
46
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;
Clemens Ladisch47228e42010-11-18 09:53:07 +0100378 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
379 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100380 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200381 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
382 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100383 delta += runtime->buffer_size;
384 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500385 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100386 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500387 crossed_boundary++;
388 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100389 new_hw_ptr = hw_base + pos;
390 hdelta -= runtime->hw_ptr_buffer_jiffies;
391 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100392 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100393 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100394
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100395 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100396 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100397 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
398 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
399 substream->stream, (long)pos,
400 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100401 return 0;
402 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200403
404 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100405 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200406 goto no_jiffies_check;
407
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200408 /* Skip the jiffies check for hardwares with BATCH flag.
409 * Such hardware usually just increases the position at each IRQ,
410 * thus it can't give any strange position.
411 */
412 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
413 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100414 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200415 if (hdelta < runtime->delay)
416 goto no_jiffies_check;
417 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500418 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200419 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
420 delta = jdelta /
421 (((runtime->period_size * HZ) / runtime->rate)
422 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100423 /* move new_hw_ptr according jiffies not pos variable */
424 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100425 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100426 /* use loop to avoid checks for delta overflows */
427 /* the delta value is small or zero in most cases */
428 while (delta > 0) {
429 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500430 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100431 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500432 crossed_boundary--;
433 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100434 delta--;
435 }
436 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100437 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
438 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200439 (long)pos, (long)hdelta,
440 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100441 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100442 (unsigned long)old_hw_ptr,
443 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100444 /* reset values to proper state */
445 delta = 0;
446 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200447 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200448 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200449 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100450 hw_ptr_error(substream, in_interrupt,
451 "Lost interrupts?",
452 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100453 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100454 (long)new_hw_ptr,
455 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100456 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100457
Clemens Ladischab69a492010-11-15 10:46:23 +0100458 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600459 if (runtime->status->hw_ptr == new_hw_ptr) {
460 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100461 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600462 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
465 runtime->silence_size > 0)
466 snd_pcm_playback_silence(substream, new_hw_ptr);
467
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100468 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200469 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
470 if (delta < 0)
471 delta += runtime->boundary;
472 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
473 runtime->hw_ptr_interrupt += delta;
474 if (runtime->hw_ptr_interrupt >= runtime->boundary)
475 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100476 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100477 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500479 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500480 if (crossed_boundary) {
481 snd_BUG_ON(crossed_boundary != 1);
482 runtime->hw_ptr_wrap += runtime->boundary;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600485 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500486
Jaroslav Kysela12509322010-01-07 15:36:31 +0100487 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100491int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100493 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/**
497 * snd_pcm_set_ops - set the PCM operators
498 * @pcm: the pcm instance
499 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
500 * @ops: the operator table
501 *
502 * Sets the given PCM operators to the pcm instance.
503 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200504void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
505 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Takashi Iwai877211f2005-11-17 13:59:38 +0100507 struct snd_pcm_str *stream = &pcm->streams[direction];
508 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 for (substream = stream->substream; substream != NULL; substream = substream->next)
511 substream->ops = ops;
512}
513
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200514EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516/**
517 * snd_pcm_sync - set the PCM sync id
518 * @substream: the pcm substream
519 *
520 * Sets the PCM sync identifier for the card.
521 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100522void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Takashi Iwai877211f2005-11-17 13:59:38 +0100524 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 runtime->sync.id32[0] = substream->pcm->card->number;
527 runtime->sync.id32[1] = -1;
528 runtime->sync.id32[2] = -1;
529 runtime->sync.id32[3] = -1;
530}
531
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200532EXPORT_SYMBOL(snd_pcm_set_sync);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
535 * Standard ioctl routine
536 */
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static inline unsigned int div32(unsigned int a, unsigned int b,
539 unsigned int *r)
540{
541 if (b == 0) {
542 *r = 0;
543 return UINT_MAX;
544 }
545 *r = a % b;
546 return a / b;
547}
548
549static inline unsigned int div_down(unsigned int a, unsigned int b)
550{
551 if (b == 0)
552 return UINT_MAX;
553 return a / b;
554}
555
556static inline unsigned int div_up(unsigned int a, unsigned int b)
557{
558 unsigned int r;
559 unsigned int q;
560 if (b == 0)
561 return UINT_MAX;
562 q = div32(a, b, &r);
563 if (r)
564 ++q;
565 return q;
566}
567
568static inline unsigned int mul(unsigned int a, unsigned int b)
569{
570 if (a == 0)
571 return 0;
572 if (div_down(UINT_MAX, a) < b)
573 return UINT_MAX;
574 return a * b;
575}
576
577static inline unsigned int muldiv32(unsigned int a, unsigned int b,
578 unsigned int c, unsigned int *r)
579{
580 u_int64_t n = (u_int64_t) a * b;
581 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200582 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *r = 0;
584 return UINT_MAX;
585 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200586 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (n >= UINT_MAX) {
588 *r = 0;
589 return UINT_MAX;
590 }
591 return n;
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/**
595 * snd_interval_refine - refine the interval value of configurator
596 * @i: the interval value to refine
597 * @v: the interval value to refer to
598 *
599 * Refines the interval value with the reference value.
600 * The interval is changed to the range satisfying both intervals.
601 * The interval status (min, max, integer, etc.) are evaluated.
602 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100603 * Return: Positive if the value is changed, zero if it's not changed, or a
604 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100606int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200609 if (snd_BUG_ON(snd_interval_empty(i)))
610 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (i->min < v->min) {
612 i->min = v->min;
613 i->openmin = v->openmin;
614 changed = 1;
615 } else if (i->min == v->min && !i->openmin && v->openmin) {
616 i->openmin = 1;
617 changed = 1;
618 }
619 if (i->max > v->max) {
620 i->max = v->max;
621 i->openmax = v->openmax;
622 changed = 1;
623 } else if (i->max == v->max && !i->openmax && v->openmax) {
624 i->openmax = 1;
625 changed = 1;
626 }
627 if (!i->integer && v->integer) {
628 i->integer = 1;
629 changed = 1;
630 }
631 if (i->integer) {
632 if (i->openmin) {
633 i->min++;
634 i->openmin = 0;
635 }
636 if (i->openmax) {
637 i->max--;
638 i->openmax = 0;
639 }
640 } else if (!i->openmin && !i->openmax && i->min == i->max)
641 i->integer = 1;
642 if (snd_interval_checkempty(i)) {
643 snd_interval_none(i);
644 return -EINVAL;
645 }
646 return changed;
647}
648
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200649EXPORT_SYMBOL(snd_interval_refine);
650
Takashi Iwai877211f2005-11-17 13:59:38 +0100651static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200653 if (snd_BUG_ON(snd_interval_empty(i)))
654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (snd_interval_single(i))
656 return 0;
657 i->max = i->min;
658 i->openmax = i->openmin;
659 if (i->openmax)
660 i->max++;
661 return 1;
662}
663
Takashi Iwai877211f2005-11-17 13:59:38 +0100664static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200666 if (snd_BUG_ON(snd_interval_empty(i)))
667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (snd_interval_single(i))
669 return 0;
670 i->min = i->max;
671 i->openmin = i->openmax;
672 if (i->openmin)
673 i->min--;
674 return 1;
675}
676
Takashi Iwai877211f2005-11-17 13:59:38 +0100677void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 if (a->empty || b->empty) {
680 snd_interval_none(c);
681 return;
682 }
683 c->empty = 0;
684 c->min = mul(a->min, b->min);
685 c->openmin = (a->openmin || b->openmin);
686 c->max = mul(a->max, b->max);
687 c->openmax = (a->openmax || b->openmax);
688 c->integer = (a->integer && b->integer);
689}
690
691/**
692 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200693 * @a: dividend
694 * @b: divisor
695 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
697 * c = a / b
698 *
699 * Returns non-zero if the value is changed, zero if not changed.
700 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100701void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 unsigned int r;
704 if (a->empty || b->empty) {
705 snd_interval_none(c);
706 return;
707 }
708 c->empty = 0;
709 c->min = div32(a->min, b->max, &r);
710 c->openmin = (r || a->openmin || b->openmax);
711 if (b->min > 0) {
712 c->max = div32(a->max, b->min, &r);
713 if (r) {
714 c->max++;
715 c->openmax = 1;
716 } else
717 c->openmax = (a->openmax || b->openmin);
718 } else {
719 c->max = UINT_MAX;
720 c->openmax = 0;
721 }
722 c->integer = 0;
723}
724
725/**
726 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200727 * @a: dividend 1
728 * @b: dividend 2
729 * @k: divisor (as integer)
730 * @c: result
731 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * c = a * b / k
733 *
734 * Returns non-zero if the value is changed, zero if not changed.
735 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100736void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
737 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 unsigned int r;
740 if (a->empty || b->empty) {
741 snd_interval_none(c);
742 return;
743 }
744 c->empty = 0;
745 c->min = muldiv32(a->min, b->min, k, &r);
746 c->openmin = (r || a->openmin || b->openmin);
747 c->max = muldiv32(a->max, b->max, k, &r);
748 if (r) {
749 c->max++;
750 c->openmax = 1;
751 } else
752 c->openmax = (a->openmax || b->openmax);
753 c->integer = 0;
754}
755
756/**
757 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200758 * @a: dividend 1
759 * @k: dividend 2 (as integer)
760 * @b: divisor
761 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
763 * c = a * k / b
764 *
765 * Returns non-zero if the value is changed, zero if not changed.
766 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100767void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
768 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 unsigned int r;
771 if (a->empty || b->empty) {
772 snd_interval_none(c);
773 return;
774 }
775 c->empty = 0;
776 c->min = muldiv32(a->min, k, b->max, &r);
777 c->openmin = (r || a->openmin || b->openmax);
778 if (b->min > 0) {
779 c->max = muldiv32(a->max, k, b->min, &r);
780 if (r) {
781 c->max++;
782 c->openmax = 1;
783 } else
784 c->openmax = (a->openmax || b->openmin);
785 } else {
786 c->max = UINT_MAX;
787 c->openmax = 0;
788 }
789 c->integer = 0;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/* ---- */
793
794
795/**
796 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200797 * @i: interval to refine
798 * @rats_count: number of ratnum_t
799 * @rats: ratnum_t array
800 * @nump: pointer to store the resultant numerator
801 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100803 * Return: Positive if the value is changed, zero if it's not changed, or a
804 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100806int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100807 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100808 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100810 unsigned int best_num, best_den;
811 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100813 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100815 unsigned int result_num, result_den;
816 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 best_num = best_den = best_diff = 0;
819 for (k = 0; k < rats_count; ++k) {
820 unsigned int num = rats[k].num;
821 unsigned int den;
822 unsigned int q = i->min;
823 int diff;
824 if (q == 0)
825 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100826 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (den < rats[k].den_min)
828 continue;
829 if (den > rats[k].den_max)
830 den = rats[k].den_max;
831 else {
832 unsigned int r;
833 r = (den - rats[k].den_min) % rats[k].den_step;
834 if (r != 0)
835 den -= r;
836 }
837 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100838 if (diff < 0)
839 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (best_num == 0 ||
841 diff * best_den < best_diff * den) {
842 best_diff = diff;
843 best_den = den;
844 best_num = num;
845 }
846 }
847 if (best_den == 0) {
848 i->empty = 1;
849 return -EINVAL;
850 }
851 t.min = div_down(best_num, best_den);
852 t.openmin = !!(best_num % best_den);
853
Krzysztof Helt8374e242009-12-21 17:07:08 +0100854 result_num = best_num;
855 result_diff = best_diff;
856 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 best_num = best_den = best_diff = 0;
858 for (k = 0; k < rats_count; ++k) {
859 unsigned int num = rats[k].num;
860 unsigned int den;
861 unsigned int q = i->max;
862 int diff;
863 if (q == 0) {
864 i->empty = 1;
865 return -EINVAL;
866 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100867 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (den > rats[k].den_max)
869 continue;
870 if (den < rats[k].den_min)
871 den = rats[k].den_min;
872 else {
873 unsigned int r;
874 r = (den - rats[k].den_min) % rats[k].den_step;
875 if (r != 0)
876 den += rats[k].den_step - r;
877 }
878 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100879 if (diff < 0)
880 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (best_num == 0 ||
882 diff * best_den < best_diff * den) {
883 best_diff = diff;
884 best_den = den;
885 best_num = num;
886 }
887 }
888 if (best_den == 0) {
889 i->empty = 1;
890 return -EINVAL;
891 }
892 t.max = div_up(best_num, best_den);
893 t.openmax = !!(best_num % best_den);
894 t.integer = 0;
895 err = snd_interval_refine(i, &t);
896 if (err < 0)
897 return err;
898
899 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100900 if (best_diff * result_den < result_diff * best_den) {
901 result_num = best_num;
902 result_den = best_den;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100905 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100907 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 return err;
910}
911
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200912EXPORT_SYMBOL(snd_interval_ratnum);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914/**
915 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200916 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100917 * @rats_count: number of struct ratden
918 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200919 * @nump: pointer to store the resultant numerator
920 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100922 * Return: Positive if the value is changed, zero if it's not changed, or a
923 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100925static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100926 unsigned int rats_count,
927 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned int *nump, unsigned int *denp)
929{
930 unsigned int best_num, best_diff, best_den;
931 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100932 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int err;
934
935 best_num = best_den = best_diff = 0;
936 for (k = 0; k < rats_count; ++k) {
937 unsigned int num;
938 unsigned int den = rats[k].den;
939 unsigned int q = i->min;
940 int diff;
941 num = mul(q, den);
942 if (num > rats[k].num_max)
943 continue;
944 if (num < rats[k].num_min)
945 num = rats[k].num_max;
946 else {
947 unsigned int r;
948 r = (num - rats[k].num_min) % rats[k].num_step;
949 if (r != 0)
950 num += rats[k].num_step - r;
951 }
952 diff = num - q * den;
953 if (best_num == 0 ||
954 diff * best_den < best_diff * den) {
955 best_diff = diff;
956 best_den = den;
957 best_num = num;
958 }
959 }
960 if (best_den == 0) {
961 i->empty = 1;
962 return -EINVAL;
963 }
964 t.min = div_down(best_num, best_den);
965 t.openmin = !!(best_num % best_den);
966
967 best_num = best_den = best_diff = 0;
968 for (k = 0; k < rats_count; ++k) {
969 unsigned int num;
970 unsigned int den = rats[k].den;
971 unsigned int q = i->max;
972 int diff;
973 num = mul(q, den);
974 if (num < rats[k].num_min)
975 continue;
976 if (num > rats[k].num_max)
977 num = rats[k].num_max;
978 else {
979 unsigned int r;
980 r = (num - rats[k].num_min) % rats[k].num_step;
981 if (r != 0)
982 num -= r;
983 }
984 diff = q * den - num;
985 if (best_num == 0 ||
986 diff * best_den < best_diff * den) {
987 best_diff = diff;
988 best_den = den;
989 best_num = num;
990 }
991 }
992 if (best_den == 0) {
993 i->empty = 1;
994 return -EINVAL;
995 }
996 t.max = div_up(best_num, best_den);
997 t.openmax = !!(best_num % best_den);
998 t.integer = 0;
999 err = snd_interval_refine(i, &t);
1000 if (err < 0)
1001 return err;
1002
1003 if (snd_interval_single(i)) {
1004 if (nump)
1005 *nump = best_num;
1006 if (denp)
1007 *denp = best_den;
1008 }
1009 return err;
1010}
1011
1012/**
1013 * snd_interval_list - refine the interval value from the list
1014 * @i: the interval value to refine
1015 * @count: the number of elements in the list
1016 * @list: the value list
1017 * @mask: the bit-mask to evaluate
1018 *
1019 * Refines the interval value from the list.
1020 * When mask is non-zero, only the elements corresponding to bit 1 are
1021 * evaluated.
1022 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001023 * Return: Positive if the value is changed, zero if it's not changed, or a
1024 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Mark Brown4af87a92012-03-14 19:48:43 +00001026int snd_interval_list(struct snd_interval *i, unsigned int count,
1027 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001030 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001031
1032 if (!count) {
1033 i->empty = 1;
1034 return -EINVAL;
1035 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001036 snd_interval_any(&list_range);
1037 list_range.min = UINT_MAX;
1038 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 for (k = 0; k < count; k++) {
1040 if (mask && !(mask & (1 << k)))
1041 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001042 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001044 list_range.min = min(list_range.min, list[k]);
1045 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001047 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001050EXPORT_SYMBOL(snd_interval_list);
1051
Peter Rosinf66f8982015-01-28 15:16:06 +01001052/**
1053 * snd_interval_ranges - refine the interval value from the list of ranges
1054 * @i: the interval value to refine
1055 * @count: the number of elements in the list of ranges
1056 * @ranges: the ranges list
1057 * @mask: the bit-mask to evaluate
1058 *
1059 * Refines the interval value from the list of ranges.
1060 * When mask is non-zero, only the elements corresponding to bit 1 are
1061 * evaluated.
1062 *
1063 * Return: Positive if the value is changed, zero if it's not changed, or a
1064 * negative error code.
1065 */
1066int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1067 const struct snd_interval *ranges, unsigned int mask)
1068{
1069 unsigned int k;
1070 struct snd_interval range_union;
1071 struct snd_interval range;
1072
1073 if (!count) {
1074 snd_interval_none(i);
1075 return -EINVAL;
1076 }
1077 snd_interval_any(&range_union);
1078 range_union.min = UINT_MAX;
1079 range_union.max = 0;
1080 for (k = 0; k < count; k++) {
1081 if (mask && !(mask & (1 << k)))
1082 continue;
1083 snd_interval_copy(&range, &ranges[k]);
1084 if (snd_interval_refine(&range, i) < 0)
1085 continue;
1086 if (snd_interval_empty(&range))
1087 continue;
1088
1089 if (range.min < range_union.min) {
1090 range_union.min = range.min;
1091 range_union.openmin = 1;
1092 }
1093 if (range.min == range_union.min && !range.openmin)
1094 range_union.openmin = 0;
1095 if (range.max > range_union.max) {
1096 range_union.max = range.max;
1097 range_union.openmax = 1;
1098 }
1099 if (range.max == range_union.max && !range.openmax)
1100 range_union.openmax = 0;
1101 }
1102 return snd_interval_refine(i, &range_union);
1103}
1104EXPORT_SYMBOL(snd_interval_ranges);
1105
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001106static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 unsigned int n;
1109 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001110 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (n != 0 || i->openmin) {
1112 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001113 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 changed = 1;
1115 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001116 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (n != 0 || i->openmax) {
1118 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001119 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 changed = 1;
1121 }
1122 if (snd_interval_checkempty(i)) {
1123 i->empty = 1;
1124 return -EINVAL;
1125 }
1126 return changed;
1127}
1128
1129/* Info constraints helpers */
1130
1131/**
1132 * snd_pcm_hw_rule_add - add the hw-constraint rule
1133 * @runtime: the pcm runtime instance
1134 * @cond: condition bits
1135 * @var: the variable to evaluate
1136 * @func: the evaluation function
1137 * @private: the private data pointer passed to function
1138 * @dep: the dependent variables
1139 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001140 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001142int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int var,
1144 snd_pcm_hw_rule_func_t func, void *private,
1145 int dep, ...)
1146{
Takashi Iwai877211f2005-11-17 13:59:38 +01001147 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1148 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 unsigned int k;
1150 va_list args;
1151 va_start(args, dep);
1152 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001153 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 unsigned int new_rules = constrs->rules_all + 16;
1155 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001156 if (!new) {
1157 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (constrs->rules) {
1161 memcpy(new, constrs->rules,
1162 constrs->rules_num * sizeof(*c));
1163 kfree(constrs->rules);
1164 }
1165 constrs->rules = new;
1166 constrs->rules_all = new_rules;
1167 }
1168 c = &constrs->rules[constrs->rules_num];
1169 c->cond = cond;
1170 c->func = func;
1171 c->var = var;
1172 c->private = private;
1173 k = 0;
1174 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001175 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1176 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001177 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 c->deps[k++] = dep;
1180 if (dep < 0)
1181 break;
1182 dep = va_arg(args, int);
1183 }
1184 constrs->rules_num++;
1185 va_end(args);
1186 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001189EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001192 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001193 * @runtime: PCM runtime instance
1194 * @var: hw_params variable to apply the mask
1195 * @mask: the bitmap mask
1196 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001197 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001198 *
1199 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001201int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 u_int32_t mask)
1203{
Takashi Iwai877211f2005-11-17 13:59:38 +01001204 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1205 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 *maskp->bits &= mask;
1207 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1208 if (*maskp->bits == 0)
1209 return -EINVAL;
1210 return 0;
1211}
1212
1213/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001214 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001215 * @runtime: PCM runtime instance
1216 * @var: hw_params variable to apply the mask
1217 * @mask: the 64bit bitmap mask
1218 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001219 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001220 *
1221 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001223int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 u_int64_t mask)
1225{
Takashi Iwai877211f2005-11-17 13:59:38 +01001226 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1227 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 maskp->bits[0] &= (u_int32_t)mask;
1229 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1230 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1231 if (! maskp->bits[0] && ! maskp->bits[1])
1232 return -EINVAL;
1233 return 0;
1234}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001235EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001238 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001239 * @runtime: PCM runtime instance
1240 * @var: hw_params variable to apply the integer constraint
1241 *
1242 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001243 *
1244 * Return: Positive if the value is changed, zero if it's not changed, or a
1245 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001247int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Takashi Iwai877211f2005-11-17 13:59:38 +01001249 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return snd_interval_setinteger(constrs_interval(constrs, var));
1251}
1252
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001253EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001256 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001257 * @runtime: PCM runtime instance
1258 * @var: hw_params variable to apply the range
1259 * @min: the minimal value
1260 * @max: the maximal value
1261 *
1262 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001263 *
1264 * Return: Positive if the value is changed, zero if it's not changed, or a
1265 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001267int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned int min, unsigned int max)
1269{
Takashi Iwai877211f2005-11-17 13:59:38 +01001270 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1271 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 t.min = min;
1273 t.max = max;
1274 t.openmin = t.openmax = 0;
1275 t.integer = 0;
1276 return snd_interval_refine(constrs_interval(constrs, var), &t);
1277}
1278
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001279EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1280
Takashi Iwai877211f2005-11-17 13:59:38 +01001281static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1282 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwai877211f2005-11-17 13:59:38 +01001284 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1286}
1287
1288
1289/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001290 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001291 * @runtime: PCM runtime instance
1292 * @cond: condition bits
1293 * @var: hw_params variable to apply the list constraint
1294 * @l: list
1295 *
1296 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001297 *
1298 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001300int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 unsigned int cond,
1302 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001303 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001306 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 var, -1);
1308}
1309
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001310EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1311
Peter Rosinf66f8982015-01-28 15:16:06 +01001312static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1313 struct snd_pcm_hw_rule *rule)
1314{
1315 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1316 return snd_interval_ranges(hw_param_interval(params, rule->var),
1317 r->count, r->ranges, r->mask);
1318}
1319
1320
1321/**
1322 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1323 * @runtime: PCM runtime instance
1324 * @cond: condition bits
1325 * @var: hw_params variable to apply the list of range constraints
1326 * @r: ranges
1327 *
1328 * Apply the list of range constraints to an interval parameter.
1329 *
1330 * Return: Zero if successful, or a negative error code on failure.
1331 */
1332int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1333 unsigned int cond,
1334 snd_pcm_hw_param_t var,
1335 const struct snd_pcm_hw_constraint_ranges *r)
1336{
1337 return snd_pcm_hw_rule_add(runtime, cond, var,
1338 snd_pcm_hw_rule_ranges, (void *)r,
1339 var, -1);
1340}
1341EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1342
Takashi Iwai877211f2005-11-17 13:59:38 +01001343static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1344 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001346 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 unsigned int num = 0, den = 0;
1348 int err;
1349 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1350 r->nrats, r->rats, &num, &den);
1351 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1352 params->rate_num = num;
1353 params->rate_den = den;
1354 }
1355 return err;
1356}
1357
1358/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001359 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001360 * @runtime: PCM runtime instance
1361 * @cond: condition bits
1362 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001363 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001364 *
1365 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001367int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 unsigned int cond,
1369 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001370 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001373 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 var, -1);
1375}
1376
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001377EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1378
Takashi Iwai877211f2005-11-17 13:59:38 +01001379static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1380 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001382 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 unsigned int num = 0, den = 0;
1384 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1385 r->nrats, r->rats, &num, &den);
1386 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1387 params->rate_num = num;
1388 params->rate_den = den;
1389 }
1390 return err;
1391}
1392
1393/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001394 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001395 * @runtime: PCM runtime instance
1396 * @cond: condition bits
1397 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001398 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001399 *
1400 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001402int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned int cond,
1404 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001405 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001408 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 var, -1);
1410}
1411
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001412EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1413
Takashi Iwai877211f2005-11-17 13:59:38 +01001414static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1415 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 unsigned int l = (unsigned long) rule->private;
1418 int width = l & 0xffff;
1419 unsigned int msbits = l >> 16;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09001420 const struct snd_interval *i =
1421 hw_param_interval_c(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 Sakamotob46fe5d2017-05-17 08:48:18 +09001739 static const int vars[] = {
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001740 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 };
Takashi Sakamotob46fe5d2017-05-17 08:48:18 +09001750 const int *v;
1751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001753 for (v = vars; *v != -1; v++) {
1754 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1755 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1756 else
1757 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001758 if (snd_BUG_ON(err < 0))
1759 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
Takashi Iwai877211f2005-11-17 13:59:38 +01001764static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 void *arg)
1766{
Takashi Iwai877211f2005-11-17 13:59:38 +01001767 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned long flags;
1769 snd_pcm_stream_lock_irqsave(substream, flags);
1770 if (snd_pcm_running(substream) &&
1771 snd_pcm_update_hw_ptr(substream) >= 0)
1772 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001773 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001775 runtime->hw_ptr_wrap = 0;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 snd_pcm_stream_unlock_irqrestore(substream, flags);
1778 return 0;
1779}
1780
Takashi Iwai877211f2005-11-17 13:59:38 +01001781static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 void *arg)
1783{
Takashi Iwai877211f2005-11-17 13:59:38 +01001784 struct snd_pcm_channel_info *info = arg;
1785 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int width;
1787 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1788 info->offset = -1;
1789 return 0;
1790 }
1791 width = snd_pcm_format_physical_width(runtime->format);
1792 if (width < 0)
1793 return width;
1794 info->offset = 0;
1795 switch (runtime->access) {
1796 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1797 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1798 info->first = info->channel * width;
1799 info->step = runtime->channels * width;
1800 break;
1801 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1802 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1803 {
1804 size_t size = runtime->dma_bytes / runtime->channels;
1805 info->first = info->channel * size * 8;
1806 info->step = width;
1807 break;
1808 }
1809 default:
1810 snd_BUG();
1811 break;
1812 }
1813 return 0;
1814}
1815
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001816static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1817 void *arg)
1818{
1819 struct snd_pcm_hw_params *params = arg;
1820 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001821 int channels;
1822 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001823
1824 params->fifo_size = substream->runtime->hw.fifo_size;
1825 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1826 format = params_format(params);
1827 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001828 frame_size = snd_pcm_format_size(format, channels);
1829 if (frame_size > 0)
1830 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001831 }
1832 return 0;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/**
1836 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1837 * @substream: the pcm substream instance
1838 * @cmd: ioctl command
1839 * @arg: ioctl argument
1840 *
1841 * Processes the generic ioctl commands for PCM.
1842 * Can be passed as the ioctl callback for PCM ops.
1843 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001844 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001846int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 unsigned int cmd, void *arg)
1848{
1849 switch (cmd) {
1850 case SNDRV_PCM_IOCTL1_INFO:
1851 return 0;
1852 case SNDRV_PCM_IOCTL1_RESET:
1853 return snd_pcm_lib_ioctl_reset(substream, arg);
1854 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1855 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001856 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1857 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 return -ENXIO;
1860}
1861
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001862EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864/**
1865 * snd_pcm_period_elapsed - update the pcm status for the next period
1866 * @substream: the pcm substream instance
1867 *
1868 * This function is called from the interrupt handler when the
1869 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001870 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 *
1872 * Even if more than one periods have elapsed since the last call, you
1873 * have to call this only once.
1874 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001875void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai877211f2005-11-17 13:59:38 +01001877 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unsigned long flags;
1879
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001880 if (PCM_RUNTIME_CHECK(substream))
1881 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 snd_pcm_stream_lock_irqsave(substream, flags);
1885 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001886 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto _end;
1888
Jie Yang90bbaf62015-10-16 17:57:46 +08001889#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (substream->timer_running)
1891 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001895 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001898EXPORT_SYMBOL(snd_pcm_period_elapsed);
1899
Takashi Iwai13075512008-01-08 18:08:14 +01001900/*
1901 * Wait until avail_min data becomes available
1902 * Returns a negative error code if any error occurs during operation.
1903 * The available space is stored on availp. When err = 0 and avail = 0
1904 * on the capture stream, it indicates the stream is in DRAINING state.
1905 */
David Dillow5daeba32010-06-27 00:13:20 +02001906static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001907 snd_pcm_uframes_t *availp)
1908{
1909 struct snd_pcm_runtime *runtime = substream->runtime;
1910 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1911 wait_queue_t wait;
1912 int err = 0;
1913 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001914 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001915
Arjan van de Ven763437a2011-09-15 08:49:25 +02001916 init_waitqueue_entry(&wait, current);
1917 set_current_state(TASK_INTERRUPTIBLE);
1918 add_wait_queue(&runtime->tsleep, &wait);
1919
Takashi Iwaif2b36142011-05-26 08:09:38 +02001920 if (runtime->no_period_wakeup)
1921 wait_time = MAX_SCHEDULE_TIMEOUT;
1922 else {
1923 wait_time = 10;
1924 if (runtime->rate) {
1925 long t = runtime->period_size * 2 / runtime->rate;
1926 wait_time = max(t, wait_time);
1927 }
1928 wait_time = msecs_to_jiffies(wait_time * 1000);
1929 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001930
Takashi Iwai13075512008-01-08 18:08:14 +01001931 for (;;) {
1932 if (signal_pending(current)) {
1933 err = -ERESTARTSYS;
1934 break;
1935 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001936
1937 /*
1938 * We need to check if space became available already
1939 * (and thus the wakeup happened already) first to close
1940 * the race of space already having become available.
1941 * This check must happen after been added to the waitqueue
1942 * and having current state be INTERRUPTIBLE.
1943 */
1944 if (is_playback)
1945 avail = snd_pcm_playback_avail(runtime);
1946 else
1947 avail = snd_pcm_capture_avail(runtime);
1948 if (avail >= runtime->twake)
1949 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001950 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001951
1952 tout = schedule_timeout(wait_time);
1953
Takashi Iwai13075512008-01-08 18:08:14 +01001954 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001955 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001956 switch (runtime->status->state) {
1957 case SNDRV_PCM_STATE_SUSPENDED:
1958 err = -ESTRPIPE;
1959 goto _endloop;
1960 case SNDRV_PCM_STATE_XRUN:
1961 err = -EPIPE;
1962 goto _endloop;
1963 case SNDRV_PCM_STATE_DRAINING:
1964 if (is_playback)
1965 err = -EPIPE;
1966 else
1967 avail = 0; /* indicate draining */
1968 goto _endloop;
1969 case SNDRV_PCM_STATE_OPEN:
1970 case SNDRV_PCM_STATE_SETUP:
1971 case SNDRV_PCM_STATE_DISCONNECTED:
1972 err = -EBADFD;
1973 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001974 case SNDRV_PCM_STATE_PAUSED:
1975 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001976 }
1977 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001978 pcm_dbg(substream->pcm,
1979 "%s write error (DMA or IRQ trouble?)\n",
1980 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001981 err = -EIO;
1982 break;
1983 }
Takashi Iwai13075512008-01-08 18:08:14 +01001984 }
1985 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001986 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001987 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001988 *availp = avail;
1989 return err;
1990}
1991
Takashi Iwai877211f2005-11-17 13:59:38 +01001992static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 unsigned int hwoff,
1994 unsigned long data, unsigned int off,
1995 snd_pcm_uframes_t frames)
1996{
Takashi Iwai877211f2005-11-17 13:59:38 +01001997 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int err;
1999 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2000 if (substream->ops->copy) {
2001 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2002 return err;
2003 } else {
2004 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2006 return -EFAULT;
2007 }
2008 return 0;
2009}
2010
Takashi Iwai877211f2005-11-17 13:59:38 +01002011typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 unsigned long data, unsigned int off,
2013 snd_pcm_uframes_t size);
2014
Takashi Iwai877211f2005-11-17 13:59:38 +01002015static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 unsigned long data,
2017 snd_pcm_uframes_t size,
2018 int nonblock,
2019 transfer_f transfer)
2020{
Takashi Iwai877211f2005-11-17 13:59:38 +01002021 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 snd_pcm_uframes_t xfer = 0;
2023 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002024 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 int err = 0;
2026
2027 if (size == 0)
2028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 snd_pcm_stream_lock_irq(substream);
2031 switch (runtime->status->state) {
2032 case SNDRV_PCM_STATE_PREPARED:
2033 case SNDRV_PCM_STATE_RUNNING:
2034 case SNDRV_PCM_STATE_PAUSED:
2035 break;
2036 case SNDRV_PCM_STATE_XRUN:
2037 err = -EPIPE;
2038 goto _end_unlock;
2039 case SNDRV_PCM_STATE_SUSPENDED:
2040 err = -ESTRPIPE;
2041 goto _end_unlock;
2042 default:
2043 err = -EBADFD;
2044 goto _end_unlock;
2045 }
2046
David Dillow5daeba32010-06-27 00:13:20 +02002047 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002048 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2049 snd_pcm_update_hw_ptr(substream);
2050 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 while (size > 0) {
2052 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002054 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (nonblock) {
2056 err = -EAGAIN;
2057 goto _end_unlock;
2058 }
David Dillow5daeba32010-06-27 00:13:20 +02002059 runtime->twake = min_t(snd_pcm_uframes_t, size,
2060 runtime->control->avail_min ? : 1);
2061 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002062 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 frames = size > avail ? avail : size;
2066 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2067 if (frames > cont)
2068 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002069 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002070 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002071 snd_pcm_stream_unlock_irq(substream);
2072 return -EINVAL;
2073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 appl_ptr = runtime->control->appl_ptr;
2075 appl_ofs = appl_ptr % runtime->buffer_size;
2076 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002077 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002079 if (err < 0)
2080 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 switch (runtime->status->state) {
2082 case SNDRV_PCM_STATE_XRUN:
2083 err = -EPIPE;
2084 goto _end_unlock;
2085 case SNDRV_PCM_STATE_SUSPENDED:
2086 err = -ESTRPIPE;
2087 goto _end_unlock;
2088 default:
2089 break;
2090 }
2091 appl_ptr += frames;
2092 if (appl_ptr >= runtime->boundary)
2093 appl_ptr -= runtime->boundary;
2094 runtime->control->appl_ptr = appl_ptr;
2095 if (substream->ops->ack)
2096 substream->ops->ack(substream);
2097
2098 offset += frames;
2099 size -= frames;
2100 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002101 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2103 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2104 err = snd_pcm_start(substream);
2105 if (err < 0)
2106 goto _end_unlock;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002110 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002111 if (xfer > 0 && err >= 0)
2112 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2115}
2116
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002117/* sanity-check for read/write methods */
2118static int pcm_sanity_check(struct snd_pcm_substream *substream)
2119{
2120 struct snd_pcm_runtime *runtime;
2121 if (PCM_RUNTIME_CHECK(substream))
2122 return -ENXIO;
2123 runtime = substream->runtime;
2124 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2125 return -EINVAL;
2126 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2127 return -EBADFD;
2128 return 0;
2129}
2130
Takashi Iwai877211f2005-11-17 13:59:38 +01002131snd_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 -07002132{
Takashi Iwai877211f2005-11-17 13:59:38 +01002133 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002135 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002137 err = pcm_sanity_check(substream);
2138 if (err < 0)
2139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002141 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2144 runtime->channels > 1)
2145 return -EINVAL;
2146 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2147 snd_pcm_lib_write_transfer);
2148}
2149
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002150EXPORT_SYMBOL(snd_pcm_lib_write);
2151
Takashi Iwai877211f2005-11-17 13:59:38 +01002152static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 unsigned int hwoff,
2154 unsigned long data, unsigned int off,
2155 snd_pcm_uframes_t frames)
2156{
Takashi Iwai877211f2005-11-17 13:59:38 +01002157 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int err;
2159 void __user **bufs = (void __user **)data;
2160 int channels = runtime->channels;
2161 int c;
2162 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002163 if (snd_BUG_ON(!substream->ops->silence))
2164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 for (c = 0; c < channels; ++c, ++bufs) {
2166 if (*bufs == NULL) {
2167 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2168 return err;
2169 } else {
2170 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2171 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2172 return err;
2173 }
2174 }
2175 } else {
2176 /* default transfer behaviour */
2177 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 for (c = 0; c < channels; ++c, ++bufs) {
2179 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2180 if (*bufs == NULL) {
2181 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2182 } else {
2183 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2184 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2185 return -EFAULT;
2186 }
2187 }
2188 }
2189 return 0;
2190}
2191
Takashi Iwai877211f2005-11-17 13:59:38 +01002192snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 void __user **bufs,
2194 snd_pcm_uframes_t frames)
2195{
Takashi Iwai877211f2005-11-17 13:59:38 +01002196 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002200 err = pcm_sanity_check(substream);
2201 if (err < 0)
2202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002204 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2207 return -EINVAL;
2208 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2209 nonblock, snd_pcm_lib_writev_transfer);
2210}
2211
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002212EXPORT_SYMBOL(snd_pcm_lib_writev);
2213
Takashi Iwai877211f2005-11-17 13:59:38 +01002214static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 unsigned int hwoff,
2216 unsigned long data, unsigned int off,
2217 snd_pcm_uframes_t frames)
2218{
Takashi Iwai877211f2005-11-17 13:59:38 +01002219 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 int err;
2221 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2222 if (substream->ops->copy) {
2223 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2224 return err;
2225 } else {
2226 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2228 return -EFAULT;
2229 }
2230 return 0;
2231}
2232
Takashi Iwai877211f2005-11-17 13:59:38 +01002233static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 unsigned long data,
2235 snd_pcm_uframes_t size,
2236 int nonblock,
2237 transfer_f transfer)
2238{
Takashi Iwai877211f2005-11-17 13:59:38 +01002239 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 snd_pcm_uframes_t xfer = 0;
2241 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002242 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 int err = 0;
2244
2245 if (size == 0)
2246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 snd_pcm_stream_lock_irq(substream);
2249 switch (runtime->status->state) {
2250 case SNDRV_PCM_STATE_PREPARED:
2251 if (size >= runtime->start_threshold) {
2252 err = snd_pcm_start(substream);
2253 if (err < 0)
2254 goto _end_unlock;
2255 }
2256 break;
2257 case SNDRV_PCM_STATE_DRAINING:
2258 case SNDRV_PCM_STATE_RUNNING:
2259 case SNDRV_PCM_STATE_PAUSED:
2260 break;
2261 case SNDRV_PCM_STATE_XRUN:
2262 err = -EPIPE;
2263 goto _end_unlock;
2264 case SNDRV_PCM_STATE_SUSPENDED:
2265 err = -ESTRPIPE;
2266 goto _end_unlock;
2267 default:
2268 err = -EBADFD;
2269 goto _end_unlock;
2270 }
2271
David Dillow5daeba32010-06-27 00:13:20 +02002272 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002273 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2274 snd_pcm_update_hw_ptr(substream);
2275 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 while (size > 0) {
2277 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002279 if (!avail) {
2280 if (runtime->status->state ==
2281 SNDRV_PCM_STATE_DRAINING) {
2282 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 goto _end_unlock;
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (nonblock) {
2286 err = -EAGAIN;
2287 goto _end_unlock;
2288 }
David Dillow5daeba32010-06-27 00:13:20 +02002289 runtime->twake = min_t(snd_pcm_uframes_t, size,
2290 runtime->control->avail_min ? : 1);
2291 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002292 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002294 if (!avail)
2295 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 frames = size > avail ? avail : size;
2298 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2299 if (frames > cont)
2300 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002301 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002302 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002303 snd_pcm_stream_unlock_irq(substream);
2304 return -EINVAL;
2305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 appl_ptr = runtime->control->appl_ptr;
2307 appl_ofs = appl_ptr % runtime->buffer_size;
2308 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002309 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002311 if (err < 0)
2312 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 switch (runtime->status->state) {
2314 case SNDRV_PCM_STATE_XRUN:
2315 err = -EPIPE;
2316 goto _end_unlock;
2317 case SNDRV_PCM_STATE_SUSPENDED:
2318 err = -ESTRPIPE;
2319 goto _end_unlock;
2320 default:
2321 break;
2322 }
2323 appl_ptr += frames;
2324 if (appl_ptr >= runtime->boundary)
2325 appl_ptr -= runtime->boundary;
2326 runtime->control->appl_ptr = appl_ptr;
2327 if (substream->ops->ack)
2328 substream->ops->ack(substream);
2329
2330 offset += frames;
2331 size -= frames;
2332 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002333 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002336 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002337 if (xfer > 0 && err >= 0)
2338 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2341}
2342
Takashi Iwai877211f2005-11-17 13:59:38 +01002343snd_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 -07002344{
Takashi Iwai877211f2005-11-17 13:59:38 +01002345 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002349 err = pcm_sanity_check(substream);
2350 if (err < 0)
2351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002353 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2355 return -EINVAL;
2356 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2357}
2358
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002359EXPORT_SYMBOL(snd_pcm_lib_read);
2360
Takashi Iwai877211f2005-11-17 13:59:38 +01002361static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 unsigned int hwoff,
2363 unsigned long data, unsigned int off,
2364 snd_pcm_uframes_t frames)
2365{
Takashi Iwai877211f2005-11-17 13:59:38 +01002366 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int err;
2368 void __user **bufs = (void __user **)data;
2369 int channels = runtime->channels;
2370 int c;
2371 if (substream->ops->copy) {
2372 for (c = 0; c < channels; ++c, ++bufs) {
2373 char __user *buf;
2374 if (*bufs == NULL)
2375 continue;
2376 buf = *bufs + samples_to_bytes(runtime, off);
2377 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2378 return err;
2379 }
2380 } else {
2381 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 for (c = 0; c < channels; ++c, ++bufs) {
2383 char *hwbuf;
2384 char __user *buf;
2385 if (*bufs == NULL)
2386 continue;
2387
2388 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2389 buf = *bufs + samples_to_bytes(runtime, off);
2390 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2391 return -EFAULT;
2392 }
2393 }
2394 return 0;
2395}
2396
Takashi Iwai877211f2005-11-17 13:59:38 +01002397snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 void __user **bufs,
2399 snd_pcm_uframes_t frames)
2400{
Takashi Iwai877211f2005-11-17 13:59:38 +01002401 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002403 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002405 err = pcm_sanity_check(substream);
2406 if (err < 0)
2407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2410 return -EBADFD;
2411
Takashi Iwai0df63e42006-04-28 15:13:41 +02002412 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2414 return -EINVAL;
2415 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002419
2420/*
2421 * standard channel mapping helpers
2422 */
2423
2424/* default channel maps for multi-channel playbacks, up to 8 channels */
2425const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2426 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002427 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002428 { .channels = 2,
2429 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2430 { .channels = 4,
2431 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2432 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2433 { .channels = 6,
2434 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2435 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2436 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2437 { .channels = 8,
2438 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2439 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2440 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2441 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2442 { }
2443};
2444EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2445
2446/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2447const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2448 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002449 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002450 { .channels = 2,
2451 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2452 { .channels = 4,
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2454 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2455 { .channels = 6,
2456 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2457 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2458 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2459 { .channels = 8,
2460 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2461 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2462 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2463 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2464 { }
2465};
2466EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2467
2468static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2469{
2470 if (ch > info->max_channels)
2471 return false;
2472 return !info->channel_mask || (info->channel_mask & (1U << ch));
2473}
2474
2475static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_info *uinfo)
2477{
2478 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2479
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2481 uinfo->count = 0;
2482 uinfo->count = info->max_channels;
2483 uinfo->value.integer.min = 0;
2484 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2485 return 0;
2486}
2487
2488/* get callback for channel map ctl element
2489 * stores the channel position firstly matching with the current channels
2490 */
2491static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2493{
2494 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2495 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2496 struct snd_pcm_substream *substream;
2497 const struct snd_pcm_chmap_elem *map;
2498
2499 if (snd_BUG_ON(!info->chmap))
2500 return -EINVAL;
2501 substream = snd_pcm_chmap_substream(info, idx);
2502 if (!substream)
2503 return -ENODEV;
2504 memset(ucontrol->value.integer.value, 0,
2505 sizeof(ucontrol->value.integer.value));
2506 if (!substream->runtime)
2507 return 0; /* no channels set */
2508 for (map = info->chmap; map->channels; map++) {
2509 int i;
2510 if (map->channels == substream->runtime->channels &&
2511 valid_chmap_channels(info, map->channels)) {
2512 for (i = 0; i < map->channels; i++)
2513 ucontrol->value.integer.value[i] = map->map[i];
2514 return 0;
2515 }
2516 }
2517 return -EINVAL;
2518}
2519
2520/* tlv callback for channel map ctl element
2521 * expands the pre-defined channel maps in a form of TLV
2522 */
2523static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2524 unsigned int size, unsigned int __user *tlv)
2525{
2526 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2527 const struct snd_pcm_chmap_elem *map;
2528 unsigned int __user *dst;
2529 int c, count = 0;
2530
2531 if (snd_BUG_ON(!info->chmap))
2532 return -EINVAL;
2533 if (size < 8)
2534 return -ENOMEM;
2535 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2536 return -EFAULT;
2537 size -= 8;
2538 dst = tlv + 2;
2539 for (map = info->chmap; map->channels; map++) {
2540 int chs_bytes = map->channels * 4;
2541 if (!valid_chmap_channels(info, map->channels))
2542 continue;
2543 if (size < 8)
2544 return -ENOMEM;
2545 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2546 put_user(chs_bytes, dst + 1))
2547 return -EFAULT;
2548 dst += 2;
2549 size -= 8;
2550 count += 8;
2551 if (size < chs_bytes)
2552 return -ENOMEM;
2553 size -= chs_bytes;
2554 count += chs_bytes;
2555 for (c = 0; c < map->channels; c++) {
2556 if (put_user(map->map[c], dst))
2557 return -EFAULT;
2558 dst++;
2559 }
2560 }
2561 if (put_user(count, tlv + 1))
2562 return -EFAULT;
2563 return 0;
2564}
2565
2566static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2567{
2568 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2569 info->pcm->streams[info->stream].chmap_kctl = NULL;
2570 kfree(info);
2571}
2572
2573/**
2574 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2575 * @pcm: the assigned PCM instance
2576 * @stream: stream direction
2577 * @chmap: channel map elements (for query)
2578 * @max_channels: the max number of channels for the stream
2579 * @private_value: the value passed to each kcontrol's private_value field
2580 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2581 *
2582 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002583 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002584 */
2585int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2586 const struct snd_pcm_chmap_elem *chmap,
2587 int max_channels,
2588 unsigned long private_value,
2589 struct snd_pcm_chmap **info_ret)
2590{
2591 struct snd_pcm_chmap *info;
2592 struct snd_kcontrol_new knew = {
2593 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2594 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002595 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2596 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2597 .info = pcm_chmap_ctl_info,
2598 .get = pcm_chmap_ctl_get,
2599 .tlv.c = pcm_chmap_ctl_tlv,
2600 };
2601 int err;
2602
Takashi Iwai8d879be2016-05-10 16:07:40 +02002603 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2604 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002605 info = kzalloc(sizeof(*info), GFP_KERNEL);
2606 if (!info)
2607 return -ENOMEM;
2608 info->pcm = pcm;
2609 info->stream = stream;
2610 info->chmap = chmap;
2611 info->max_channels = max_channels;
2612 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2613 knew.name = "Playback Channel Map";
2614 else
2615 knew.name = "Capture Channel Map";
2616 knew.device = pcm->device;
2617 knew.count = pcm->streams[stream].substream_count;
2618 knew.private_value = private_value;
2619 info->kctl = snd_ctl_new1(&knew, info);
2620 if (!info->kctl) {
2621 kfree(info);
2622 return -ENOMEM;
2623 }
2624 info->kctl->private_free = pcm_chmap_ctl_private_free;
2625 err = snd_ctl_add(pcm->card, info->kctl);
2626 if (err < 0)
2627 return err;
2628 pcm->streams[stream].chmap_kctl = info->kctl;
2629 if (info_ret)
2630 *info_ret = info;
2631 return 0;
2632}
2633EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);