blob: c80d80e312e347f180784a929fe7492858d546f9 [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) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200581 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *r = 0;
583 return UINT_MAX;
584 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200585 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (n >= UINT_MAX) {
587 *r = 0;
588 return UINT_MAX;
589 }
590 return n;
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/**
594 * snd_interval_refine - refine the interval value of configurator
595 * @i: the interval value to refine
596 * @v: the interval value to refer to
597 *
598 * Refines the interval value with the reference value.
599 * The interval is changed to the range satisfying both intervals.
600 * The interval status (min, max, integer, etc.) are evaluated.
601 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100602 * Return: Positive if the value is changed, zero if it's not changed, or a
603 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100605int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200608 if (snd_BUG_ON(snd_interval_empty(i)))
609 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (i->min < v->min) {
611 i->min = v->min;
612 i->openmin = v->openmin;
613 changed = 1;
614 } else if (i->min == v->min && !i->openmin && v->openmin) {
615 i->openmin = 1;
616 changed = 1;
617 }
618 if (i->max > v->max) {
619 i->max = v->max;
620 i->openmax = v->openmax;
621 changed = 1;
622 } else if (i->max == v->max && !i->openmax && v->openmax) {
623 i->openmax = 1;
624 changed = 1;
625 }
626 if (!i->integer && v->integer) {
627 i->integer = 1;
628 changed = 1;
629 }
630 if (i->integer) {
631 if (i->openmin) {
632 i->min++;
633 i->openmin = 0;
634 }
635 if (i->openmax) {
636 i->max--;
637 i->openmax = 0;
638 }
639 } else if (!i->openmin && !i->openmax && i->min == i->max)
640 i->integer = 1;
641 if (snd_interval_checkempty(i)) {
642 snd_interval_none(i);
643 return -EINVAL;
644 }
645 return changed;
646}
647
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200648EXPORT_SYMBOL(snd_interval_refine);
649
Takashi Iwai877211f2005-11-17 13:59:38 +0100650static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200652 if (snd_BUG_ON(snd_interval_empty(i)))
653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (snd_interval_single(i))
655 return 0;
656 i->max = i->min;
657 i->openmax = i->openmin;
658 if (i->openmax)
659 i->max++;
660 return 1;
661}
662
Takashi Iwai877211f2005-11-17 13:59:38 +0100663static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200665 if (snd_BUG_ON(snd_interval_empty(i)))
666 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (snd_interval_single(i))
668 return 0;
669 i->min = i->max;
670 i->openmin = i->openmax;
671 if (i->openmin)
672 i->min--;
673 return 1;
674}
675
Takashi Iwai877211f2005-11-17 13:59:38 +0100676void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 if (a->empty || b->empty) {
679 snd_interval_none(c);
680 return;
681 }
682 c->empty = 0;
683 c->min = mul(a->min, b->min);
684 c->openmin = (a->openmin || b->openmin);
685 c->max = mul(a->max, b->max);
686 c->openmax = (a->openmax || b->openmax);
687 c->integer = (a->integer && b->integer);
688}
689
690/**
691 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200692 * @a: dividend
693 * @b: divisor
694 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * c = a / b
697 *
698 * Returns non-zero if the value is changed, zero if not changed.
699 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100700void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 unsigned int r;
703 if (a->empty || b->empty) {
704 snd_interval_none(c);
705 return;
706 }
707 c->empty = 0;
708 c->min = div32(a->min, b->max, &r);
709 c->openmin = (r || a->openmin || b->openmax);
710 if (b->min > 0) {
711 c->max = div32(a->max, b->min, &r);
712 if (r) {
713 c->max++;
714 c->openmax = 1;
715 } else
716 c->openmax = (a->openmax || b->openmin);
717 } else {
718 c->max = UINT_MAX;
719 c->openmax = 0;
720 }
721 c->integer = 0;
722}
723
724/**
725 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200726 * @a: dividend 1
727 * @b: dividend 2
728 * @k: divisor (as integer)
729 * @c: result
730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * c = a * b / k
732 *
733 * Returns non-zero if the value is changed, zero if not changed.
734 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100735void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
736 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 unsigned int r;
739 if (a->empty || b->empty) {
740 snd_interval_none(c);
741 return;
742 }
743 c->empty = 0;
744 c->min = muldiv32(a->min, b->min, k, &r);
745 c->openmin = (r || a->openmin || b->openmin);
746 c->max = muldiv32(a->max, b->max, k, &r);
747 if (r) {
748 c->max++;
749 c->openmax = 1;
750 } else
751 c->openmax = (a->openmax || b->openmax);
752 c->integer = 0;
753}
754
755/**
756 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200757 * @a: dividend 1
758 * @k: dividend 2 (as integer)
759 * @b: divisor
760 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
762 * c = a * k / b
763 *
764 * Returns non-zero if the value is changed, zero if not changed.
765 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100766void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
767 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 unsigned int r;
770 if (a->empty || b->empty) {
771 snd_interval_none(c);
772 return;
773 }
774 c->empty = 0;
775 c->min = muldiv32(a->min, k, b->max, &r);
776 c->openmin = (r || a->openmin || b->openmax);
777 if (b->min > 0) {
778 c->max = muldiv32(a->max, k, b->min, &r);
779 if (r) {
780 c->max++;
781 c->openmax = 1;
782 } else
783 c->openmax = (a->openmax || b->openmin);
784 } else {
785 c->max = UINT_MAX;
786 c->openmax = 0;
787 }
788 c->integer = 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* ---- */
792
793
794/**
795 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200796 * @i: interval to refine
797 * @rats_count: number of ratnum_t
798 * @rats: ratnum_t array
799 * @nump: pointer to store the resultant numerator
800 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100802 * Return: Positive if the value is changed, zero if it's not changed, or a
803 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100805int snd_interval_ratnum(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100806 unsigned int rats_count, const struct snd_ratnum *rats,
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100809 unsigned int best_num, best_den;
810 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100812 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100814 unsigned int result_num, result_den;
815 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 best_num = best_den = best_diff = 0;
818 for (k = 0; k < rats_count; ++k) {
819 unsigned int num = rats[k].num;
820 unsigned int den;
821 unsigned int q = i->min;
822 int diff;
823 if (q == 0)
824 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100825 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (den < rats[k].den_min)
827 continue;
828 if (den > rats[k].den_max)
829 den = rats[k].den_max;
830 else {
831 unsigned int r;
832 r = (den - rats[k].den_min) % rats[k].den_step;
833 if (r != 0)
834 den -= r;
835 }
836 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100837 if (diff < 0)
838 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (best_num == 0 ||
840 diff * best_den < best_diff * den) {
841 best_diff = diff;
842 best_den = den;
843 best_num = num;
844 }
845 }
846 if (best_den == 0) {
847 i->empty = 1;
848 return -EINVAL;
849 }
850 t.min = div_down(best_num, best_den);
851 t.openmin = !!(best_num % best_den);
852
Krzysztof Helt8374e242009-12-21 17:07:08 +0100853 result_num = best_num;
854 result_diff = best_diff;
855 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 best_num = best_den = best_diff = 0;
857 for (k = 0; k < rats_count; ++k) {
858 unsigned int num = rats[k].num;
859 unsigned int den;
860 unsigned int q = i->max;
861 int diff;
862 if (q == 0) {
863 i->empty = 1;
864 return -EINVAL;
865 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100866 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (den > rats[k].den_max)
868 continue;
869 if (den < rats[k].den_min)
870 den = rats[k].den_min;
871 else {
872 unsigned int r;
873 r = (den - rats[k].den_min) % rats[k].den_step;
874 if (r != 0)
875 den += rats[k].den_step - r;
876 }
877 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100878 if (diff < 0)
879 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (best_num == 0 ||
881 diff * best_den < best_diff * den) {
882 best_diff = diff;
883 best_den = den;
884 best_num = num;
885 }
886 }
887 if (best_den == 0) {
888 i->empty = 1;
889 return -EINVAL;
890 }
891 t.max = div_up(best_num, best_den);
892 t.openmax = !!(best_num % best_den);
893 t.integer = 0;
894 err = snd_interval_refine(i, &t);
895 if (err < 0)
896 return err;
897
898 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100899 if (best_diff * result_den < result_diff * best_den) {
900 result_num = best_num;
901 result_den = best_den;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100904 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100906 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 return err;
909}
910
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200911EXPORT_SYMBOL(snd_interval_ratnum);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/**
914 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200915 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100916 * @rats_count: number of struct ratden
917 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200918 * @nump: pointer to store the resultant numerator
919 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100921 * Return: Positive if the value is changed, zero if it's not changed, or a
922 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100924static int snd_interval_ratden(struct snd_interval *i,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +0100925 unsigned int rats_count,
926 const struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned int *nump, unsigned int *denp)
928{
929 unsigned int best_num, best_diff, best_den;
930 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100931 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int err;
933
934 best_num = best_den = best_diff = 0;
935 for (k = 0; k < rats_count; ++k) {
936 unsigned int num;
937 unsigned int den = rats[k].den;
938 unsigned int q = i->min;
939 int diff;
940 num = mul(q, den);
941 if (num > rats[k].num_max)
942 continue;
943 if (num < rats[k].num_min)
944 num = rats[k].num_max;
945 else {
946 unsigned int r;
947 r = (num - rats[k].num_min) % rats[k].num_step;
948 if (r != 0)
949 num += rats[k].num_step - r;
950 }
951 diff = num - q * den;
952 if (best_num == 0 ||
953 diff * best_den < best_diff * den) {
954 best_diff = diff;
955 best_den = den;
956 best_num = num;
957 }
958 }
959 if (best_den == 0) {
960 i->empty = 1;
961 return -EINVAL;
962 }
963 t.min = div_down(best_num, best_den);
964 t.openmin = !!(best_num % best_den);
965
966 best_num = best_den = best_diff = 0;
967 for (k = 0; k < rats_count; ++k) {
968 unsigned int num;
969 unsigned int den = rats[k].den;
970 unsigned int q = i->max;
971 int diff;
972 num = mul(q, den);
973 if (num < rats[k].num_min)
974 continue;
975 if (num > rats[k].num_max)
976 num = rats[k].num_max;
977 else {
978 unsigned int r;
979 r = (num - rats[k].num_min) % rats[k].num_step;
980 if (r != 0)
981 num -= r;
982 }
983 diff = q * den - num;
984 if (best_num == 0 ||
985 diff * best_den < best_diff * den) {
986 best_diff = diff;
987 best_den = den;
988 best_num = num;
989 }
990 }
991 if (best_den == 0) {
992 i->empty = 1;
993 return -EINVAL;
994 }
995 t.max = div_up(best_num, best_den);
996 t.openmax = !!(best_num % best_den);
997 t.integer = 0;
998 err = snd_interval_refine(i, &t);
999 if (err < 0)
1000 return err;
1001
1002 if (snd_interval_single(i)) {
1003 if (nump)
1004 *nump = best_num;
1005 if (denp)
1006 *denp = best_den;
1007 }
1008 return err;
1009}
1010
1011/**
1012 * snd_interval_list - refine the interval value from the list
1013 * @i: the interval value to refine
1014 * @count: the number of elements in the list
1015 * @list: the value list
1016 * @mask: the bit-mask to evaluate
1017 *
1018 * Refines the interval value from the list.
1019 * When mask is non-zero, only the elements corresponding to bit 1 are
1020 * evaluated.
1021 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001022 * Return: Positive if the value is changed, zero if it's not changed, or a
1023 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Mark Brown4af87a92012-03-14 19:48:43 +00001025int snd_interval_list(struct snd_interval *i, unsigned int count,
1026 const unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001029 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001030
1031 if (!count) {
1032 i->empty = 1;
1033 return -EINVAL;
1034 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001035 snd_interval_any(&list_range);
1036 list_range.min = UINT_MAX;
1037 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 for (k = 0; k < count; k++) {
1039 if (mask && !(mask & (1 << k)))
1040 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001041 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001043 list_range.min = min(list_range.min, list[k]);
1044 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001046 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001049EXPORT_SYMBOL(snd_interval_list);
1050
Peter Rosinf66f8982015-01-28 15:16:06 +01001051/**
1052 * snd_interval_ranges - refine the interval value from the list of ranges
1053 * @i: the interval value to refine
1054 * @count: the number of elements in the list of ranges
1055 * @ranges: the ranges list
1056 * @mask: the bit-mask to evaluate
1057 *
1058 * Refines the interval value from the list of ranges.
1059 * When mask is non-zero, only the elements corresponding to bit 1 are
1060 * evaluated.
1061 *
1062 * Return: Positive if the value is changed, zero if it's not changed, or a
1063 * negative error code.
1064 */
1065int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1066 const struct snd_interval *ranges, unsigned int mask)
1067{
1068 unsigned int k;
1069 struct snd_interval range_union;
1070 struct snd_interval range;
1071
1072 if (!count) {
1073 snd_interval_none(i);
1074 return -EINVAL;
1075 }
1076 snd_interval_any(&range_union);
1077 range_union.min = UINT_MAX;
1078 range_union.max = 0;
1079 for (k = 0; k < count; k++) {
1080 if (mask && !(mask & (1 << k)))
1081 continue;
1082 snd_interval_copy(&range, &ranges[k]);
1083 if (snd_interval_refine(&range, i) < 0)
1084 continue;
1085 if (snd_interval_empty(&range))
1086 continue;
1087
1088 if (range.min < range_union.min) {
1089 range_union.min = range.min;
1090 range_union.openmin = 1;
1091 }
1092 if (range.min == range_union.min && !range.openmin)
1093 range_union.openmin = 0;
1094 if (range.max > range_union.max) {
1095 range_union.max = range.max;
1096 range_union.openmax = 1;
1097 }
1098 if (range.max == range_union.max && !range.openmax)
1099 range_union.openmax = 0;
1100 }
1101 return snd_interval_refine(i, &range_union);
1102}
1103EXPORT_SYMBOL(snd_interval_ranges);
1104
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001105static int snd_interval_step(struct snd_interval *i, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 unsigned int n;
1108 int changed = 0;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001109 n = i->min % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (n != 0 || i->openmin) {
1111 i->min += step - n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001112 i->openmin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 changed = 1;
1114 }
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001115 n = i->max % step;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (n != 0 || i->openmax) {
1117 i->max -= n;
Clemens Ladischdf1e4712014-09-07 21:43:41 +02001118 i->openmax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 changed = 1;
1120 }
1121 if (snd_interval_checkempty(i)) {
1122 i->empty = 1;
1123 return -EINVAL;
1124 }
1125 return changed;
1126}
1127
1128/* Info constraints helpers */
1129
1130/**
1131 * snd_pcm_hw_rule_add - add the hw-constraint rule
1132 * @runtime: the pcm runtime instance
1133 * @cond: condition bits
1134 * @var: the variable to evaluate
1135 * @func: the evaluation function
1136 * @private: the private data pointer passed to function
1137 * @dep: the dependent variables
1138 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001139 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001141int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int var,
1143 snd_pcm_hw_rule_func_t func, void *private,
1144 int dep, ...)
1145{
Takashi Iwai877211f2005-11-17 13:59:38 +01001146 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1147 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned int k;
1149 va_list args;
1150 va_start(args, dep);
1151 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001152 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 unsigned int new_rules = constrs->rules_all + 16;
1154 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001155 if (!new) {
1156 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (constrs->rules) {
1160 memcpy(new, constrs->rules,
1161 constrs->rules_num * sizeof(*c));
1162 kfree(constrs->rules);
1163 }
1164 constrs->rules = new;
1165 constrs->rules_all = new_rules;
1166 }
1167 c = &constrs->rules[constrs->rules_num];
1168 c->cond = cond;
1169 c->func = func;
1170 c->var = var;
1171 c->private = private;
1172 k = 0;
1173 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001174 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1175 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001176 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 c->deps[k++] = dep;
1179 if (dep < 0)
1180 break;
1181 dep = va_arg(args, int);
1182 }
1183 constrs->rules_num++;
1184 va_end(args);
1185 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001188EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001191 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001192 * @runtime: PCM runtime instance
1193 * @var: hw_params variable to apply the mask
1194 * @mask: the bitmap mask
1195 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001196 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001197 *
1198 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001200int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 u_int32_t mask)
1202{
Takashi Iwai877211f2005-11-17 13:59:38 +01001203 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1204 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 *maskp->bits &= mask;
1206 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1207 if (*maskp->bits == 0)
1208 return -EINVAL;
1209 return 0;
1210}
1211
1212/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001213 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001214 * @runtime: PCM runtime instance
1215 * @var: hw_params variable to apply the mask
1216 * @mask: the 64bit bitmap mask
1217 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001218 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001219 *
1220 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001222int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 u_int64_t mask)
1224{
Takashi Iwai877211f2005-11-17 13:59:38 +01001225 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1226 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 maskp->bits[0] &= (u_int32_t)mask;
1228 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1229 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1230 if (! maskp->bits[0] && ! maskp->bits[1])
1231 return -EINVAL;
1232 return 0;
1233}
Mark Brown63a5d4c2014-02-20 12:13:52 +09001234EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001237 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001238 * @runtime: PCM runtime instance
1239 * @var: hw_params variable to apply the integer constraint
1240 *
1241 * Apply the constraint of integer to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001242 *
1243 * Return: Positive if the value is changed, zero if it's not changed, or a
1244 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001246int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return snd_interval_setinteger(constrs_interval(constrs, var));
1250}
1251
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001252EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001255 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001256 * @runtime: PCM runtime instance
1257 * @var: hw_params variable to apply the range
1258 * @min: the minimal value
1259 * @max: the maximal value
1260 *
1261 * Apply the min/max range constraint to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001262 *
1263 * Return: Positive if the value is changed, zero if it's not changed, or a
1264 * negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001266int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 unsigned int min, unsigned int max)
1268{
Takashi Iwai877211f2005-11-17 13:59:38 +01001269 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1270 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 t.min = min;
1272 t.max = max;
1273 t.openmin = t.openmax = 0;
1274 t.integer = 0;
1275 return snd_interval_refine(constrs_interval(constrs, var), &t);
1276}
1277
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001278EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1279
Takashi Iwai877211f2005-11-17 13:59:38 +01001280static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1281 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai877211f2005-11-17 13:59:38 +01001283 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1285}
1286
1287
1288/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001289 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001290 * @runtime: PCM runtime instance
1291 * @cond: condition bits
1292 * @var: hw_params variable to apply the list constraint
1293 * @l: list
1294 *
1295 * Apply the list of constraints to an interval parameter.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001296 *
1297 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001299int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 unsigned int cond,
1301 snd_pcm_hw_param_t var,
Mark Brown14641892012-07-05 12:15:01 +01001302 const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 return snd_pcm_hw_rule_add(runtime, cond, var,
Mark Brown14641892012-07-05 12:15:01 +01001305 snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 var, -1);
1307}
1308
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001309EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1310
Peter Rosinf66f8982015-01-28 15:16:06 +01001311static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1312 struct snd_pcm_hw_rule *rule)
1313{
1314 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1315 return snd_interval_ranges(hw_param_interval(params, rule->var),
1316 r->count, r->ranges, r->mask);
1317}
1318
1319
1320/**
1321 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1322 * @runtime: PCM runtime instance
1323 * @cond: condition bits
1324 * @var: hw_params variable to apply the list of range constraints
1325 * @r: ranges
1326 *
1327 * Apply the list of range constraints to an interval parameter.
1328 *
1329 * Return: Zero if successful, or a negative error code on failure.
1330 */
1331int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1332 unsigned int cond,
1333 snd_pcm_hw_param_t var,
1334 const struct snd_pcm_hw_constraint_ranges *r)
1335{
1336 return snd_pcm_hw_rule_add(runtime, cond, var,
1337 snd_pcm_hw_rule_ranges, (void *)r,
1338 var, -1);
1339}
1340EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1343 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001345 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned int num = 0, den = 0;
1347 int err;
1348 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1349 r->nrats, r->rats, &num, &den);
1350 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1351 params->rate_num = num;
1352 params->rate_den = den;
1353 }
1354 return err;
1355}
1356
1357/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001358 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001359 * @runtime: PCM runtime instance
1360 * @cond: condition bits
1361 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001362 * @r: struct snd_ratnums constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001363 *
1364 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001366int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 unsigned int cond,
1368 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001369 const struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001372 snd_pcm_hw_rule_ratnums, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 var, -1);
1374}
1375
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001376EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1377
Takashi Iwai877211f2005-11-17 13:59:38 +01001378static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1379 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001381 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned int num = 0, den = 0;
1383 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1384 r->nrats, r->rats, &num, &den);
1385 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1386 params->rate_num = num;
1387 params->rate_den = den;
1388 }
1389 return err;
1390}
1391
1392/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001393 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001394 * @runtime: PCM runtime instance
1395 * @cond: condition bits
1396 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001397 * @r: struct snd_ratdens constriants
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001398 *
1399 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001401int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned int cond,
1403 snd_pcm_hw_param_t var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001404 const struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 return snd_pcm_hw_rule_add(runtime, cond, var,
Lars-Peter Clausene5e113c2015-10-28 11:37:53 +01001407 snd_pcm_hw_rule_ratdens, (void *)r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 var, -1);
1409}
1410
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001411EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1412
Takashi Iwai877211f2005-11-17 13:59:38 +01001413static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1414 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 unsigned int l = (unsigned long) rule->private;
1417 int width = l & 0xffff;
1418 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001419 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001420
1421 if (!snd_interval_single(i))
1422 return 0;
1423
1424 if ((snd_interval_value(i) == width) ||
1425 (width == 0 && snd_interval_value(i) > msbits))
Lars-Peter Clausen19f52fa2014-12-29 18:43:36 +01001426 params->msbits = min_not_zero(params->msbits, msbits);
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
1430
1431/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001432 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001433 * @runtime: PCM runtime instance
1434 * @cond: condition bits
1435 * @width: sample bits width
1436 * @msbits: msbits width
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001437 *
Lars-Peter Clausen8ef9df52014-12-29 18:43:37 +01001438 * This constraint will set the number of most significant bits (msbits) if a
1439 * sample format with the specified width has been select. If width is set to 0
1440 * the msbits will be set for any sample format with a width larger than the
1441 * specified msbits.
1442 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001443 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001445int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 unsigned int cond,
1447 unsigned int width,
1448 unsigned int msbits)
1449{
1450 unsigned long l = (msbits << 16) | width;
1451 return snd_pcm_hw_rule_add(runtime, cond, -1,
1452 snd_pcm_hw_rule_msbits,
1453 (void*) l,
1454 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1455}
1456
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001457EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1458
Takashi Iwai877211f2005-11-17 13:59:38 +01001459static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1460 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 unsigned long step = (unsigned long) rule->private;
Clemens Ladisch0f519b62014-09-07 21:43:07 +02001463 return snd_interval_step(hw_param_interval(params, rule->var), step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001467 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001468 * @runtime: PCM runtime instance
1469 * @cond: condition bits
1470 * @var: hw_params variable to apply the step constraint
1471 * @step: step size
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001472 *
1473 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001475int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 unsigned int cond,
1477 snd_pcm_hw_param_t var,
1478 unsigned long step)
1479{
1480 return snd_pcm_hw_rule_add(runtime, cond, var,
1481 snd_pcm_hw_rule_step, (void *) step,
1482 var, -1);
1483}
1484
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001485EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1486
Takashi Iwai877211f2005-11-17 13:59:38 +01001487static 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 -07001488{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001489 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1491 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1492 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1493 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1494 };
1495 return snd_interval_list(hw_param_interval(params, rule->var),
1496 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1497}
1498
1499/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001500 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001501 * @runtime: PCM runtime instance
1502 * @cond: condition bits
1503 * @var: hw_params variable to apply the power-of-2 constraint
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001504 *
1505 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001507int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 unsigned int cond,
1509 snd_pcm_hw_param_t var)
1510{
1511 return snd_pcm_hw_rule_add(runtime, cond, var,
1512 snd_pcm_hw_rule_pow2, NULL,
1513 var, -1);
1514}
1515
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001516EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1517
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001518static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1519 struct snd_pcm_hw_rule *rule)
1520{
1521 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1522 struct snd_interval *rate;
1523
1524 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1525 return snd_interval_list(rate, 1, &base_rate, 0);
1526}
1527
1528/**
1529 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1530 * @runtime: PCM runtime instance
1531 * @base_rate: the rate at which the hardware does not resample
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001532 *
1533 * Return: Zero if successful, or a negative error code on failure.
Clemens Ladischd5b702a2011-09-16 23:03:02 +02001534 */
1535int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1536 unsigned int base_rate)
1537{
1538 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1539 SNDRV_PCM_HW_PARAM_RATE,
1540 snd_pcm_hw_rule_noresample_func,
1541 (void *)(uintptr_t)base_rate,
1542 SNDRV_PCM_HW_PARAM_RATE, -1);
1543}
1544EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1545
Takashi Iwai877211f2005-11-17 13:59:38 +01001546static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001547 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 if (hw_is_mask(var)) {
1550 snd_mask_any(hw_param_mask(params, var));
1551 params->cmask |= 1 << var;
1552 params->rmask |= 1 << var;
1553 return;
1554 }
1555 if (hw_is_interval(var)) {
1556 snd_interval_any(hw_param_interval(params, var));
1557 params->cmask |= 1 << var;
1558 params->rmask |= 1 << var;
1559 return;
1560 }
1561 snd_BUG();
1562}
1563
Takashi Iwai877211f2005-11-17 13:59:38 +01001564void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 unsigned int k;
1567 memset(params, 0, sizeof(*params));
1568 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1569 _snd_pcm_hw_param_any(params, k);
1570 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1571 _snd_pcm_hw_param_any(params, k);
1572 params->info = ~0U;
1573}
1574
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001575EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001578 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001579 * @params: the hw_params instance
1580 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001581 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001583 * Return: The value for field @var if it's fixed in configuration space
1584 * defined by @params. -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001586int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1587 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001590 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!snd_mask_single(mask))
1592 return -EINVAL;
1593 if (dir)
1594 *dir = 0;
1595 return snd_mask_value(mask);
1596 }
1597 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001598 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (!snd_interval_single(i))
1600 return -EINVAL;
1601 if (dir)
1602 *dir = i->openmin;
1603 return snd_interval_value(i);
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return -EINVAL;
1606}
1607
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001608EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwai877211f2005-11-17 13:59:38 +01001610void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 snd_pcm_hw_param_t var)
1612{
1613 if (hw_is_mask(var)) {
1614 snd_mask_none(hw_param_mask(params, var));
1615 params->cmask |= 1 << var;
1616 params->rmask |= 1 << var;
1617 } else if (hw_is_interval(var)) {
1618 snd_interval_none(hw_param_interval(params, var));
1619 params->cmask |= 1 << var;
1620 params->rmask |= 1 << var;
1621 } else {
1622 snd_BUG();
1623 }
1624}
1625
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001626EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Takashi Iwai877211f2005-11-17 13:59:38 +01001628static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001629 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 int changed;
1632 if (hw_is_mask(var))
1633 changed = snd_mask_refine_first(hw_param_mask(params, var));
1634 else if (hw_is_interval(var))
1635 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001636 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (changed) {
1639 params->cmask |= 1 << var;
1640 params->rmask |= 1 << var;
1641 }
1642 return changed;
1643}
1644
1645
1646/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001647 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001648 * @pcm: PCM instance
1649 * @params: the hw_params instance
1650 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001651 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001653 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * values > minimum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001655 *
1656 * Return: The minimum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001658int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1659 struct snd_pcm_hw_params *params,
1660 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 int changed = _snd_pcm_hw_param_first(params, var);
1663 if (changed < 0)
1664 return changed;
1665 if (params->rmask) {
1666 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001667 if (snd_BUG_ON(err < 0))
1668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670 return snd_pcm_hw_param_value(params, var, dir);
1671}
1672
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001673EXPORT_SYMBOL(snd_pcm_hw_param_first);
1674
Takashi Iwai877211f2005-11-17 13:59:38 +01001675static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001676 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 int changed;
1679 if (hw_is_mask(var))
1680 changed = snd_mask_refine_last(hw_param_mask(params, var));
1681 else if (hw_is_interval(var))
1682 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001683 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (changed) {
1686 params->cmask |= 1 << var;
1687 params->rmask |= 1 << var;
1688 }
1689 return changed;
1690}
1691
1692
1693/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001694 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001695 * @pcm: PCM instance
1696 * @params: the hw_params instance
1697 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001698 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001700 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * values < maximum. Reduce configuration space accordingly.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001702 *
1703 * Return: The maximum, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001705int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1706 struct snd_pcm_hw_params *params,
1707 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 int changed = _snd_pcm_hw_param_last(params, var);
1710 if (changed < 0)
1711 return changed;
1712 if (params->rmask) {
1713 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001714 if (snd_BUG_ON(err < 0))
1715 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 return snd_pcm_hw_param_value(params, var, dir);
1718}
1719
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001720EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001723 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001724 * @pcm: PCM instance
1725 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001727 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * The configuration chosen is that obtained fixing in this order:
1729 * first access, first format, first subformat, min channels,
1730 * min rate, min period time, max buffer size, min tick time
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001731 *
1732 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001734int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1735 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001737 static int vars[] = {
1738 SNDRV_PCM_HW_PARAM_ACCESS,
1739 SNDRV_PCM_HW_PARAM_FORMAT,
1740 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1741 SNDRV_PCM_HW_PARAM_CHANNELS,
1742 SNDRV_PCM_HW_PARAM_RATE,
1743 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1744 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1745 SNDRV_PCM_HW_PARAM_TICK_TIME,
1746 -1
1747 };
1748 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001750 for (v = vars; *v != -1; v++) {
1751 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1752 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1753 else
1754 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001755 if (snd_BUG_ON(err < 0))
1756 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760
Takashi Iwai877211f2005-11-17 13:59:38 +01001761static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 void *arg)
1763{
Takashi Iwai877211f2005-11-17 13:59:38 +01001764 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned long flags;
1766 snd_pcm_stream_lock_irqsave(substream, flags);
1767 if (snd_pcm_running(substream) &&
1768 snd_pcm_update_hw_ptr(substream) >= 0)
1769 runtime->status->hw_ptr %= runtime->buffer_size;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001770 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 runtime->status->hw_ptr = 0;
Pierre-Louis Bossart0e8014d2012-10-22 16:42:14 -05001772 runtime->hw_ptr_wrap = 0;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 snd_pcm_stream_unlock_irqrestore(substream, flags);
1775 return 0;
1776}
1777
Takashi Iwai877211f2005-11-17 13:59:38 +01001778static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 void *arg)
1780{
Takashi Iwai877211f2005-11-17 13:59:38 +01001781 struct snd_pcm_channel_info *info = arg;
1782 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int width;
1784 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1785 info->offset = -1;
1786 return 0;
1787 }
1788 width = snd_pcm_format_physical_width(runtime->format);
1789 if (width < 0)
1790 return width;
1791 info->offset = 0;
1792 switch (runtime->access) {
1793 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1794 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1795 info->first = info->channel * width;
1796 info->step = runtime->channels * width;
1797 break;
1798 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1799 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1800 {
1801 size_t size = runtime->dma_bytes / runtime->channels;
1802 info->first = info->channel * size * 8;
1803 info->step = width;
1804 break;
1805 }
1806 default:
1807 snd_BUG();
1808 break;
1809 }
1810 return 0;
1811}
1812
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001813static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1814 void *arg)
1815{
1816 struct snd_pcm_hw_params *params = arg;
1817 snd_pcm_format_t format;
Clemens Ladischa9960e62014-09-21 22:50:57 +02001818 int channels;
1819 ssize_t frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001820
1821 params->fifo_size = substream->runtime->hw.fifo_size;
1822 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1823 format = params_format(params);
1824 channels = params_channels(params);
Clemens Ladischa9960e62014-09-21 22:50:57 +02001825 frame_size = snd_pcm_format_size(format, channels);
1826 if (frame_size > 0)
1827 params->fifo_size /= (unsigned)frame_size;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001828 }
1829 return 0;
1830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/**
1833 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1834 * @substream: the pcm substream instance
1835 * @cmd: ioctl command
1836 * @arg: ioctl argument
1837 *
1838 * Processes the generic ioctl commands for PCM.
1839 * Can be passed as the ioctl callback for PCM ops.
1840 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001841 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001843int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 unsigned int cmd, void *arg)
1845{
1846 switch (cmd) {
1847 case SNDRV_PCM_IOCTL1_INFO:
1848 return 0;
1849 case SNDRV_PCM_IOCTL1_RESET:
1850 return snd_pcm_lib_ioctl_reset(substream, arg);
1851 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1852 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001853 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1854 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856 return -ENXIO;
1857}
1858
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001859EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/**
1862 * snd_pcm_period_elapsed - update the pcm status for the next period
1863 * @substream: the pcm substream instance
1864 *
1865 * This function is called from the interrupt handler when the
1866 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001867 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 *
1869 * Even if more than one periods have elapsed since the last call, you
1870 * have to call this only once.
1871 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001872void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai877211f2005-11-17 13:59:38 +01001874 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 unsigned long flags;
1876
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001877 if (PCM_RUNTIME_CHECK(substream))
1878 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 snd_pcm_stream_lock_irqsave(substream, flags);
1882 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001883 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto _end;
1885
Jie Yang90bbaf62015-10-16 17:57:46 +08001886#ifdef CONFIG_SND_PCM_TIMER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (substream->timer_running)
1888 snd_timer_interrupt(substream->timer, 1);
Jie Yang90bbaf62015-10-16 17:57:46 +08001889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
Takashi Iwai3aa02cb2016-04-14 18:02:37 +02001892 snd_pcm_stream_unlock_irqrestore(substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001895EXPORT_SYMBOL(snd_pcm_period_elapsed);
1896
Takashi Iwai13075512008-01-08 18:08:14 +01001897/*
1898 * Wait until avail_min data becomes available
1899 * Returns a negative error code if any error occurs during operation.
1900 * The available space is stored on availp. When err = 0 and avail = 0
1901 * on the capture stream, it indicates the stream is in DRAINING state.
1902 */
David Dillow5daeba32010-06-27 00:13:20 +02001903static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001904 snd_pcm_uframes_t *availp)
1905{
1906 struct snd_pcm_runtime *runtime = substream->runtime;
1907 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1908 wait_queue_t wait;
1909 int err = 0;
1910 snd_pcm_uframes_t avail = 0;
Takashi Iwaif2b36142011-05-26 08:09:38 +02001911 long wait_time, tout;
Takashi Iwai13075512008-01-08 18:08:14 +01001912
Arjan van de Ven763437a2011-09-15 08:49:25 +02001913 init_waitqueue_entry(&wait, current);
1914 set_current_state(TASK_INTERRUPTIBLE);
1915 add_wait_queue(&runtime->tsleep, &wait);
1916
Takashi Iwaif2b36142011-05-26 08:09:38 +02001917 if (runtime->no_period_wakeup)
1918 wait_time = MAX_SCHEDULE_TIMEOUT;
1919 else {
1920 wait_time = 10;
1921 if (runtime->rate) {
1922 long t = runtime->period_size * 2 / runtime->rate;
1923 wait_time = max(t, wait_time);
1924 }
1925 wait_time = msecs_to_jiffies(wait_time * 1000);
1926 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001927
Takashi Iwai13075512008-01-08 18:08:14 +01001928 for (;;) {
1929 if (signal_pending(current)) {
1930 err = -ERESTARTSYS;
1931 break;
1932 }
Arjan van de Ven763437a2011-09-15 08:49:25 +02001933
1934 /*
1935 * We need to check if space became available already
1936 * (and thus the wakeup happened already) first to close
1937 * the race of space already having become available.
1938 * This check must happen after been added to the waitqueue
1939 * and having current state be INTERRUPTIBLE.
1940 */
1941 if (is_playback)
1942 avail = snd_pcm_playback_avail(runtime);
1943 else
1944 avail = snd_pcm_capture_avail(runtime);
1945 if (avail >= runtime->twake)
1946 break;
Takashi Iwai13075512008-01-08 18:08:14 +01001947 snd_pcm_stream_unlock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001948
1949 tout = schedule_timeout(wait_time);
1950
Takashi Iwai13075512008-01-08 18:08:14 +01001951 snd_pcm_stream_lock_irq(substream);
Arjan van de Ven763437a2011-09-15 08:49:25 +02001952 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwai13075512008-01-08 18:08:14 +01001953 switch (runtime->status->state) {
1954 case SNDRV_PCM_STATE_SUSPENDED:
1955 err = -ESTRPIPE;
1956 goto _endloop;
1957 case SNDRV_PCM_STATE_XRUN:
1958 err = -EPIPE;
1959 goto _endloop;
1960 case SNDRV_PCM_STATE_DRAINING:
1961 if (is_playback)
1962 err = -EPIPE;
1963 else
1964 avail = 0; /* indicate draining */
1965 goto _endloop;
1966 case SNDRV_PCM_STATE_OPEN:
1967 case SNDRV_PCM_STATE_SETUP:
1968 case SNDRV_PCM_STATE_DISCONNECTED:
1969 err = -EBADFD;
1970 goto _endloop;
JongHo Kimed697e12013-12-17 23:02:24 +09001971 case SNDRV_PCM_STATE_PAUSED:
1972 continue;
Takashi Iwai13075512008-01-08 18:08:14 +01001973 }
1974 if (!tout) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001975 pcm_dbg(substream->pcm,
1976 "%s write error (DMA or IRQ trouble?)\n",
1977 is_playback ? "playback" : "capture");
Takashi Iwai13075512008-01-08 18:08:14 +01001978 err = -EIO;
1979 break;
1980 }
Takashi Iwai13075512008-01-08 18:08:14 +01001981 }
1982 _endloop:
Arjan van de Ven763437a2011-09-15 08:49:25 +02001983 set_current_state(TASK_RUNNING);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001984 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001985 *availp = avail;
1986 return err;
1987}
1988
Takashi Iwai877211f2005-11-17 13:59:38 +01001989static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 unsigned int hwoff,
1991 unsigned long data, unsigned int off,
1992 snd_pcm_uframes_t frames)
1993{
Takashi Iwai877211f2005-11-17 13:59:38 +01001994 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int err;
1996 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1997 if (substream->ops->copy) {
1998 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1999 return err;
2000 } else {
2001 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2003 return -EFAULT;
2004 }
2005 return 0;
2006}
2007
Takashi Iwai877211f2005-11-17 13:59:38 +01002008typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 unsigned long data, unsigned int off,
2010 snd_pcm_uframes_t size);
2011
Takashi Iwai877211f2005-11-17 13:59:38 +01002012static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 unsigned long data,
2014 snd_pcm_uframes_t size,
2015 int nonblock,
2016 transfer_f transfer)
2017{
Takashi Iwai877211f2005-11-17 13:59:38 +01002018 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 snd_pcm_uframes_t xfer = 0;
2020 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002021 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int err = 0;
2023
2024 if (size == 0)
2025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 snd_pcm_stream_lock_irq(substream);
2028 switch (runtime->status->state) {
2029 case SNDRV_PCM_STATE_PREPARED:
2030 case SNDRV_PCM_STATE_RUNNING:
2031 case SNDRV_PCM_STATE_PAUSED:
2032 break;
2033 case SNDRV_PCM_STATE_XRUN:
2034 err = -EPIPE;
2035 goto _end_unlock;
2036 case SNDRV_PCM_STATE_SUSPENDED:
2037 err = -ESTRPIPE;
2038 goto _end_unlock;
2039 default:
2040 err = -EBADFD;
2041 goto _end_unlock;
2042 }
2043
David Dillow5daeba32010-06-27 00:13:20 +02002044 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002045 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2046 snd_pcm_update_hw_ptr(substream);
2047 avail = snd_pcm_playback_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 while (size > 0) {
2049 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002051 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (nonblock) {
2053 err = -EAGAIN;
2054 goto _end_unlock;
2055 }
David Dillow5daeba32010-06-27 00:13:20 +02002056 runtime->twake = min_t(snd_pcm_uframes_t, size,
2057 runtime->control->avail_min ? : 1);
2058 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002059 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 frames = size > avail ? avail : size;
2063 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2064 if (frames > cont)
2065 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002066 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002067 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002068 snd_pcm_stream_unlock_irq(substream);
2069 return -EINVAL;
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 appl_ptr = runtime->control->appl_ptr;
2072 appl_ofs = appl_ptr % runtime->buffer_size;
2073 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002074 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002076 if (err < 0)
2077 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 switch (runtime->status->state) {
2079 case SNDRV_PCM_STATE_XRUN:
2080 err = -EPIPE;
2081 goto _end_unlock;
2082 case SNDRV_PCM_STATE_SUSPENDED:
2083 err = -ESTRPIPE;
2084 goto _end_unlock;
2085 default:
2086 break;
2087 }
2088 appl_ptr += frames;
2089 if (appl_ptr >= runtime->boundary)
2090 appl_ptr -= runtime->boundary;
2091 runtime->control->appl_ptr = appl_ptr;
2092 if (substream->ops->ack)
2093 substream->ops->ack(substream);
2094
2095 offset += frames;
2096 size -= frames;
2097 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002098 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2100 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2101 err = snd_pcm_start(substream);
2102 if (err < 0)
2103 goto _end_unlock;
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002107 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002108 if (xfer > 0 && err >= 0)
2109 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2112}
2113
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002114/* sanity-check for read/write methods */
2115static int pcm_sanity_check(struct snd_pcm_substream *substream)
2116{
2117 struct snd_pcm_runtime *runtime;
2118 if (PCM_RUNTIME_CHECK(substream))
2119 return -ENXIO;
2120 runtime = substream->runtime;
2121 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2122 return -EINVAL;
2123 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2124 return -EBADFD;
2125 return 0;
2126}
2127
Takashi Iwai877211f2005-11-17 13:59:38 +01002128snd_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 -07002129{
Takashi Iwai877211f2005-11-17 13:59:38 +01002130 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002132 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002134 err = pcm_sanity_check(substream);
2135 if (err < 0)
2136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002138 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2141 runtime->channels > 1)
2142 return -EINVAL;
2143 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2144 snd_pcm_lib_write_transfer);
2145}
2146
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002147EXPORT_SYMBOL(snd_pcm_lib_write);
2148
Takashi Iwai877211f2005-11-17 13:59:38 +01002149static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 unsigned int hwoff,
2151 unsigned long data, unsigned int off,
2152 snd_pcm_uframes_t frames)
2153{
Takashi Iwai877211f2005-11-17 13:59:38 +01002154 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int err;
2156 void __user **bufs = (void __user **)data;
2157 int channels = runtime->channels;
2158 int c;
2159 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002160 if (snd_BUG_ON(!substream->ops->silence))
2161 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 for (c = 0; c < channels; ++c, ++bufs) {
2163 if (*bufs == NULL) {
2164 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2165 return err;
2166 } else {
2167 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2168 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2169 return err;
2170 }
2171 }
2172 } else {
2173 /* default transfer behaviour */
2174 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 for (c = 0; c < channels; ++c, ++bufs) {
2176 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2177 if (*bufs == NULL) {
2178 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2179 } else {
2180 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2181 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2182 return -EFAULT;
2183 }
2184 }
2185 }
2186 return 0;
2187}
2188
Takashi Iwai877211f2005-11-17 13:59:38 +01002189snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 void __user **bufs,
2191 snd_pcm_uframes_t frames)
2192{
Takashi Iwai877211f2005-11-17 13:59:38 +01002193 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002195 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002197 err = pcm_sanity_check(substream);
2198 if (err < 0)
2199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002201 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2204 return -EINVAL;
2205 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2206 nonblock, snd_pcm_lib_writev_transfer);
2207}
2208
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002209EXPORT_SYMBOL(snd_pcm_lib_writev);
2210
Takashi Iwai877211f2005-11-17 13:59:38 +01002211static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 unsigned int hwoff,
2213 unsigned long data, unsigned int off,
2214 snd_pcm_uframes_t frames)
2215{
Takashi Iwai877211f2005-11-17 13:59:38 +01002216 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 int err;
2218 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2219 if (substream->ops->copy) {
2220 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2221 return err;
2222 } else {
2223 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2225 return -EFAULT;
2226 }
2227 return 0;
2228}
2229
Takashi Iwai877211f2005-11-17 13:59:38 +01002230static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 unsigned long data,
2232 snd_pcm_uframes_t size,
2233 int nonblock,
2234 transfer_f transfer)
2235{
Takashi Iwai877211f2005-11-17 13:59:38 +01002236 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 snd_pcm_uframes_t xfer = 0;
2238 snd_pcm_uframes_t offset = 0;
Takashi Iwai0910c212012-05-11 17:50:49 +02002239 snd_pcm_uframes_t avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int err = 0;
2241
2242 if (size == 0)
2243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 snd_pcm_stream_lock_irq(substream);
2246 switch (runtime->status->state) {
2247 case SNDRV_PCM_STATE_PREPARED:
2248 if (size >= runtime->start_threshold) {
2249 err = snd_pcm_start(substream);
2250 if (err < 0)
2251 goto _end_unlock;
2252 }
2253 break;
2254 case SNDRV_PCM_STATE_DRAINING:
2255 case SNDRV_PCM_STATE_RUNNING:
2256 case SNDRV_PCM_STATE_PAUSED:
2257 break;
2258 case SNDRV_PCM_STATE_XRUN:
2259 err = -EPIPE;
2260 goto _end_unlock;
2261 case SNDRV_PCM_STATE_SUSPENDED:
2262 err = -ESTRPIPE;
2263 goto _end_unlock;
2264 default:
2265 err = -EBADFD;
2266 goto _end_unlock;
2267 }
2268
David Dillow5daeba32010-06-27 00:13:20 +02002269 runtime->twake = runtime->control->avail_min ? : 1;
Takashi Iwai0910c212012-05-11 17:50:49 +02002270 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2271 snd_pcm_update_hw_ptr(substream);
2272 avail = snd_pcm_capture_avail(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 while (size > 0) {
2274 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 snd_pcm_uframes_t cont;
Takashi Iwai13075512008-01-08 18:08:14 +01002276 if (!avail) {
2277 if (runtime->status->state ==
2278 SNDRV_PCM_STATE_DRAINING) {
2279 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto _end_unlock;
2281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (nonblock) {
2283 err = -EAGAIN;
2284 goto _end_unlock;
2285 }
David Dillow5daeba32010-06-27 00:13:20 +02002286 runtime->twake = min_t(snd_pcm_uframes_t, size,
2287 runtime->control->avail_min ? : 1);
2288 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002289 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002291 if (!avail)
2292 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 frames = size > avail ? avail : size;
2295 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2296 if (frames > cont)
2297 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002298 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002299 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002300 snd_pcm_stream_unlock_irq(substream);
2301 return -EINVAL;
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 appl_ptr = runtime->control->appl_ptr;
2304 appl_ofs = appl_ptr % runtime->buffer_size;
2305 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002306 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002308 if (err < 0)
2309 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 switch (runtime->status->state) {
2311 case SNDRV_PCM_STATE_XRUN:
2312 err = -EPIPE;
2313 goto _end_unlock;
2314 case SNDRV_PCM_STATE_SUSPENDED:
2315 err = -ESTRPIPE;
2316 goto _end_unlock;
2317 default:
2318 break;
2319 }
2320 appl_ptr += frames;
2321 if (appl_ptr >= runtime->boundary)
2322 appl_ptr -= runtime->boundary;
2323 runtime->control->appl_ptr = appl_ptr;
2324 if (substream->ops->ack)
2325 substream->ops->ack(substream);
2326
2327 offset += frames;
2328 size -= frames;
2329 xfer += frames;
Takashi Iwai0910c212012-05-11 17:50:49 +02002330 avail -= frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002333 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002334 if (xfer > 0 && err >= 0)
2335 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2338}
2339
Takashi Iwai877211f2005-11-17 13:59:38 +01002340snd_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 -07002341{
Takashi Iwai877211f2005-11-17 13:59:38 +01002342 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002346 err = pcm_sanity_check(substream);
2347 if (err < 0)
2348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002350 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2352 return -EINVAL;
2353 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2354}
2355
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002356EXPORT_SYMBOL(snd_pcm_lib_read);
2357
Takashi Iwai877211f2005-11-17 13:59:38 +01002358static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 unsigned int hwoff,
2360 unsigned long data, unsigned int off,
2361 snd_pcm_uframes_t frames)
2362{
Takashi Iwai877211f2005-11-17 13:59:38 +01002363 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int err;
2365 void __user **bufs = (void __user **)data;
2366 int channels = runtime->channels;
2367 int c;
2368 if (substream->ops->copy) {
2369 for (c = 0; c < channels; ++c, ++bufs) {
2370 char __user *buf;
2371 if (*bufs == NULL)
2372 continue;
2373 buf = *bufs + samples_to_bytes(runtime, off);
2374 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2375 return err;
2376 }
2377 } else {
2378 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 for (c = 0; c < channels; ++c, ++bufs) {
2380 char *hwbuf;
2381 char __user *buf;
2382 if (*bufs == NULL)
2383 continue;
2384
2385 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2386 buf = *bufs + samples_to_bytes(runtime, off);
2387 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2388 return -EFAULT;
2389 }
2390 }
2391 return 0;
2392}
2393
Takashi Iwai877211f2005-11-17 13:59:38 +01002394snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 void __user **bufs,
2396 snd_pcm_uframes_t frames)
2397{
Takashi Iwai877211f2005-11-17 13:59:38 +01002398 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002400 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002402 err = pcm_sanity_check(substream);
2403 if (err < 0)
2404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2407 return -EBADFD;
2408
Takashi Iwai0df63e42006-04-28 15:13:41 +02002409 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2411 return -EINVAL;
2412 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415EXPORT_SYMBOL(snd_pcm_lib_readv);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002416
2417/*
2418 * standard channel mapping helpers
2419 */
2420
2421/* default channel maps for multi-channel playbacks, up to 8 channels */
2422const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2423 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002424 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002425 { .channels = 2,
2426 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2427 { .channels = 4,
2428 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2429 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2430 { .channels = 6,
2431 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2432 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2433 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2434 { .channels = 8,
2435 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2436 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2437 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2438 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2439 { }
2440};
2441EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2442
2443/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2444const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2445 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02002446 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002447 { .channels = 2,
2448 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2449 { .channels = 4,
2450 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2451 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2452 { .channels = 6,
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2454 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2455 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2456 { .channels = 8,
2457 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2458 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2459 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2460 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2461 { }
2462};
2463EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2464
2465static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2466{
2467 if (ch > info->max_channels)
2468 return false;
2469 return !info->channel_mask || (info->channel_mask & (1U << ch));
2470}
2471
2472static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_info *uinfo)
2474{
2475 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2476
2477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2478 uinfo->count = 0;
2479 uinfo->count = info->max_channels;
2480 uinfo->value.integer.min = 0;
2481 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2482 return 0;
2483}
2484
2485/* get callback for channel map ctl element
2486 * stores the channel position firstly matching with the current channels
2487 */
2488static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_value *ucontrol)
2490{
2491 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2492 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2493 struct snd_pcm_substream *substream;
2494 const struct snd_pcm_chmap_elem *map;
2495
Takashi Iwai552a14a2017-06-14 16:20:32 +02002496 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002497 return -EINVAL;
2498 substream = snd_pcm_chmap_substream(info, idx);
2499 if (!substream)
2500 return -ENODEV;
2501 memset(ucontrol->value.integer.value, 0,
2502 sizeof(ucontrol->value.integer.value));
2503 if (!substream->runtime)
2504 return 0; /* no channels set */
2505 for (map = info->chmap; map->channels; map++) {
2506 int i;
2507 if (map->channels == substream->runtime->channels &&
2508 valid_chmap_channels(info, map->channels)) {
2509 for (i = 0; i < map->channels; i++)
2510 ucontrol->value.integer.value[i] = map->map[i];
2511 return 0;
2512 }
2513 }
2514 return -EINVAL;
2515}
2516
2517/* tlv callback for channel map ctl element
2518 * expands the pre-defined channel maps in a form of TLV
2519 */
2520static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2521 unsigned int size, unsigned int __user *tlv)
2522{
2523 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2524 const struct snd_pcm_chmap_elem *map;
2525 unsigned int __user *dst;
2526 int c, count = 0;
2527
Takashi Iwai552a14a2017-06-14 16:20:32 +02002528 if (!info->chmap)
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002529 return -EINVAL;
2530 if (size < 8)
2531 return -ENOMEM;
2532 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2533 return -EFAULT;
2534 size -= 8;
2535 dst = tlv + 2;
2536 for (map = info->chmap; map->channels; map++) {
2537 int chs_bytes = map->channels * 4;
2538 if (!valid_chmap_channels(info, map->channels))
2539 continue;
2540 if (size < 8)
2541 return -ENOMEM;
2542 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2543 put_user(chs_bytes, dst + 1))
2544 return -EFAULT;
2545 dst += 2;
2546 size -= 8;
2547 count += 8;
2548 if (size < chs_bytes)
2549 return -ENOMEM;
2550 size -= chs_bytes;
2551 count += chs_bytes;
2552 for (c = 0; c < map->channels; c++) {
2553 if (put_user(map->map[c], dst))
2554 return -EFAULT;
2555 dst++;
2556 }
2557 }
2558 if (put_user(count, tlv + 1))
2559 return -EFAULT;
2560 return 0;
2561}
2562
2563static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2564{
2565 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2566 info->pcm->streams[info->stream].chmap_kctl = NULL;
2567 kfree(info);
2568}
2569
2570/**
2571 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2572 * @pcm: the assigned PCM instance
2573 * @stream: stream direction
2574 * @chmap: channel map elements (for query)
2575 * @max_channels: the max number of channels for the stream
2576 * @private_value: the value passed to each kcontrol's private_value field
2577 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2578 *
2579 * Create channel-mapping control elements assigned to the given PCM stream(s).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01002580 * Return: Zero if successful, or a negative error value.
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002581 */
2582int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2583 const struct snd_pcm_chmap_elem *chmap,
2584 int max_channels,
2585 unsigned long private_value,
2586 struct snd_pcm_chmap **info_ret)
2587{
2588 struct snd_pcm_chmap *info;
2589 struct snd_kcontrol_new knew = {
2590 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2591 .access = SNDRV_CTL_ELEM_ACCESS_READ |
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002592 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2593 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2594 .info = pcm_chmap_ctl_info,
2595 .get = pcm_chmap_ctl_get,
2596 .tlv.c = pcm_chmap_ctl_tlv,
2597 };
2598 int err;
2599
Takashi Iwai8d879be2016-05-10 16:07:40 +02002600 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2601 return -EBUSY;
Takashi Iwai2d3391e2012-07-27 18:27:00 +02002602 info = kzalloc(sizeof(*info), GFP_KERNEL);
2603 if (!info)
2604 return -ENOMEM;
2605 info->pcm = pcm;
2606 info->stream = stream;
2607 info->chmap = chmap;
2608 info->max_channels = max_channels;
2609 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2610 knew.name = "Playback Channel Map";
2611 else
2612 knew.name = "Capture Channel Map";
2613 knew.device = pcm->device;
2614 knew.count = pcm->streams[stream].substream_count;
2615 knew.private_value = private_value;
2616 info->kctl = snd_ctl_new1(&knew, info);
2617 if (!info->kctl) {
2618 kfree(info);
2619 return -ENOMEM;
2620 }
2621 info->kctl->private_free = pcm_chmap_ctl_private_free;
2622 err = snd_ctl_add(pcm->card, info->kctl);
2623 if (err < 0)
2624 return err;
2625 pcm->streams[stream].chmap_kctl = info->kctl;
2626 if (info_ret)
2627 *info_ret = info;
2628 return 0;
2629}
2630EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);