blob: f57a58ac7ae0cfe8f9c1bb5536a3ce0c8a8c94e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/time.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020025#include <linux/math64.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/control.h>
Takashi Iwai2d3391e2012-07-27 18:27:00 +020029#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/info.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/timer.h>
34
Takashi Iwaif5914902014-11-04 12:45:59 +010035#ifdef CONFIG_SND_PCM_XRUN_DEBUG
36#define CREATE_TRACE_POINTS
37#include "pcm_trace.h"
38#else
39#define trace_hwptr(substream, pos, in_interrupt)
40#define trace_xrun(substream)
41#define trace_hw_ptr_error(substream, reason)
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * fill ring buffer with silence
46 * runtime->silence_start: starting pointer to silence area
47 * runtime->silence_filled: size filled with silence
48 * runtime->silence_threshold: threshold from application
49 * runtime->silence_size: maximal size from application
50 *
51 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
52 */
Takashi Iwai877211f2005-11-17 13:59:38 +010053void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Takashi Iwai877211f2005-11-17 13:59:38 +010055 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 snd_pcm_uframes_t frames, ofs, transfer;
57
58 if (runtime->silence_size < runtime->boundary) {
59 snd_pcm_sframes_t noise_dist, n;
60 if (runtime->silence_start != runtime->control->appl_ptr) {
61 n = runtime->control->appl_ptr - runtime->silence_start;
62 if (n < 0)
63 n += runtime->boundary;
64 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
65 runtime->silence_filled -= n;
66 else
67 runtime->silence_filled = 0;
68 runtime->silence_start = runtime->control->appl_ptr;
69 }
Takashi Iwai235475c2005-12-07 15:28:07 +010070 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
73 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
74 return;
75 frames = runtime->silence_threshold - noise_dist;
76 if (frames > runtime->silence_size)
77 frames = runtime->silence_size;
78 } else {
79 if (new_hw_ptr == ULONG_MAX) { /* initialization */
80 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020081 if (avail > runtime->buffer_size)
82 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 runtime->silence_filled = avail > 0 ? avail : 0;
84 runtime->silence_start = (runtime->status->hw_ptr +
85 runtime->silence_filled) %
86 runtime->boundary;
87 } else {
88 ofs = runtime->status->hw_ptr;
89 frames = new_hw_ptr - ofs;
90 if ((snd_pcm_sframes_t)frames < 0)
91 frames += runtime->boundary;
92 runtime->silence_filled -= frames;
93 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
94 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020095 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020097 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 frames = runtime->buffer_size - runtime->silence_filled;
101 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200102 if (snd_BUG_ON(frames > runtime->buffer_size))
103 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (frames == 0)
105 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +0200106 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 while (frames > 0) {
108 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
109 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
110 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
111 if (substream->ops->silence) {
112 int err;
113 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200114 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 } else {
116 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
117 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
118 }
119 } else {
120 unsigned int c;
121 unsigned int channels = runtime->channels;
122 if (substream->ops->silence) {
123 for (c = 0; c < channels; ++c) {
124 int err;
125 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200126 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128 } else {
129 size_t dma_csize = runtime->dma_bytes / channels;
130 for (c = 0; c < channels; ++c) {
131 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
132 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
133 }
134 }
135 }
136 runtime->silence_filled += transfer;
137 frames -= transfer;
138 ofs = 0;
139 }
140}
141
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200142#ifdef CONFIG_SND_DEBUG
143void snd_pcm_debug_name(struct snd_pcm_substream *substream,
Takashi Iwaic0070112009-06-08 15:58:48 +0200144 char *name, size_t len)
145{
146 snprintf(name, len, "pcmC%dD%d%c:%d",
147 substream->pcm->card->number,
148 substream->pcm->device,
149 substream->stream ? 'c' : 'p',
150 substream->number);
151}
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200152EXPORT_SYMBOL(snd_pcm_debug_name);
153#endif
Takashi Iwaic0070112009-06-08 15:58:48 +0200154
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100155#define XRUN_DEBUG_BASIC (1<<0)
156#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
157#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100158
159#ifdef CONFIG_SND_PCM_XRUN_DEBUG
160
161#define xrun_debug(substream, mask) \
162 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200163#else
164#define xrun_debug(substream, mask) 0
165#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100166
167#define dump_stack_on_xrun(substream) do { \
168 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
169 dump_stack(); \
170 } while (0)
171
Takashi Iwai877211f2005-11-17 13:59:38 +0100172static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200174 struct snd_pcm_runtime *runtime = substream->runtime;
175
Takashi Iwaif5914902014-11-04 12:45:59 +0100176 trace_xrun(substream);
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200177 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
178 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100180 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200181 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200182 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100183 pcm_warn(substream->pcm, "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100184 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Jarkko Nikula0f170142010-03-26 16:07:25 +0200188#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwaif5914902014-11-04 12:45:59 +0100189#define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100190 do { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100191 trace_hw_ptr_error(substream, reason); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100192 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Takashi Iwaif5914902014-11-04 12:45:59 +0100193 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
194 (in_interrupt) ? 'Q' : 'P', ##args); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100195 dump_stack_on_xrun(substream); \
196 } \
197 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100199#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
200
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100201#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100202
203#endif
204
Jaroslav Kysela12509322010-01-07 15:36:31 +0100205int snd_pcm_update_state(struct snd_pcm_substream *substream,
206 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 snd_pcm_uframes_t avail;
209
210 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
211 avail = snd_pcm_playback_avail(runtime);
212 else
213 avail = snd_pcm_capture_avail(runtime);
214 if (avail > runtime->avail_max)
215 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200216 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
217 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200219 return -EPIPE;
220 }
221 } else {
222 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200224 return -EPIPE;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
David Dillow5daeba32010-06-27 00:13:20 +0200227 if (runtime->twake) {
228 if (avail >= runtime->twake)
229 wake_up(&runtime->tsleep);
230 } else if (avail >= runtime->control->avail_min)
231 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600235static void update_audio_tstamp(struct snd_pcm_substream *substream,
236 struct timespec *curr_tstamp,
237 struct timespec *audio_tstamp)
238{
239 struct snd_pcm_runtime *runtime = substream->runtime;
240 u64 audio_frames, audio_nsecs;
241 struct timespec driver_tstamp;
242
243 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
244 return;
245
246 if (!(substream->ops->get_time_info) ||
247 (runtime->audio_tstamp_report.actual_type ==
248 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
249
250 /*
251 * provide audio timestamp derived from pointer position
252 * add delay only if requested
253 */
254
255 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
256
257 if (runtime->audio_tstamp_config.report_delay) {
258 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
259 audio_frames -= runtime->delay;
260 else
261 audio_frames += runtime->delay;
262 }
263 audio_nsecs = div_u64(audio_frames * 1000000000LL,
264 runtime->rate);
265 *audio_tstamp = ns_to_timespec(audio_nsecs);
266 }
Henrik Eriksson14eb4542017-11-21 09:29:28 +0100267 if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
268 runtime->status->audio_tstamp = *audio_tstamp;
269 runtime->status->tstamp = *curr_tstamp;
270 }
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600271
272 /*
273 * re-take a driver timestamp to let apps detect if the reference tstamp
274 * read by low-level hardware was provided with a delay
275 */
276 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
277 runtime->driver_tstamp = driver_tstamp;
278}
279
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100280static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
281 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Takashi Iwai877211f2005-11-17 13:59:38 +0100283 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100285 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200286 snd_pcm_sframes_t hdelta, delta;
287 unsigned long jdelta;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500288 unsigned long curr_jiffies;
289 struct timespec curr_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500290 struct timespec audio_tstamp;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500291 int crossed_boundary = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200293 old_hw_ptr = runtime->status->hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500294
295 /*
296 * group pointer, time and jiffies reads to allow for more
297 * accurate correlations/corrections.
298 * The values are stored at the end of this routine after
299 * corrections for hw_ptr position
300 */
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100301 pos = substream->ops->pointer(substream);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500302 curr_jiffies = jiffies;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500303 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600304 if ((substream->ops->get_time_info) &&
305 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
306 substream->ops->get_time_info(substream, &curr_tstamp,
307 &audio_tstamp,
308 &runtime->audio_tstamp_config,
309 &runtime->audio_tstamp_report);
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500310
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600311 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
312 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
313 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
314 } else
315 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (pos == SNDRV_PCM_POS_XRUN) {
319 xrun(substream);
320 return -EPIPE;
321 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100322 if (pos >= runtime->buffer_size) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100323 if (printk_ratelimit()) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100324 char name[16];
Eliot Blennerhassettacb03d42011-07-23 12:36:25 +1200325 snd_pcm_debug_name(substream, name, sizeof(name));
Takashi Iwai09e56df2014-02-04 18:19:48 +0100326 pcm_err(substream->pcm,
Takashi Iwai0ab1ace2016-03-10 20:56:20 +0100327 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
Takashi Iwai09e56df2014-02-04 18:19:48 +0100328 name, pos, runtime->buffer_size,
329 runtime->period_size);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100330 }
331 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200332 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100333 pos -= pos % runtime->min_align;
Takashi Iwaif5914902014-11-04 12:45:59 +0100334 trace_hwptr(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100335 hw_base = runtime->hw_ptr_base;
336 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100337 if (in_interrupt) {
338 /* we know that one period was processed */
339 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100340 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100341 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200342 /* check for double acknowledged interrupts */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500343 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Koro Chen13a98832015-05-13 22:39:03 +0800344 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200345 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500346 if (hw_base >= runtime->boundary) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200347 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500348 crossed_boundary++;
349 }
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200350 new_hw_ptr = hw_base + pos;
351 goto __delta;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100355 /* new_hw_ptr might be lower than old_hw_ptr in case when */
356 /* pointer crosses the end of the ring buffer */
357 if (new_hw_ptr < old_hw_ptr) {
358 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500359 if (hw_base >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100360 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500361 crossed_boundary++;
362 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100363 new_hw_ptr = hw_base + pos;
364 }
365 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200366 delta = new_hw_ptr - old_hw_ptr;
367 if (delta < 0)
368 delta += runtime->boundary;
Clemens Ladischab69a492010-11-15 10:46:23 +0100369
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100370 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200371 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100372 /*
373 * Without regular period interrupts, we have to check
374 * the elapsed time to detect xruns.
375 */
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500376 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Clemens Ladisch47228e42010-11-18 09:53:07 +0100377 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
378 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100379 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200380 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
381 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100382 delta += runtime->buffer_size;
383 hw_base += runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500384 if (hw_base >= runtime->boundary) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100385 hw_base = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500386 crossed_boundary++;
387 }
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100388 new_hw_ptr = hw_base + pos;
389 hdelta -= runtime->hw_ptr_buffer_jiffies;
390 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100391 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100392 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100393
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100394 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100395 if (delta >= runtime->buffer_size + runtime->period_size) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100396 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
397 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
398 substream->stream, (long)pos,
399 (long)new_hw_ptr, (long)old_hw_ptr);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100400 return 0;
401 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200402
403 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100404 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200405 goto no_jiffies_check;
406
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200407 /* Skip the jiffies check for hardwares with BATCH flag.
408 * Such hardware usually just increases the position at each IRQ,
409 * thus it can't give any strange position.
410 */
411 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
412 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100413 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200414 if (hdelta < runtime->delay)
415 goto no_jiffies_check;
416 hdelta -= runtime->delay;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500417 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200418 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
419 delta = jdelta /
420 (((runtime->period_size * HZ) / runtime->rate)
421 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100422 /* move new_hw_ptr according jiffies not pos variable */
423 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100424 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100425 /* use loop to avoid checks for delta overflows */
426 /* the delta value is small or zero in most cases */
427 while (delta > 0) {
428 new_hw_ptr += runtime->period_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500429 if (new_hw_ptr >= runtime->boundary) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100430 new_hw_ptr -= runtime->boundary;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500431 crossed_boundary--;
432 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100433 delta--;
434 }
435 /* align hw_base to buffer_size */
Takashi Iwaif5914902014-11-04 12:45:59 +0100436 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
437 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200438 (long)pos, (long)hdelta,
439 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100440 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100441 (unsigned long)old_hw_ptr,
442 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100443 /* reset values to proper state */
444 delta = 0;
445 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200446 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200447 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200448 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaif5914902014-11-04 12:45:59 +0100449 hw_ptr_error(substream, in_interrupt,
450 "Lost interrupts?",
451 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100452 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100453 (long)new_hw_ptr,
454 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100455 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100456
Clemens Ladischab69a492010-11-15 10:46:23 +0100457 no_delta_check:
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600458 if (runtime->status->hw_ptr == new_hw_ptr) {
459 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100460 return 0;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600461 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
464 runtime->silence_size > 0)
465 snd_pcm_playback_silence(substream, new_hw_ptr);
466
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100467 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200468 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
469 if (delta < 0)
470 delta += runtime->boundary;
471 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
472 runtime->hw_ptr_interrupt += delta;
473 if (runtime->hw_ptr_interrupt >= runtime->boundary)
474 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100475 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100476 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 runtime->status->hw_ptr = new_hw_ptr;
Pierre-Louis Bossart3509a032012-05-22 14:54:02 -0500478 runtime->hw_ptr_jiffies = curr_jiffies;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -0500479 if (crossed_boundary) {
480 snd_BUG_ON(crossed_boundary != 1);
481 runtime->hw_ptr_wrap += runtime->boundary;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600484 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500485
Jaroslav Kysela12509322010-01-07 15:36:31 +0100486 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100490int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100492 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495/**
496 * snd_pcm_set_ops - set the PCM operators
497 * @pcm: the pcm instance
498 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
499 * @ops: the operator table
500 *
501 * Sets the given PCM operators to the pcm instance.
502 */
Lars-Peter Clausene6c2e7e2013-05-24 15:18:10 +0200503void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
504 const struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Takashi Iwai877211f2005-11-17 13:59:38 +0100506 struct snd_pcm_str *stream = &pcm->streams[direction];
507 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 for (substream = stream->substream; substream != NULL; substream = substream->next)
510 substream->ops = ops;
511}
512
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200513EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/**
516 * snd_pcm_sync - set the PCM sync id
517 * @substream: the pcm substream
518 *
519 * Sets the PCM sync identifier for the card.
520 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100521void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai877211f2005-11-17 13:59:38 +0100523 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 runtime->sync.id32[0] = substream->pcm->card->number;
526 runtime->sync.id32[1] = -1;
527 runtime->sync.id32[2] = -1;
528 runtime->sync.id32[3] = -1;
529}
530
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200531EXPORT_SYMBOL(snd_pcm_set_sync);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * Standard ioctl routine
535 */
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static inline unsigned int div32(unsigned int a, unsigned int b,
538 unsigned int *r)
539{
540 if (b == 0) {
541 *r = 0;
542 return UINT_MAX;
543 }
544 *r = a % b;
545 return a / b;
546}
547
548static inline unsigned int div_down(unsigned int a, unsigned int b)
549{
550 if (b == 0)
551 return UINT_MAX;
552 return a / b;
553}
554
555static inline unsigned int div_up(unsigned int a, unsigned int b)
556{
557 unsigned int r;
558 unsigned int q;
559 if (b == 0)
560 return UINT_MAX;
561 q = div32(a, b, &r);
562 if (r)
563 ++q;
564 return q;
565}
566
567static inline unsigned int mul(unsigned int a, unsigned int b)
568{
569 if (a == 0)
570 return 0;
571 if (div_down(UINT_MAX, a) < b)
572 return UINT_MAX;
573 return a * b;
574}
575
576static inline unsigned int muldiv32(unsigned int a, unsigned int b,
577 unsigned int c, unsigned int *r)
578{
579 u_int64_t n = (u_int64_t) a * b;
580 if (c == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 *r = 0;
582 return UINT_MAX;
583 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200584 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (n >= UINT_MAX) {
586 *r = 0;
587 return UINT_MAX;
588 }
589 return n;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/**
593 * snd_interval_refine - refine the interval value of configurator
594 * @i: the interval value to refine
595 * @v: the interval value to refer to
596 *
597 * Refines the interval value with the reference value.
598 * The interval is changed to the range satisfying both intervals.
599 * The interval status (min, max, integer, etc.) are evaluated.
600 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100601 * Return: Positive if the value is changed, zero if it's not changed, or a
602 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100604int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200607 if (snd_BUG_ON(snd_interval_empty(i)))
608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (i->min < v->min) {
610 i->min = v->min;
611 i->openmin = v->openmin;
612 changed = 1;
613 } else if (i->min == v->min && !i->openmin && v->openmin) {
614 i->openmin = 1;
615 changed = 1;
616 }
617 if (i->max > v->max) {
618 i->max = v->max;
619 i->openmax = v->openmax;
620 changed = 1;
621 } else if (i->max == v->max && !i->openmax && v->openmax) {
622 i->openmax = 1;
623 changed = 1;
624 }
625 if (!i->integer && v->integer) {
626 i->integer = 1;
627 changed = 1;
628 }
629 if (i->integer) {
630 if (i->openmin) {
631 i->min++;
632 i->openmin = 0;
633 }
634 if (i->openmax) {
635 i->max--;
636 i->openmax = 0;
637 }
638 } else if (!i->openmin && !i->openmax && i->min == i->max)
639 i->integer = 1;
640 if (snd_interval_checkempty(i)) {
641 snd_interval_none(i);
642 return -EINVAL;
643 }
644 return changed;
645}
646
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200647EXPORT_SYMBOL(snd_interval_refine);
648
Takashi Iwai877211f2005-11-17 13:59:38 +0100649static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200651 const unsigned int last_max = i->max;
652
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;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200658 if (i->openmin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 i->max++;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200660 /* only exclude max value if also excluded before refine */
661 i->openmax = (i->openmax && i->max >= last_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 1;
663}
664
Takashi Iwai877211f2005-11-17 13:59:38 +0100665static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200667 const unsigned int last_min = i->min;
668
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200669 if (snd_BUG_ON(snd_interval_empty(i)))
670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (snd_interval_single(i))
672 return 0;
673 i->min = i->max;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200674 if (i->openmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 i->min--;
Timo Wischer7b07e3d2018-07-10 17:28:45 +0200676 /* only exclude min value if also excluded before refine */
677 i->openmin = (i->openmin && i->min <= last_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 1;
679}
680
Takashi Iwai877211f2005-11-17 13:59:38 +0100681void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 if (a->empty || b->empty) {
684 snd_interval_none(c);
685 return;
686 }
687 c->empty = 0;
688 c->min = mul(a->min, b->min);
689 c->openmin = (a->openmin || b->openmin);
690 c->max = mul(a->max, b->max);
691 c->openmax = (a->openmax || b->openmax);
692 c->integer = (a->integer && b->integer);
693}
694
695/**
696 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200697 * @a: dividend
698 * @b: divisor
699 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * c = a / b
702 *
703 * Returns non-zero if the value is changed, zero if not changed.
704 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100705void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 unsigned int r;
708 if (a->empty || b->empty) {
709 snd_interval_none(c);
710 return;
711 }
712 c->empty = 0;
713 c->min = div32(a->min, b->max, &r);
714 c->openmin = (r || a->openmin || b->openmax);
715 if (b->min > 0) {
716 c->max = div32(a->max, b->min, &r);
717 if (r) {
718 c->max++;
719 c->openmax = 1;
720 } else
721 c->openmax = (a->openmax || b->openmin);
722 } else {
723 c->max = UINT_MAX;
724 c->openmax = 0;
725 }
726 c->integer = 0;
727}
728
729/**
730 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200731 * @a: dividend 1
732 * @b: dividend 2
733 * @k: divisor (as integer)
734 * @c: result
735 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * c = a * b / k
737 *
738 * Returns non-zero if the value is changed, zero if not changed.
739 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100740void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
741 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 unsigned int r;
744 if (a->empty || b->empty) {
745 snd_interval_none(c);
746 return;
747 }
748 c->empty = 0;
749 c->min = muldiv32(a->min, b->min, k, &r);
750 c->openmin = (r || a->openmin || b->openmin);
751 c->max = muldiv32(a->max, b->max, k, &r);
752 if (r) {
753 c->max++;
754 c->openmax = 1;
755 } else
756 c->openmax = (a->openmax || b->openmax);
757 c->integer = 0;
758}
759
760/**
761 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200762 * @a: dividend 1
763 * @k: dividend 2 (as integer)
764 * @b: divisor
765 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *
767 * c = a * k / b
768 *
769 * Returns non-zero if the value is changed, zero if not changed.
770 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100771void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
772 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 unsigned int r;
775 if (a->empty || b->empty) {
776 snd_interval_none(c);
777 return;
778 }
779 c->empty = 0;
780 c->min = muldiv32(a->min, k, b->max, &r);
781 c->openmin = (r || a->openmin || b->openmax);
782 if (b->min > 0) {
783 c->max = muldiv32(a->max, k, b->min, &r);
784 if (r) {
785 c->max++;
786 c->openmax = 1;
787 } else
788 c->openmax = (a->openmax || b->openmin);
789 } else {
790 c->max = UINT_MAX;
791 c->openmax = 0;
792 }
793 c->integer = 0;
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/* ---- */
797
798
799/**
800 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200801 * @i: interval to refine
802 * @rats_count: number of ratnum_t
803 * @rats: ratnum_t array
804 * @nump: pointer to store the resultant numerator
805 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100807 * Return: Positive if the value is changed, zero if it's not changed, or a
808 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100810int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100811 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100812 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100814 unsigned int best_num, best_den;
815 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100817 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100819 unsigned int result_num, result_den;
820 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 best_num = best_den = best_diff = 0;
823 for (k = 0; k < rats_count; ++k) {
824 unsigned int num = rats[k].num;
825 unsigned int den;
826 unsigned int q = i->min;
827 int diff;
828 if (q == 0)
829 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100830 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (den < rats[k].den_min)
832 continue;
833 if (den > rats[k].den_max)
834 den = rats[k].den_max;
835 else {
836 unsigned int r;
837 r = (den - rats[k].den_min) % rats[k].den_step;
838 if (r != 0)
839 den -= r;
840 }
841 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100842 if (diff < 0)
843 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (best_num == 0 ||
845 diff * best_den < best_diff * den) {
846 best_diff = diff;
847 best_den = den;
848 best_num = num;
849 }
850 }
851 if (best_den == 0) {
852 i->empty = 1;
853 return -EINVAL;
854 }
855 t.min = div_down(best_num, best_den);
856 t.openmin = !!(best_num % best_den);
857
Krzysztof Helt8374e242009-12-21 17:07:08 +0100858 result_num = best_num;
859 result_diff = best_diff;
860 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 best_num = best_den = best_diff = 0;
862 for (k = 0; k < rats_count; ++k) {
863 unsigned int num = rats[k].num;
864 unsigned int den;
865 unsigned int q = i->max;
866 int diff;
867 if (q == 0) {
868 i->empty = 1;
869 return -EINVAL;
870 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100871 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (den > rats[k].den_max)
873 continue;
874 if (den < rats[k].den_min)
875 den = rats[k].den_min;
876 else {
877 unsigned int r;
878 r = (den - rats[k].den_min) % rats[k].den_step;
879 if (r != 0)
880 den += rats[k].den_step - r;
881 }
882 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100883 if (diff < 0)
884 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (best_num == 0 ||
886 diff * best_den < best_diff * den) {
887 best_diff = diff;
888 best_den = den;
889 best_num = num;
890 }
891 }
892 if (best_den == 0) {
893 i->empty = 1;
894 return -EINVAL;
895 }
896 t.max = div_up(best_num, best_den);
897 t.openmax = !!(best_num % best_den);
898 t.integer = 0;
899 err = snd_interval_refine(i, &t);
900 if (err < 0)
901 return err;
902
903 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100904 if (best_diff * result_den < result_diff * best_den) {
905 result_num = best_num;
906 result_den = best_den;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100909 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100911 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 return err;
914}
915
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200916EXPORT_SYMBOL(snd_interval_ratnum);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
919 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200920 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 * @rats_count: number of struct ratden
922 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200923 * @nump: pointer to store the resultant numerator
924 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100926 * Return: Positive if the value is changed, zero if it's not changed, or a
927 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100929static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100930 unsigned int rats_count,
931 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned int *nump, unsigned int *denp)
933{
934 unsigned int best_num, best_diff, best_den;
935 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100936 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int err;
938
939 best_num = best_den = best_diff = 0;
940 for (k = 0; k < rats_count; ++k) {
941 unsigned int num;
942 unsigned int den = rats[k].den;
943 unsigned int q = i->min;
944 int diff;
945 num = mul(q, den);
946 if (num > rats[k].num_max)
947 continue;
948 if (num < rats[k].num_min)
949 num = rats[k].num_max;
950 else {
951 unsigned int r;
952 r = (num - rats[k].num_min) % rats[k].num_step;
953 if (r != 0)
954 num += rats[k].num_step - r;
955 }
956 diff = num - q * den;
957 if (best_num == 0 ||
958 diff * best_den < best_diff * den) {
959 best_diff = diff;
960 best_den = den;
961 best_num = num;
962 }
963 }
964 if (best_den == 0) {
965 i->empty = 1;
966 return -EINVAL;
967 }
968 t.min = div_down(best_num, best_den);
969 t.openmin = !!(best_num % best_den);
970
971 best_num = best_den = best_diff = 0;
972 for (k = 0; k < rats_count; ++k) {
973 unsigned int num;
974 unsigned int den = rats[k].den;
975 unsigned int q = i->max;
976 int diff;
977 num = mul(q, den);
978 if (num < rats[k].num_min)
979 continue;
980 if (num > rats[k].num_max)
981 num = rats[k].num_max;
982 else {
983 unsigned int r;
984 r = (num - rats[k].num_min) % rats[k].num_step;
985 if (r != 0)
986 num -= r;
987 }
988 diff = q * den - num;
989 if (best_num == 0 ||
990 diff * best_den < best_diff * den) {
991 best_diff = diff;
992 best_den = den;
993 best_num = num;
994 }
995 }
996 if (best_den == 0) {
997 i->empty = 1;
998 return -EINVAL;
999 }
1000 t.max = div_up(best_num, best_den);
1001 t.openmax = !!(best_num % best_den);
1002 t.integer = 0;
1003 err = snd_interval_refine(i, &t);
1004 if (err < 0)
1005 return err;
1006
1007 if (snd_interval_single(i)) {
1008 if (nump)
1009 *nump = best_num;
1010 if (denp)
1011 *denp = best_den;
1012 }
1013 return err;
1014}
1015
1016/**
1017 * snd_interval_list - refine the interval value from the list
1018 * @i: the interval value to refine
1019 * @count: the number of elements in the list
1020 * @list: the value list
1021 * @mask: the bit-mask to evaluate
1022 *
1023 * Refines the interval value from the list.
1024 * When mask is non-zero, only the elements corresponding to bit 1 are
1025 * evaluated.
1026 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001027 * Return: Positive if the value is changed, zero if it's not changed, or a
1028 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
Mark Brown4af87a92012-03-14 19:48:43 +00001030int snd_interval_list(struct snd_interval *i, unsigned int count,
1031 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001034 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001035
1036 if (!count) {
1037 i->empty = 1;
1038 return -EINVAL;
1039 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001040 snd_interval_any(&list_range);
1041 list_range.min = UINT_MAX;
1042 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 for (k = 0; k < count; k++) {
1044 if (mask && !(mask & (1 << k)))
1045 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001046 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001048 list_range.min = min(list_range.min, list[k]);
1049 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001051 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001054EXPORT_SYMBOL(snd_interval_list);
1055
Peter Rosinf66f8982015-01-28 15:16:06 +01001056/**
1057 * snd_interval_ranges - refine the interval value from the list of ranges
1058 * @i: the interval value to refine
1059 * @count: the number of elements in the list of ranges
1060 * @ranges: the ranges list
1061 * @mask: the bit-mask to evaluate
1062 *
1063 * Refines the interval value from the list of ranges.
1064 * When mask is non-zero, only the elements corresponding to bit 1 are
1065 * evaluated.
1066 *
1067 * Return: Positive if the value is changed, zero if it's not changed, or a
1068 * negative error code.
1069 */
1070int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1071 const struct snd_interval *ranges, unsigned int mask)
1072{
1073 unsigned int k;
1074 struct snd_interval range_union;
1075 struct snd_interval range;
1076
1077 if (!count) {
1078 snd_interval_none(i);
1079 return -EINVAL;
1080 }
1081 snd_interval_any(&range_union);
1082 range_union.min = UINT_MAX;
1083 range_union.max = 0;
1084 for (k = 0; k < count; k++) {
1085 if (mask && !(mask & (1 << k)))
1086 continue;
1087 snd_interval_copy(&range, &ranges[k]);
1088 if (snd_interval_refine(&range, i) < 0)
1089 continue;
1090 if (snd_interval_empty(&range))
1091 continue;
1092
1093 if (range.min < range_union.min) {
1094 range_union.min = range.min;
1095 range_union.openmin = 1;
1096 }
1097 if (range.min == range_union.min && !range.openmin)
1098 range_union.openmin = 0;
1099 if (range.max > range_union.max) {
1100 range_union.max = range.max;
1101 range_union.openmax = 1;
1102 }
1103 if (range.max == range_union.max && !range.openmax)
1104 range_union.openmax = 0;
1105 }
1106 return snd_interval_refine(i, &range_union);
1107}
1108EXPORT_SYMBOL(snd_interval_ranges);
1109
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001110static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 unsigned int n;
1113 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001114 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (n != 0 || i->openmin) {
1116 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001117 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 changed = 1;
1119 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001120 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (n != 0 || i->openmax) {
1122 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001123 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 changed = 1;
1125 }
1126 if (snd_interval_checkempty(i)) {
1127 i->empty = 1;
1128 return -EINVAL;
1129 }
1130 return changed;
1131}
1132
1133/* Info constraints helpers */
1134
1135/**
1136 * snd_pcm_hw_rule_add - add the hw-constraint rule
1137 * @runtime: the pcm runtime instance
1138 * @cond: condition bits
1139 * @var: the variable to evaluate
1140 * @func: the evaluation function
1141 * @private: the private data pointer passed to function
1142 * @dep: the dependent variables
1143 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001144 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001146int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int var,
1148 snd_pcm_hw_rule_func_t func, void *private,
1149 int dep, ...)
1150{
Takashi Iwai877211f2005-11-17 13:59:38 +01001151 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1152 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 unsigned int k;
1154 va_list args;
1155 va_start(args, dep);
1156 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned int new_rules = constrs->rules_all + 16;
1159 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001160 if (!new) {
1161 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (constrs->rules) {
1165 memcpy(new, constrs->rules,
1166 constrs->rules_num * sizeof(*c));
1167 kfree(constrs->rules);
1168 }
1169 constrs->rules = new;
1170 constrs->rules_all = new_rules;
1171 }
1172 c = &constrs->rules[constrs->rules_num];
1173 c->cond = cond;
1174 c->func = func;
1175 c->var = var;
1176 c->private = private;
1177 k = 0;
1178 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001179 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1180 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001181 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 c->deps[k++] = dep;
1184 if (dep < 0)
1185 break;
1186 dep = va_arg(args, int);
1187 }
1188 constrs->rules_num++;
1189 va_end(args);
1190 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001193EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001196 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001197 * @runtime: PCM runtime instance
1198 * @var: hw_params variable to apply the mask
1199 * @mask: the bitmap mask
1200 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001201 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001202 *
1203 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001205int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 u_int32_t mask)
1207{
Takashi Iwai877211f2005-11-17 13:59:38 +01001208 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1209 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 *maskp->bits &= mask;
1211 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1212 if (*maskp->bits == 0)
1213 return -EINVAL;
1214 return 0;
1215}
1216
1217/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001218 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001219 * @runtime: PCM runtime instance
1220 * @var: hw_params variable to apply the mask
1221 * @mask: the 64bit bitmap mask
1222 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001223 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001224 *
1225 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001227int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 u_int64_t mask)
1229{
Takashi Iwai877211f2005-11-17 13:59:38 +01001230 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1231 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 maskp->bits[0] &= (u_int32_t)mask;
1233 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1234 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1235 if (! maskp->bits[0] && ! maskp->bits[1])
1236 return -EINVAL;
1237 return 0;
1238}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001239EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001242 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001243 * @runtime: PCM runtime instance
1244 * @var: hw_params variable to apply the integer constraint
1245 *
1246 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001247 *
1248 * Return: Positive if the value is changed, zero if it's not changed, or a
1249 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001251int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwai877211f2005-11-17 13:59:38 +01001253 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return snd_interval_setinteger(constrs_interval(constrs, var));
1255}
1256
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001257EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001260 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001261 * @runtime: PCM runtime instance
1262 * @var: hw_params variable to apply the range
1263 * @min: the minimal value
1264 * @max: the maximal value
1265 *
1266 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001267 *
1268 * Return: Positive if the value is changed, zero if it's not changed, or a
1269 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001271int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 unsigned int min, unsigned int max)
1273{
Takashi Iwai877211f2005-11-17 13:59:38 +01001274 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1275 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 t.min = min;
1277 t.max = max;
1278 t.openmin = t.openmax = 0;
1279 t.integer = 0;
1280 return snd_interval_refine(constrs_interval(constrs, var), &t);
1281}
1282
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001283EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1284
Takashi Iwai877211f2005-11-17 13:59:38 +01001285static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1286 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Takashi Iwai877211f2005-11-17 13:59:38 +01001288 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1290}
1291
1292
1293/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001294 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001295 * @runtime: PCM runtime instance
1296 * @cond: condition bits
1297 * @var: hw_params variable to apply the list constraint
1298 * @l: list
1299 *
1300 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001301 *
1302 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001304int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 unsigned int cond,
1306 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001307 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001310 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 var, -1);
1312}
1313
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001314EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1315
Peter Rosinf66f8982015-01-28 15:16:06 +01001316static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1317 struct snd_pcm_hw_rule *rule)
1318{
1319 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1320 return snd_interval_ranges(hw_param_interval(params, rule->var),
1321 r->count, r->ranges, r->mask);
1322}
1323
1324
1325/**
1326 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1327 * @runtime: PCM runtime instance
1328 * @cond: condition bits
1329 * @var: hw_params variable to apply the list of range constraints
1330 * @r: ranges
1331 *
1332 * Apply the list of range constraints to an interval parameter.
1333 *
1334 * Return: Zero if successful, or a negative error code on failure.
1335 */
1336int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1337 unsigned int cond,
1338 snd_pcm_hw_param_t var,
1339 const struct snd_pcm_hw_constraint_ranges *r)
1340{
1341 return snd_pcm_hw_rule_add(runtime, cond, var,
1342 snd_pcm_hw_rule_ranges, (void *)r,
1343 var, -1);
1344}
1345EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1346
Takashi Iwai877211f2005-11-17 13:59:38 +01001347static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1348 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001350 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 unsigned int num = 0, den = 0;
1352 int err;
1353 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1354 r->nrats, r->rats, &num, &den);
1355 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1356 params->rate_num = num;
1357 params->rate_den = den;
1358 }
1359 return err;
1360}
1361
1362/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001363 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001364 * @runtime: PCM runtime instance
1365 * @cond: condition bits
1366 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001367 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001368 *
1369 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001371int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unsigned int cond,
1373 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001374 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001377 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 var, -1);
1379}
1380
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001381EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1382
Takashi Iwai877211f2005-11-17 13:59:38 +01001383static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1384 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001386 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 unsigned int num = 0, den = 0;
1388 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1389 r->nrats, r->rats, &num, &den);
1390 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1391 params->rate_num = num;
1392 params->rate_den = den;
1393 }
1394 return err;
1395}
1396
1397/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001398 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001399 * @runtime: PCM runtime instance
1400 * @cond: condition bits
1401 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001402 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001403 *
1404 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001406int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned int cond,
1408 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001409 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001412 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 var, -1);
1414}
1415
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001416EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1417
Takashi Iwai877211f2005-11-17 13:59:38 +01001418static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1419 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 unsigned int l = (unsigned long) rule->private;
1422 int width = l & 0xffff;
1423 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001424 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001425
1426 if (!snd_interval_single(i))
1427 return 0;
1428
1429 if ((snd_interval_value(i) == width) ||
1430 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001431 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434}
1435
1436/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001437 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001438 * @runtime: PCM runtime instance
1439 * @cond: condition bits
1440 * @width: sample bits width
1441 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001442 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001443 * This constraint will set the number of most significant bits (msbits) if a
1444 * sample format with the specified width has been select. If width is set to 0
1445 * the msbits will be set for any sample format with a width larger than the
1446 * specified msbits.
1447 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001448 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001450int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 unsigned int cond,
1452 unsigned int width,
1453 unsigned int msbits)
1454{
1455 unsigned long l = (msbits << 16) | width;
1456 return snd_pcm_hw_rule_add(runtime, cond, -1,
1457 snd_pcm_hw_rule_msbits,
1458 (void*) l,
1459 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1460}
1461
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001462EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1463
Takashi Iwai877211f2005-11-17 13:59:38 +01001464static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1465 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001468 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001472 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001473 * @runtime: PCM runtime instance
1474 * @cond: condition bits
1475 * @var: hw_params variable to apply the step constraint
1476 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001477 *
1478 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001480int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 unsigned int cond,
1482 snd_pcm_hw_param_t var,
1483 unsigned long step)
1484{
1485 return snd_pcm_hw_rule_add(runtime, cond, var,
1486 snd_pcm_hw_rule_step, (void *) step,
1487 var, -1);
1488}
1489
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001490EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1491
Takashi Iwai877211f2005-11-17 13:59:38 +01001492static 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 -07001493{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001494 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1496 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1497 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1498 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1499 };
1500 return snd_interval_list(hw_param_interval(params, rule->var),
1501 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1502}
1503
1504/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001505 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001506 * @runtime: PCM runtime instance
1507 * @cond: condition bits
1508 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001509 *
1510 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001512int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 unsigned int cond,
1514 snd_pcm_hw_param_t var)
1515{
1516 return snd_pcm_hw_rule_add(runtime, cond, var,
1517 snd_pcm_hw_rule_pow2, NULL,
1518 var, -1);
1519}
1520
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001521EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1522
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001523static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1524 struct snd_pcm_hw_rule *rule)
1525{
1526 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1527 struct snd_interval *rate;
1528
1529 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1530 return snd_interval_list(rate, 1, &base_rate, 0);
1531}
1532
1533/**
1534 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1535 * @runtime: PCM runtime instance
1536 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001537 *
1538 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001539 */
1540int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1541 unsigned int base_rate)
1542{
1543 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1544 SNDRV_PCM_HW_PARAM_RATE,
1545 snd_pcm_hw_rule_noresample_func,
1546 (void *)(uintptr_t)base_rate,
1547 SNDRV_PCM_HW_PARAM_RATE, -1);
1548}
1549EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1550
Takashi Iwai877211f2005-11-17 13:59:38 +01001551static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001552 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 if (hw_is_mask(var)) {
1555 snd_mask_any(hw_param_mask(params, var));
1556 params->cmask |= 1 << var;
1557 params->rmask |= 1 << var;
1558 return;
1559 }
1560 if (hw_is_interval(var)) {
1561 snd_interval_any(hw_param_interval(params, var));
1562 params->cmask |= 1 << var;
1563 params->rmask |= 1 << var;
1564 return;
1565 }
1566 snd_BUG();
1567}
1568
Takashi Iwai877211f2005-11-17 13:59:38 +01001569void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 unsigned int k;
1572 memset(params, 0, sizeof(*params));
1573 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1574 _snd_pcm_hw_param_any(params, k);
1575 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1576 _snd_pcm_hw_param_any(params, k);
1577 params->info = ~0U;
1578}
1579
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001580EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001583 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001584 * @params: the hw_params instance
1585 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001586 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001588 * Return: The value for field @var if it's fixed in configuration space
1589 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001591int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1592 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001595 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!snd_mask_single(mask))
1597 return -EINVAL;
1598 if (dir)
1599 *dir = 0;
1600 return snd_mask_value(mask);
1601 }
1602 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001603 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (!snd_interval_single(i))
1605 return -EINVAL;
1606 if (dir)
1607 *dir = i->openmin;
1608 return snd_interval_value(i);
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return -EINVAL;
1611}
1612
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001613EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Takashi Iwai877211f2005-11-17 13:59:38 +01001615void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 snd_pcm_hw_param_t var)
1617{
1618 if (hw_is_mask(var)) {
1619 snd_mask_none(hw_param_mask(params, var));
1620 params->cmask |= 1 << var;
1621 params->rmask |= 1 << var;
1622 } else if (hw_is_interval(var)) {
1623 snd_interval_none(hw_param_interval(params, var));
1624 params->cmask |= 1 << var;
1625 params->rmask |= 1 << var;
1626 } else {
1627 snd_BUG();
1628 }
1629}
1630
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001631EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Takashi Iwai877211f2005-11-17 13:59:38 +01001633static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001634 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 int changed;
1637 if (hw_is_mask(var))
1638 changed = snd_mask_refine_first(hw_param_mask(params, var));
1639 else if (hw_is_interval(var))
1640 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001641 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (changed) {
1644 params->cmask |= 1 << var;
1645 params->rmask |= 1 << var;
1646 }
1647 return changed;
1648}
1649
1650
1651/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001652 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001653 * @pcm: PCM instance
1654 * @params: the hw_params instance
1655 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001656 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001658 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001660 *
1661 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001663int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1664 struct snd_pcm_hw_params *params,
1665 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int changed = _snd_pcm_hw_param_first(params, var);
1668 if (changed < 0)
1669 return changed;
1670 if (params->rmask) {
1671 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001672 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675 return snd_pcm_hw_param_value(params, var, dir);
1676}
1677
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001678EXPORT_SYMBOL(snd_pcm_hw_param_first);
1679
Takashi Iwai877211f2005-11-17 13:59:38 +01001680static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001681 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 int changed;
1684 if (hw_is_mask(var))
1685 changed = snd_mask_refine_last(hw_param_mask(params, var));
1686 else if (hw_is_interval(var))
1687 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001688 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (changed) {
1691 params->cmask |= 1 << var;
1692 params->rmask |= 1 << var;
1693 }
1694 return changed;
1695}
1696
1697
1698/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001699 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001700 * @pcm: PCM instance
1701 * @params: the hw_params instance
1702 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001703 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001705 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001707 *
1708 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001710int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1711 struct snd_pcm_hw_params *params,
1712 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int changed = _snd_pcm_hw_param_last(params, var);
1715 if (changed < 0)
1716 return changed;
1717 if (params->rmask) {
1718 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai83da0242018-01-01 09:50:50 +01001719 if (err < 0)
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 return snd_pcm_hw_param_value(params, var, dir);
1723}
1724
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001725EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001728 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001729 * @pcm: PCM instance
1730 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001732 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * The configuration chosen is that obtained fixing in this order:
1734 * first access, first format, first subformat, min channels,
1735 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001736 *
1737 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001739int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1740 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001742 static int vars[] = {
1743 SNDRV_PCM_HW_PARAM_ACCESS,
1744 SNDRV_PCM_HW_PARAM_FORMAT,
1745 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1746 SNDRV_PCM_HW_PARAM_CHANNELS,
1747 SNDRV_PCM_HW_PARAM_RATE,
1748 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1749 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1750 SNDRV_PCM_HW_PARAM_TICK_TIME,
1751 -1
1752 };
1753 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001755 for (v = vars; *v != -1; v++) {
1756 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1757 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1758 else
1759 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001760 if (snd_BUG_ON(err < 0))
1761 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
1764}
1765
Takashi Iwai877211f2005-11-17 13:59:38 +01001766static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 void *arg)
1768{
Takashi Iwai877211f2005-11-17 13:59:38 +01001769 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 unsigned long flags;
1771 snd_pcm_stream_lock_irqsave(substream, flags);
1772 if (snd_pcm_running(substream) &&
1773 snd_pcm_update_hw_ptr(substream) >= 0)
1774 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001775 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001777 runtime->hw_ptr_wrap = 0;
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 snd_pcm_stream_unlock_irqrestore(substream, flags);
1780 return 0;
1781}
1782
Takashi Iwai877211f2005-11-17 13:59:38 +01001783static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 void *arg)
1785{
Takashi Iwai877211f2005-11-17 13:59:38 +01001786 struct snd_pcm_channel_info *info = arg;
1787 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int width;
1789 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1790 info->offset = -1;
1791 return 0;
1792 }
1793 width = snd_pcm_format_physical_width(runtime->format);
1794 if (width < 0)
1795 return width;
1796 info->offset = 0;
1797 switch (runtime->access) {
1798 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1799 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1800 info->first = info->channel * width;
1801 info->step = runtime->channels * width;
1802 break;
1803 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1804 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1805 {
1806 size_t size = runtime->dma_bytes / runtime->channels;
1807 info->first = info->channel * size * 8;
1808 info->step = width;
1809 break;
1810 }
1811 default:
1812 snd_BUG();
1813 break;
1814 }
1815 return 0;
1816}
1817
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001818static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1819 void *arg)
1820{
1821 struct snd_pcm_hw_params *params = arg;
1822 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001823 int channels;
1824 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001825
1826 params->fifo_size = substream->runtime->hw.fifo_size;
1827 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1828 format = params_format(params);
1829 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001830 frame_size = snd_pcm_format_size(format, channels);
1831 if (frame_size > 0)
1832 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001833 }
1834 return 0;
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/**
1838 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1839 * @substream: the pcm substream instance
1840 * @cmd: ioctl command
1841 * @arg: ioctl argument
1842 *
1843 * Processes the generic ioctl commands for PCM.
1844 * Can be passed as the ioctl callback for PCM ops.
1845 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001846 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001848int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 unsigned int cmd, void *arg)
1850{
1851 switch (cmd) {
1852 case SNDRV_PCM_IOCTL1_INFO:
1853 return 0;
1854 case SNDRV_PCM_IOCTL1_RESET:
1855 return snd_pcm_lib_ioctl_reset(substream, arg);
1856 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1857 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001858 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1859 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 return -ENXIO;
1862}
1863
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001864EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866/**
1867 * snd_pcm_period_elapsed - update the pcm status for the next period
1868 * @substream: the pcm substream instance
1869 *
1870 * This function is called from the interrupt handler when the
1871 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001872 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 *
1874 * Even if more than one periods have elapsed since the last call, you
1875 * have to call this only once.
1876 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001877void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Takashi Iwai877211f2005-11-17 13:59:38 +01001879 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 unsigned long flags;
1881
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001882 if (PCM_RUNTIME_CHECK(substream))
1883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 snd_pcm_stream_lock_irqsave(substream, flags);
1887 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001888 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto _end;
1890
Jie Yang90bbaf62015-10-16 17:57:46 +08001891#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (substream->timer_running)
1893 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001897 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001900EXPORT_SYMBOL(snd_pcm_period_elapsed);
1901
Takashi Iwai13075512008-01-08 18:08:14 +01001902/*
1903 * Wait until avail_min data becomes available
1904 * Returns a negative error code if any error occurs during operation.
1905 * The available space is stored on availp. When err = 0 and avail = 0
1906 * on the capture stream, it indicates the stream is in DRAINING state.
1907 */
David Dillow5daeba32010-06-27 00:13:20 +02001908static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001909 snd_pcm_uframes_t *availp)
1910{
1911 struct snd_pcm_runtime *runtime = substream->runtime;
1912 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1913 wait_queue_t wait;
1914 int err = 0;
1915 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001916 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001917
Arjan van de Ven763437a2011-09-15 08:49:25 +02001918 init_waitqueue_entry(&wait, current);
1919 set_current_state(TASK_INTERRUPTIBLE);
1920 add_wait_queue(&runtime->tsleep, &wait);
1921
Takashi Iwaif2b36142011-05-26 08:09:38 +02001922 if (runtime->no_period_wakeup)
1923 wait_time = MAX_SCHEDULE_TIMEOUT;
1924 else {
1925 wait_time = 10;
1926 if (runtime->rate) {
1927 long t = runtime->period_size * 2 / runtime->rate;
1928 wait_time = max(t, wait_time);
1929 }
1930 wait_time = msecs_to_jiffies(wait_time * 1000);
1931 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001932
Takashi Iwai13075512008-01-08 18:08:14 +01001933 for (;;) {
1934 if (signal_pending(current)) {
1935 err = -ERESTARTSYS;
1936 break;
1937 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001938
1939 /*
1940 * We need to check if space became available already
1941 * (and thus the wakeup happened already) first to close
1942 * the race of space already having become available.
1943 * This check must happen after been added to the waitqueue
1944 * and having current state be INTERRUPTIBLE.
1945 */
1946 if (is_playback)
1947 avail = snd_pcm_playback_avail(runtime);
1948 else
1949 avail = snd_pcm_capture_avail(runtime);
1950 if (avail >= runtime->twake)
1951 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001952 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001953
1954 tout = schedule_timeout(wait_time);
1955
Takashi Iwai13075512008-01-08 18:08:14 +01001956 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001957 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001958 switch (runtime->status->state) {
1959 case SNDRV_PCM_STATE_SUSPENDED:
1960 err = -ESTRPIPE;
1961 goto _endloop;
1962 case SNDRV_PCM_STATE_XRUN:
1963 err = -EPIPE;
1964 goto _endloop;
1965 case SNDRV_PCM_STATE_DRAINING:
1966 if (is_playback)
1967 err = -EPIPE;
1968 else
1969 avail = 0; /* indicate draining */
1970 goto _endloop;
1971 case SNDRV_PCM_STATE_OPEN:
1972 case SNDRV_PCM_STATE_SETUP:
1973 case SNDRV_PCM_STATE_DISCONNECTED:
1974 err = -EBADFD;
1975 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001976 case SNDRV_PCM_STATE_PAUSED:
1977 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001978 }
1979 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001980 pcm_dbg(substream->pcm,
1981 "%s write error (DMA or IRQ trouble?)\n",
1982 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001983 err = -EIO;
1984 break;
1985 }
Takashi Iwai13075512008-01-08 18:08:14 +01001986 }
1987 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001988 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001989 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001990 *availp = avail;
1991 return err;
1992}
1993
Takashi Iwai877211f2005-11-17 13:59:38 +01001994static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 unsigned int hwoff,
1996 unsigned long data, unsigned int off,
1997 snd_pcm_uframes_t frames)
1998{
Takashi Iwai877211f2005-11-17 13:59:38 +01001999 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int err;
2001 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2002 if (substream->ops->copy) {
2003 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2004 return err;
2005 } else {
2006 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2008 return -EFAULT;
2009 }
2010 return 0;
2011}
2012
Takashi Iwai877211f2005-11-17 13:59:38 +01002013typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 unsigned long data, unsigned int off,
2015 snd_pcm_uframes_t size);
2016
Takashi Iwai877211f2005-11-17 13:59:38 +01002017static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 unsigned long data,
2019 snd_pcm_uframes_t size,
2020 int nonblock,
2021 transfer_f transfer)
2022{
Takashi Iwai877211f2005-11-17 13:59:38 +01002023 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 snd_pcm_uframes_t xfer = 0;
2025 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002026 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int err = 0;
2028
2029 if (size == 0)
2030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 snd_pcm_stream_lock_irq(substream);
2033 switch (runtime->status->state) {
2034 case SNDRV_PCM_STATE_PREPARED:
2035 case SNDRV_PCM_STATE_RUNNING:
2036 case SNDRV_PCM_STATE_PAUSED:
2037 break;
2038 case SNDRV_PCM_STATE_XRUN:
2039 err = -EPIPE;
2040 goto _end_unlock;
2041 case SNDRV_PCM_STATE_SUSPENDED:
2042 err = -ESTRPIPE;
2043 goto _end_unlock;
2044 default:
2045 err = -EBADFD;
2046 goto _end_unlock;
2047 }
2048
David Dillow5daeba32010-06-27 00:13:20 +02002049 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002050 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2051 snd_pcm_update_hw_ptr(substream);
2052 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 while (size > 0) {
2054 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002056 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (nonblock) {
2058 err = -EAGAIN;
2059 goto _end_unlock;
2060 }
David Dillow5daeba32010-06-27 00:13:20 +02002061 runtime->twake = min_t(snd_pcm_uframes_t, size,
2062 runtime->control->avail_min ? : 1);
2063 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002064 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 frames = size > avail ? avail : size;
2068 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2069 if (frames > cont)
2070 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002071 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002072 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002073 snd_pcm_stream_unlock_irq(substream);
2074 return -EINVAL;
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 appl_ptr = runtime->control->appl_ptr;
2077 appl_ofs = appl_ptr % runtime->buffer_size;
2078 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002079 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002081 if (err < 0)
2082 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 switch (runtime->status->state) {
2084 case SNDRV_PCM_STATE_XRUN:
2085 err = -EPIPE;
2086 goto _end_unlock;
2087 case SNDRV_PCM_STATE_SUSPENDED:
2088 err = -ESTRPIPE;
2089 goto _end_unlock;
2090 default:
2091 break;
2092 }
2093 appl_ptr += frames;
2094 if (appl_ptr >= runtime->boundary)
2095 appl_ptr -= runtime->boundary;
2096 runtime->control->appl_ptr = appl_ptr;
2097 if (substream->ops->ack)
2098 substream->ops->ack(substream);
2099
2100 offset += frames;
2101 size -= frames;
2102 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002103 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2105 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2106 err = snd_pcm_start(substream);
2107 if (err < 0)
2108 goto _end_unlock;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002112 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002113 if (xfer > 0 && err >= 0)
2114 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2117}
2118
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002119/* sanity-check for read/write methods */
2120static int pcm_sanity_check(struct snd_pcm_substream *substream)
2121{
2122 struct snd_pcm_runtime *runtime;
2123 if (PCM_RUNTIME_CHECK(substream))
2124 return -ENXIO;
2125 runtime = substream->runtime;
2126 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2127 return -EINVAL;
2128 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2129 return -EBADFD;
2130 return 0;
2131}
2132
Takashi Iwai877211f2005-11-17 13:59:38 +01002133snd_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 -07002134{
Takashi Iwai877211f2005-11-17 13:59:38 +01002135 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002137 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002139 err = pcm_sanity_check(substream);
2140 if (err < 0)
2141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002143 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2146 runtime->channels > 1)
2147 return -EINVAL;
2148 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2149 snd_pcm_lib_write_transfer);
2150}
2151
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002152EXPORT_SYMBOL(snd_pcm_lib_write);
2153
Takashi Iwai877211f2005-11-17 13:59:38 +01002154static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 unsigned int hwoff,
2156 unsigned long data, unsigned int off,
2157 snd_pcm_uframes_t frames)
2158{
Takashi Iwai877211f2005-11-17 13:59:38 +01002159 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int err;
2161 void __user **bufs = (void __user **)data;
2162 int channels = runtime->channels;
2163 int c;
2164 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002165 if (snd_BUG_ON(!substream->ops->silence))
2166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 for (c = 0; c < channels; ++c, ++bufs) {
2168 if (*bufs == NULL) {
2169 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2170 return err;
2171 } else {
2172 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2173 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2174 return err;
2175 }
2176 }
2177 } else {
2178 /* default transfer behaviour */
2179 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 for (c = 0; c < channels; ++c, ++bufs) {
2181 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2182 if (*bufs == NULL) {
2183 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2184 } else {
2185 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2186 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2187 return -EFAULT;
2188 }
2189 }
2190 }
2191 return 0;
2192}
2193
Takashi Iwai877211f2005-11-17 13:59:38 +01002194snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 void __user **bufs,
2196 snd_pcm_uframes_t frames)
2197{
Takashi Iwai877211f2005-11-17 13:59:38 +01002198 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002200 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002202 err = pcm_sanity_check(substream);
2203 if (err < 0)
2204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002206 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2209 return -EINVAL;
2210 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2211 nonblock, snd_pcm_lib_writev_transfer);
2212}
2213
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002214EXPORT_SYMBOL(snd_pcm_lib_writev);
2215
Takashi Iwai877211f2005-11-17 13:59:38 +01002216static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 unsigned int hwoff,
2218 unsigned long data, unsigned int off,
2219 snd_pcm_uframes_t frames)
2220{
Takashi Iwai877211f2005-11-17 13:59:38 +01002221 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int err;
2223 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2224 if (substream->ops->copy) {
2225 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2226 return err;
2227 } else {
2228 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2230 return -EFAULT;
2231 }
2232 return 0;
2233}
2234
Takashi Iwai877211f2005-11-17 13:59:38 +01002235static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 unsigned long data,
2237 snd_pcm_uframes_t size,
2238 int nonblock,
2239 transfer_f transfer)
2240{
Takashi Iwai877211f2005-11-17 13:59:38 +01002241 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 snd_pcm_uframes_t xfer = 0;
2243 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002244 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 int err = 0;
2246
2247 if (size == 0)
2248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 snd_pcm_stream_lock_irq(substream);
2251 switch (runtime->status->state) {
2252 case SNDRV_PCM_STATE_PREPARED:
2253 if (size >= runtime->start_threshold) {
2254 err = snd_pcm_start(substream);
2255 if (err < 0)
2256 goto _end_unlock;
2257 }
2258 break;
2259 case SNDRV_PCM_STATE_DRAINING:
2260 case SNDRV_PCM_STATE_RUNNING:
2261 case SNDRV_PCM_STATE_PAUSED:
2262 break;
2263 case SNDRV_PCM_STATE_XRUN:
2264 err = -EPIPE;
2265 goto _end_unlock;
2266 case SNDRV_PCM_STATE_SUSPENDED:
2267 err = -ESTRPIPE;
2268 goto _end_unlock;
2269 default:
2270 err = -EBADFD;
2271 goto _end_unlock;
2272 }
2273
David Dillow5daeba32010-06-27 00:13:20 +02002274 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002275 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2276 snd_pcm_update_hw_ptr(substream);
2277 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 while (size > 0) {
2279 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002281 if (!avail) {
2282 if (runtime->status->state ==
2283 SNDRV_PCM_STATE_DRAINING) {
2284 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto _end_unlock;
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (nonblock) {
2288 err = -EAGAIN;
2289 goto _end_unlock;
2290 }
David Dillow5daeba32010-06-27 00:13:20 +02002291 runtime->twake = min_t(snd_pcm_uframes_t, size,
2292 runtime->control->avail_min ? : 1);
2293 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002294 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002296 if (!avail)
2297 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 frames = size > avail ? avail : size;
2300 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2301 if (frames > cont)
2302 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002303 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002304 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002305 snd_pcm_stream_unlock_irq(substream);
2306 return -EINVAL;
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 appl_ptr = runtime->control->appl_ptr;
2309 appl_ofs = appl_ptr % runtime->buffer_size;
2310 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002311 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002313 if (err < 0)
2314 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 switch (runtime->status->state) {
2316 case SNDRV_PCM_STATE_XRUN:
2317 err = -EPIPE;
2318 goto _end_unlock;
2319 case SNDRV_PCM_STATE_SUSPENDED:
2320 err = -ESTRPIPE;
2321 goto _end_unlock;
2322 default:
2323 break;
2324 }
2325 appl_ptr += frames;
2326 if (appl_ptr >= runtime->boundary)
2327 appl_ptr -= runtime->boundary;
2328 runtime->control->appl_ptr = appl_ptr;
2329 if (substream->ops->ack)
2330 substream->ops->ack(substream);
2331
2332 offset += frames;
2333 size -= frames;
2334 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002335 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002338 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002339 if (xfer > 0 && err >= 0)
2340 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2343}
2344
Takashi Iwai877211f2005-11-17 13:59:38 +01002345snd_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 -07002346{
Takashi Iwai877211f2005-11-17 13:59:38 +01002347 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002349 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002351 err = pcm_sanity_check(substream);
2352 if (err < 0)
2353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002355 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2357 return -EINVAL;
2358 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2359}
2360
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002361EXPORT_SYMBOL(snd_pcm_lib_read);
2362
Takashi Iwai877211f2005-11-17 13:59:38 +01002363static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 unsigned int hwoff,
2365 unsigned long data, unsigned int off,
2366 snd_pcm_uframes_t frames)
2367{
Takashi Iwai877211f2005-11-17 13:59:38 +01002368 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 int err;
2370 void __user **bufs = (void __user **)data;
2371 int channels = runtime->channels;
2372 int c;
2373 if (substream->ops->copy) {
2374 for (c = 0; c < channels; ++c, ++bufs) {
2375 char __user *buf;
2376 if (*bufs == NULL)
2377 continue;
2378 buf = *bufs + samples_to_bytes(runtime, off);
2379 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2380 return err;
2381 }
2382 } else {
2383 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 for (c = 0; c < channels; ++c, ++bufs) {
2385 char *hwbuf;
2386 char __user *buf;
2387 if (*bufs == NULL)
2388 continue;
2389
2390 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2391 buf = *bufs + samples_to_bytes(runtime, off);
2392 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2393 return -EFAULT;
2394 }
2395 }
2396 return 0;
2397}
2398
Takashi Iwai877211f2005-11-17 13:59:38 +01002399snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 void __user **bufs,
2401 snd_pcm_uframes_t frames)
2402{
Takashi Iwai877211f2005-11-17 13:59:38 +01002403 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002405 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002407 err = pcm_sanity_check(substream);
2408 if (err < 0)
2409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2412 return -EBADFD;
2413
Takashi Iwai0df63e42006-04-28 15:13:41 +02002414 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2416 return -EINVAL;
2417 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002421
2422/*
2423 * standard channel mapping helpers
2424 */
2425
2426/* default channel maps for multi-channel playbacks, up to 8 channels */
2427const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2428 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002429 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002430 { .channels = 2,
2431 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2432 { .channels = 4,
2433 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2434 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2435 { .channels = 6,
2436 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2437 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2438 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2439 { .channels = 8,
2440 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2441 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2442 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2443 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2444 { }
2445};
2446EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2447
2448/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2449const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2450 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002451 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002452 { .channels = 2,
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2454 { .channels = 4,
2455 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2456 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2457 { .channels = 6,
2458 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2459 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2460 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2461 { .channels = 8,
2462 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2463 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2464 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2465 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2466 { }
2467};
2468EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2469
2470static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2471{
2472 if (ch > info->max_channels)
2473 return false;
2474 return !info->channel_mask || (info->channel_mask & (1U << ch));
2475}
2476
2477static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_info *uinfo)
2479{
2480 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2481
2482 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2483 uinfo->count = 0;
2484 uinfo->count = info->max_channels;
2485 uinfo->value.integer.min = 0;
2486 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2487 return 0;
2488}
2489
2490/* get callback for channel map ctl element
2491 * stores the channel position firstly matching with the current channels
2492 */
2493static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2495{
2496 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2497 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2498 struct snd_pcm_substream *substream;
2499 const struct snd_pcm_chmap_elem *map;
2500
Takashi Iwai552a14a2017-06-14 16:20:32 +02002501 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002502 return -EINVAL;
2503 substream = snd_pcm_chmap_substream(info, idx);
2504 if (!substream)
2505 return -ENODEV;
2506 memset(ucontrol->value.integer.value, 0,
2507 sizeof(ucontrol->value.integer.value));
2508 if (!substream->runtime)
2509 return 0; /* no channels set */
2510 for (map = info->chmap; map->channels; map++) {
2511 int i;
2512 if (map->channels == substream->runtime->channels &&
2513 valid_chmap_channels(info, map->channels)) {
2514 for (i = 0; i < map->channels; i++)
2515 ucontrol->value.integer.value[i] = map->map[i];
2516 return 0;
2517 }
2518 }
2519 return -EINVAL;
2520}
2521
2522/* tlv callback for channel map ctl element
2523 * expands the pre-defined channel maps in a form of TLV
2524 */
2525static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2526 unsigned int size, unsigned int __user *tlv)
2527{
2528 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2529 const struct snd_pcm_chmap_elem *map;
2530 unsigned int __user *dst;
2531 int c, count = 0;
2532
Takashi Iwai552a14a2017-06-14 16:20:32 +02002533 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002534 return -EINVAL;
2535 if (size < 8)
2536 return -ENOMEM;
2537 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2538 return -EFAULT;
2539 size -= 8;
2540 dst = tlv + 2;
2541 for (map = info->chmap; map->channels; map++) {
2542 int chs_bytes = map->channels * 4;
2543 if (!valid_chmap_channels(info, map->channels))
2544 continue;
2545 if (size < 8)
2546 return -ENOMEM;
2547 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2548 put_user(chs_bytes, dst + 1))
2549 return -EFAULT;
2550 dst += 2;
2551 size -= 8;
2552 count += 8;
2553 if (size < chs_bytes)
2554 return -ENOMEM;
2555 size -= chs_bytes;
2556 count += chs_bytes;
2557 for (c = 0; c < map->channels; c++) {
2558 if (put_user(map->map[c], dst))
2559 return -EFAULT;
2560 dst++;
2561 }
2562 }
2563 if (put_user(count, tlv + 1))
2564 return -EFAULT;
2565 return 0;
2566}
2567
2568static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2569{
2570 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2571 info->pcm->streams[info->stream].chmap_kctl = NULL;
2572 kfree(info);
2573}
2574
2575/**
2576 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2577 * @pcm: the assigned PCM instance
2578 * @stream: stream direction
2579 * @chmap: channel map elements (for query)
2580 * @max_channels: the max number of channels for the stream
2581 * @private_value: the value passed to each kcontrol's private_value field
2582 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2583 *
2584 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002585 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002586 */
2587int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2588 const struct snd_pcm_chmap_elem *chmap,
2589 int max_channels,
2590 unsigned long private_value,
2591 struct snd_pcm_chmap **info_ret)
2592{
2593 struct snd_pcm_chmap *info;
2594 struct snd_kcontrol_new knew = {
2595 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2596 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002597 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2598 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2599 .info = pcm_chmap_ctl_info,
2600 .get = pcm_chmap_ctl_get,
2601 .tlv.c = pcm_chmap_ctl_tlv,
2602 };
2603 int err;
2604
Takashi Iwai8d879be2016-05-10 16:07:40 +02002605 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2606 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002607 info = kzalloc(sizeof(*info), GFP_KERNEL);
2608 if (!info)
2609 return -ENOMEM;
2610 info->pcm = pcm;
2611 info->stream = stream;
2612 info->chmap = chmap;
2613 info->max_channels = max_channels;
2614 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2615 knew.name = "Playback Channel Map";
2616 else
2617 knew.name = "Capture Channel Map";
2618 knew.device = pcm->device;
2619 knew.count = pcm->streams[stream].substream_count;
2620 knew.private_value = private_value;
2621 info->kctl = snd_ctl_new1(&knew, info);
2622 if (!info->kctl) {
2623 kfree(info);
2624 return -ENOMEM;
2625 }
2626 info->kctl->private_free = pcm_chmap_ctl_private_free;
2627 err = snd_ctl_add(pcm->card, info->kctl);
2628 if (err < 0)
2629 return err;
2630 pcm->streams[stream].chmap_kctl = info->kctl;
2631 if (info_ret)
2632 *info_ret = info;
2633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);