ALSA: pcm: Use snd_pcm_stop_xrun() for xrun injection
[linux-block.git] / sound / core / pcm_lib.c
CommitLineData
1da177e4
LT
1/*
2 * Digital Audio (PCM) abstract layer
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
4 * 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
1da177e4 23#include <linux/slab.h>
174cd4b1 24#include <linux/sched/signal.h>
1da177e4 25#include <linux/time.h>
3f7440a6 26#include <linux/math64.h>
d81a6d71 27#include <linux/export.h>
1da177e4
LT
28#include <sound/core.h>
29#include <sound/control.h>
2d3391ec 30#include <sound/tlv.h>
1da177e4
LT
31#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35
2c4842d3
TS
36#include "pcm_local.h"
37
f5914908
TI
38#ifdef CONFIG_SND_PCM_XRUN_DEBUG
39#define CREATE_TRACE_POINTS
40#include "pcm_trace.h"
41#else
42#define trace_hwptr(substream, pos, in_interrupt)
43#define trace_xrun(substream)
44#define trace_hw_ptr_error(substream, reason)
fccf5388 45#define trace_applptr(substream, prev, curr)
f5914908
TI
46#endif
47
a9cd29e7
TI
48static int fill_silence_frames(struct snd_pcm_substream *substream,
49 snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
50
1da177e4
LT
51/*
52 * fill ring buffer with silence
53 * runtime->silence_start: starting pointer to silence area
54 * runtime->silence_filled: size filled with silence
55 * runtime->silence_threshold: threshold from application
56 * runtime->silence_size: maximal size from application
57 *
58 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
59 */
877211f5 60void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
1da177e4 61{
877211f5 62 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 63 snd_pcm_uframes_t frames, ofs, transfer;
29d1a873 64 int err;
1da177e4
LT
65
66 if (runtime->silence_size < runtime->boundary) {
67 snd_pcm_sframes_t noise_dist, n;
aa30db06
TI
68 snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
69 if (runtime->silence_start != appl_ptr) {
70 n = appl_ptr - runtime->silence_start;
1da177e4
LT
71 if (n < 0)
72 n += runtime->boundary;
73 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
74 runtime->silence_filled -= n;
75 else
76 runtime->silence_filled = 0;
aa30db06 77 runtime->silence_start = appl_ptr;
1da177e4 78 }
235475cb 79 if (runtime->silence_filled >= runtime->buffer_size)
1da177e4 80 return;
1da177e4
LT
81 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
82 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
83 return;
84 frames = runtime->silence_threshold - noise_dist;
85 if (frames > runtime->silence_size)
86 frames = runtime->silence_size;
87 } else {
88 if (new_hw_ptr == ULONG_MAX) { /* initialization */
89 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
9e216e8a
JK
90 if (avail > runtime->buffer_size)
91 avail = runtime->buffer_size;
1da177e4
LT
92 runtime->silence_filled = avail > 0 ? avail : 0;
93 runtime->silence_start = (runtime->status->hw_ptr +
94 runtime->silence_filled) %
95 runtime->boundary;
96 } else {
97 ofs = runtime->status->hw_ptr;
98 frames = new_hw_ptr - ofs;
99 if ((snd_pcm_sframes_t)frames < 0)
100 frames += runtime->boundary;
101 runtime->silence_filled -= frames;
102 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
103 runtime->silence_filled = 0;
9a826ddb 104 runtime->silence_start = new_hw_ptr;
1da177e4 105 } else {
9a826ddb 106 runtime->silence_start = ofs;
1da177e4 107 }
1da177e4
LT
108 }
109 frames = runtime->buffer_size - runtime->silence_filled;
110 }
7eaa943c
TI
111 if (snd_BUG_ON(frames > runtime->buffer_size))
112 return;
1da177e4
LT
113 if (frames == 0)
114 return;
9a826ddb 115 ofs = runtime->silence_start % runtime->buffer_size;
1da177e4
LT
116 while (frames > 0) {
117 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
a9cd29e7
TI
118 err = fill_silence_frames(substream, ofs, transfer);
119 snd_BUG_ON(err < 0);
1da177e4
LT
120 runtime->silence_filled += transfer;
121 frames -= transfer;
122 ofs = 0;
123 }
124}
125
acb03d44
EB
126#ifdef CONFIG_SND_DEBUG
127void snd_pcm_debug_name(struct snd_pcm_substream *substream,
c0070110
TI
128 char *name, size_t len)
129{
130 snprintf(name, len, "pcmC%dD%d%c:%d",
131 substream->pcm->card->number,
132 substream->pcm->device,
133 substream->stream ? 'c' : 'p',
134 substream->number);
135}
acb03d44
EB
136EXPORT_SYMBOL(snd_pcm_debug_name);
137#endif
c0070110 138
741b20cf
JK
139#define XRUN_DEBUG_BASIC (1<<0)
140#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
141#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
741b20cf 142
ed3da3d9 143#ifdef CONFIG_SND_PCM_XRUN_DEBUG
4d96eb25 144
741b20cf
JK
145#define xrun_debug(substream, mask) \
146 ((substream)->pstr->xrun_debug & (mask))
0f17014b
JN
147#else
148#define xrun_debug(substream, mask) 0
149#endif
ed3da3d9 150
741b20cf
JK
151#define dump_stack_on_xrun(substream) do { \
152 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
153 dump_stack(); \
ed3da3d9
TI
154 } while (0)
155
9cd641ed
TI
156/* call with stream lock held */
157void __snd_pcm_xrun(struct snd_pcm_substream *substream)
1da177e4 158{
13f040f9
JK
159 struct snd_pcm_runtime *runtime = substream->runtime;
160
f5914908 161 trace_xrun(substream);
13f040f9
JK
162 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
163 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
1da177e4 164 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
741b20cf 165 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
c0070110 166 char name[16];
acb03d44 167 snd_pcm_debug_name(substream, name, sizeof(name));
09e56df8 168 pcm_warn(substream->pcm, "XRUN: %s\n", name);
ed3da3d9 169 dump_stack_on_xrun(substream);
1da177e4 170 }
1da177e4
LT
171}
172
0f17014b 173#ifdef CONFIG_SND_PCM_XRUN_DEBUG
f5914908 174#define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
4d96eb25 175 do { \
f5914908 176 trace_hw_ptr_error(substream, reason); \
4d96eb25 177 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
f5914908
TI
178 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
179 (in_interrupt) ? 'Q' : 'P', ##args); \
4d96eb25
JK
180 dump_stack_on_xrun(substream); \
181 } \
182 } while (0)
183
4d96eb25
JK
184#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
185
4d96eb25 186#define hw_ptr_error(substream, fmt, args...) do { } while (0)
4d96eb25
JK
187
188#endif
189
1250932e
JK
190int snd_pcm_update_state(struct snd_pcm_substream *substream,
191 struct snd_pcm_runtime *runtime)
1da177e4
LT
192{
193 snd_pcm_uframes_t avail;
194
763e5067 195 avail = snd_pcm_avail(substream);
1da177e4
LT
196 if (avail > runtime->avail_max)
197 runtime->avail_max = avail;
4cdc115f
TI
198 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
199 if (avail >= runtime->buffer_size) {
1da177e4 200 snd_pcm_drain_done(substream);
4cdc115f
TI
201 return -EPIPE;
202 }
203 } else {
204 if (avail >= runtime->stop_threshold) {
9cd641ed 205 __snd_pcm_xrun(substream);
4cdc115f
TI
206 return -EPIPE;
207 }
1da177e4 208 }
5daeba34
DD
209 if (runtime->twake) {
210 if (avail >= runtime->twake)
211 wake_up(&runtime->tsleep);
212 } else if (avail >= runtime->control->avail_min)
213 wake_up(&runtime->sleep);
1da177e4
LT
214 return 0;
215}
216
3179f620
PLB
217static void update_audio_tstamp(struct snd_pcm_substream *substream,
218 struct timespec *curr_tstamp,
219 struct timespec *audio_tstamp)
220{
221 struct snd_pcm_runtime *runtime = substream->runtime;
222 u64 audio_frames, audio_nsecs;
223 struct timespec driver_tstamp;
224
225 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
226 return;
227
228 if (!(substream->ops->get_time_info) ||
229 (runtime->audio_tstamp_report.actual_type ==
230 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
231
232 /*
233 * provide audio timestamp derived from pointer position
234 * add delay only if requested
235 */
236
237 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
238
239 if (runtime->audio_tstamp_config.report_delay) {
240 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
241 audio_frames -= runtime->delay;
242 else
243 audio_frames += runtime->delay;
244 }
245 audio_nsecs = div_u64(audio_frames * 1000000000LL,
246 runtime->rate);
247 *audio_tstamp = ns_to_timespec(audio_nsecs);
248 }
20e3f985
HE
249 if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
250 runtime->status->audio_tstamp = *audio_tstamp;
251 runtime->status->tstamp = *curr_tstamp;
252 }
3179f620
PLB
253
254 /*
255 * re-take a driver timestamp to let apps detect if the reference tstamp
256 * read by low-level hardware was provided with a delay
257 */
258 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
259 runtime->driver_tstamp = driver_tstamp;
260}
261
f240406b
JK
262static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
263 unsigned int in_interrupt)
1da177e4 264{
877211f5 265 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 266 snd_pcm_uframes_t pos;
f240406b 267 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
bbf6ad13
JK
268 snd_pcm_sframes_t hdelta, delta;
269 unsigned long jdelta;
3509a03f
PLB
270 unsigned long curr_jiffies;
271 struct timespec curr_tstamp;
4eeaaeae 272 struct timespec audio_tstamp;
0e8014d7 273 int crossed_boundary = 0;
1da177e4 274
bbf6ad13 275 old_hw_ptr = runtime->status->hw_ptr;
3509a03f
PLB
276
277 /*
278 * group pointer, time and jiffies reads to allow for more
279 * accurate correlations/corrections.
280 * The values are stored at the end of this routine after
281 * corrections for hw_ptr position
282 */
f240406b 283 pos = substream->ops->pointer(substream);
3509a03f 284 curr_jiffies = jiffies;
4eeaaeae 285 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
3179f620
PLB
286 if ((substream->ops->get_time_info) &&
287 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
288 substream->ops->get_time_info(substream, &curr_tstamp,
289 &audio_tstamp,
290 &runtime->audio_tstamp_config,
291 &runtime->audio_tstamp_report);
292
293 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
294 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
295 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
296 } else
297 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
4eeaaeae
PLB
298 }
299
1da177e4 300 if (pos == SNDRV_PCM_POS_XRUN) {
9cd641ed 301 __snd_pcm_xrun(substream);
1da177e4
LT
302 return -EPIPE;
303 }
f240406b 304 if (pos >= runtime->buffer_size) {
09e56df8 305 if (printk_ratelimit()) {
f240406b 306 char name[16];
acb03d44 307 snd_pcm_debug_name(substream, name, sizeof(name));
09e56df8 308 pcm_err(substream->pcm,
0ab1ace8 309 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
09e56df8
TI
310 name, pos, runtime->buffer_size,
311 runtime->period_size);
f240406b
JK
312 }
313 pos = 0;
cedb8118 314 }
f240406b 315 pos -= pos % runtime->min_align;
f5914908 316 trace_hwptr(substream, pos, in_interrupt);
ed3da3d9
TI
317 hw_base = runtime->hw_ptr_base;
318 new_hw_ptr = hw_base + pos;
f240406b
JK
319 if (in_interrupt) {
320 /* we know that one period was processed */
321 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
e7636925 322 delta = runtime->hw_ptr_interrupt + runtime->period_size;
f240406b 323 if (delta > new_hw_ptr) {
bd76af0f 324 /* check for double acknowledged interrupts */
3509a03f 325 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
13a98839 326 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
bd76af0f 327 hw_base += runtime->buffer_size;
0e8014d7 328 if (hw_base >= runtime->boundary) {
bd76af0f 329 hw_base = 0;
0e8014d7
PLB
330 crossed_boundary++;
331 }
bd76af0f
JK
332 new_hw_ptr = hw_base + pos;
333 goto __delta;
334 }
1da177e4 335 }
1da177e4 336 }
f240406b
JK
337 /* new_hw_ptr might be lower than old_hw_ptr in case when */
338 /* pointer crosses the end of the ring buffer */
339 if (new_hw_ptr < old_hw_ptr) {
340 hw_base += runtime->buffer_size;
0e8014d7 341 if (hw_base >= runtime->boundary) {
f240406b 342 hw_base = 0;
0e8014d7
PLB
343 crossed_boundary++;
344 }
f240406b
JK
345 new_hw_ptr = hw_base + pos;
346 }
347 __delta:
b406e610
CL
348 delta = new_hw_ptr - old_hw_ptr;
349 if (delta < 0)
350 delta += runtime->boundary;
ab69a490 351
59ff878f 352 if (runtime->no_period_wakeup) {
12ff414e 353 snd_pcm_sframes_t xrun_threshold;
59ff878f
CL
354 /*
355 * Without regular period interrupts, we have to check
356 * the elapsed time to detect xruns.
357 */
3509a03f 358 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
47228e48
CL
359 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
360 goto no_delta_check;
59ff878f 361 hdelta = jdelta - delta * HZ / runtime->rate;
12ff414e
KA
362 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
363 while (hdelta > xrun_threshold) {
59ff878f
CL
364 delta += runtime->buffer_size;
365 hw_base += runtime->buffer_size;
0e8014d7 366 if (hw_base >= runtime->boundary) {
59ff878f 367 hw_base = 0;
0e8014d7
PLB
368 crossed_boundary++;
369 }
59ff878f
CL
370 new_hw_ptr = hw_base + pos;
371 hdelta -= runtime->hw_ptr_buffer_jiffies;
372 }
ab69a490 373 goto no_delta_check;
59ff878f 374 }
ab69a490 375
f240406b 376 /* something must be really wrong */
7b3a177b 377 if (delta >= runtime->buffer_size + runtime->period_size) {
f5914908
TI
378 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
379 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
380 substream->stream, (long)pos,
381 (long)new_hw_ptr, (long)old_hw_ptr);
f240406b
JK
382 return 0;
383 }
c87d9732
TI
384
385 /* Do jiffies check only in xrun_debug mode */
741b20cf 386 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
c87d9732
TI
387 goto no_jiffies_check;
388
3e5b5016
TI
389 /* Skip the jiffies check for hardwares with BATCH flag.
390 * Such hardware usually just increases the position at each IRQ,
391 * thus it can't give any strange position.
392 */
393 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
394 goto no_jiffies_check;
f240406b 395 hdelta = delta;
a4444da3
JK
396 if (hdelta < runtime->delay)
397 goto no_jiffies_check;
398 hdelta -= runtime->delay;
3509a03f 399 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
bbf6ad13
JK
400 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
401 delta = jdelta /
402 (((runtime->period_size * HZ) / runtime->rate)
403 + HZ/100);
f240406b
JK
404 /* move new_hw_ptr according jiffies not pos variable */
405 new_hw_ptr = old_hw_ptr;
ed69c6a8 406 hw_base = delta;
f240406b
JK
407 /* use loop to avoid checks for delta overflows */
408 /* the delta value is small or zero in most cases */
409 while (delta > 0) {
410 new_hw_ptr += runtime->period_size;
0e8014d7 411 if (new_hw_ptr >= runtime->boundary) {
f240406b 412 new_hw_ptr -= runtime->boundary;
0e8014d7
PLB
413 crossed_boundary--;
414 }
f240406b
JK
415 delta--;
416 }
417 /* align hw_base to buffer_size */
f5914908
TI
418 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
419 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
bbf6ad13
JK
420 (long)pos, (long)hdelta,
421 (long)runtime->period_size, jdelta,
ed69c6a8 422 ((hdelta * HZ) / runtime->rate), hw_base,
f240406b
JK
423 (unsigned long)old_hw_ptr,
424 (unsigned long)new_hw_ptr);
ed69c6a8
JK
425 /* reset values to proper state */
426 delta = 0;
427 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
bbf6ad13 428 }
3e5b5016 429 no_jiffies_check:
bbf6ad13 430 if (delta > runtime->period_size + runtime->period_size / 2) {
f5914908
TI
431 hw_ptr_error(substream, in_interrupt,
432 "Lost interrupts?",
433 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
ed3da3d9 434 substream->stream, (long)delta,
f240406b
JK
435 (long)new_hw_ptr,
436 (long)old_hw_ptr);
ed3da3d9 437 }
f240406b 438
ab69a490 439 no_delta_check:
3179f620
PLB
440 if (runtime->status->hw_ptr == new_hw_ptr) {
441 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
f240406b 442 return 0;
3179f620 443 }
ab1863fc 444
1da177e4
LT
445 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
446 runtime->silence_size > 0)
447 snd_pcm_playback_silence(substream, new_hw_ptr);
448
e7636925 449 if (in_interrupt) {
ead4046b
CL
450 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
451 if (delta < 0)
452 delta += runtime->boundary;
453 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
454 runtime->hw_ptr_interrupt += delta;
455 if (runtime->hw_ptr_interrupt >= runtime->boundary)
456 runtime->hw_ptr_interrupt -= runtime->boundary;
e7636925 457 }
ed3da3d9 458 runtime->hw_ptr_base = hw_base;
1da177e4 459 runtime->status->hw_ptr = new_hw_ptr;
3509a03f 460 runtime->hw_ptr_jiffies = curr_jiffies;
0e8014d7
PLB
461 if (crossed_boundary) {
462 snd_BUG_ON(crossed_boundary != 1);
463 runtime->hw_ptr_wrap += runtime->boundary;
464 }
4eeaaeae 465
3179f620 466 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
4eeaaeae 467
1250932e 468 return snd_pcm_update_state(substream, runtime);
1da177e4
LT
469}
470
471/* CAUTION: call it with irq disabled */
877211f5 472int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
1da177e4 473{
f240406b 474 return snd_pcm_update_hw_ptr0(substream, 0);
1da177e4
LT
475}
476
477/**
478 * snd_pcm_set_ops - set the PCM operators
479 * @pcm: the pcm instance
480 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
481 * @ops: the operator table
482 *
483 * Sets the given PCM operators to the pcm instance.
484 */
e6c2e7eb
LPC
485void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
486 const struct snd_pcm_ops *ops)
1da177e4 487{
877211f5
TI
488 struct snd_pcm_str *stream = &pcm->streams[direction];
489 struct snd_pcm_substream *substream;
1da177e4
LT
490
491 for (substream = stream->substream; substream != NULL; substream = substream->next)
492 substream->ops = ops;
493}
e88e8ae6 494EXPORT_SYMBOL(snd_pcm_set_ops);
1da177e4
LT
495
496/**
497 * snd_pcm_sync - set the PCM sync id
498 * @substream: the pcm substream
499 *
500 * Sets the PCM sync identifier for the card.
501 */
877211f5 502void snd_pcm_set_sync(struct snd_pcm_substream *substream)
1da177e4 503{
877211f5 504 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
505
506 runtime->sync.id32[0] = substream->pcm->card->number;
507 runtime->sync.id32[1] = -1;
508 runtime->sync.id32[2] = -1;
509 runtime->sync.id32[3] = -1;
510}
e88e8ae6
TI
511EXPORT_SYMBOL(snd_pcm_set_sync);
512
1da177e4
LT
513/*
514 * Standard ioctl routine
515 */
516
1da177e4
LT
517static inline unsigned int div32(unsigned int a, unsigned int b,
518 unsigned int *r)
519{
520 if (b == 0) {
521 *r = 0;
522 return UINT_MAX;
523 }
524 *r = a % b;
525 return a / b;
526}
527
528static inline unsigned int div_down(unsigned int a, unsigned int b)
529{
530 if (b == 0)
531 return UINT_MAX;
532 return a / b;
533}
534
535static inline unsigned int div_up(unsigned int a, unsigned int b)
536{
537 unsigned int r;
538 unsigned int q;
539 if (b == 0)
540 return UINT_MAX;
541 q = div32(a, b, &r);
542 if (r)
543 ++q;
544 return q;
545}
546
547static inline unsigned int mul(unsigned int a, unsigned int b)
548{
549 if (a == 0)
550 return 0;
551 if (div_down(UINT_MAX, a) < b)
552 return UINT_MAX;
553 return a * b;
554}
555
556static inline unsigned int muldiv32(unsigned int a, unsigned int b,
557 unsigned int c, unsigned int *r)
558{
559 u_int64_t n = (u_int64_t) a * b;
560 if (c == 0) {
1da177e4
LT
561 *r = 0;
562 return UINT_MAX;
563 }
3f7440a6 564 n = div_u64_rem(n, c, r);
1da177e4
LT
565 if (n >= UINT_MAX) {
566 *r = 0;
567 return UINT_MAX;
568 }
569 return n;
570}
571
1da177e4
LT
572/**
573 * snd_interval_refine - refine the interval value of configurator
574 * @i: the interval value to refine
575 * @v: the interval value to refer to
576 *
577 * Refines the interval value with the reference value.
578 * The interval is changed to the range satisfying both intervals.
579 * The interval status (min, max, integer, etc.) are evaluated.
580 *
eb7c06e8
YB
581 * Return: Positive if the value is changed, zero if it's not changed, or a
582 * negative error code.
1da177e4 583 */
877211f5 584int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
1da177e4
LT
585{
586 int changed = 0;
7eaa943c
TI
587 if (snd_BUG_ON(snd_interval_empty(i)))
588 return -EINVAL;
1da177e4
LT
589 if (i->min < v->min) {
590 i->min = v->min;
591 i->openmin = v->openmin;
592 changed = 1;
593 } else if (i->min == v->min && !i->openmin && v->openmin) {
594 i->openmin = 1;
595 changed = 1;
596 }
597 if (i->max > v->max) {
598 i->max = v->max;
599 i->openmax = v->openmax;
600 changed = 1;
601 } else if (i->max == v->max && !i->openmax && v->openmax) {
602 i->openmax = 1;
603 changed = 1;
604 }
605 if (!i->integer && v->integer) {
606 i->integer = 1;
607 changed = 1;
608 }
609 if (i->integer) {
610 if (i->openmin) {
611 i->min++;
612 i->openmin = 0;
613 }
614 if (i->openmax) {
615 i->max--;
616 i->openmax = 0;
617 }
618 } else if (!i->openmin && !i->openmax && i->min == i->max)
619 i->integer = 1;
620 if (snd_interval_checkempty(i)) {
621 snd_interval_none(i);
622 return -EINVAL;
623 }
624 return changed;
625}
e88e8ae6
TI
626EXPORT_SYMBOL(snd_interval_refine);
627
877211f5 628static int snd_interval_refine_first(struct snd_interval *i)
1da177e4 629{
7eaa943c
TI
630 if (snd_BUG_ON(snd_interval_empty(i)))
631 return -EINVAL;
1da177e4
LT
632 if (snd_interval_single(i))
633 return 0;
634 i->max = i->min;
635 i->openmax = i->openmin;
636 if (i->openmax)
637 i->max++;
638 return 1;
639}
640
877211f5 641static int snd_interval_refine_last(struct snd_interval *i)
1da177e4 642{
7eaa943c
TI
643 if (snd_BUG_ON(snd_interval_empty(i)))
644 return -EINVAL;
1da177e4
LT
645 if (snd_interval_single(i))
646 return 0;
647 i->min = i->max;
648 i->openmin = i->openmax;
649 if (i->openmin)
650 i->min--;
651 return 1;
652}
653
877211f5 654void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
655{
656 if (a->empty || b->empty) {
657 snd_interval_none(c);
658 return;
659 }
660 c->empty = 0;
661 c->min = mul(a->min, b->min);
662 c->openmin = (a->openmin || b->openmin);
663 c->max = mul(a->max, b->max);
664 c->openmax = (a->openmax || b->openmax);
665 c->integer = (a->integer && b->integer);
666}
667
668/**
669 * snd_interval_div - refine the interval value with division
df8db936
TI
670 * @a: dividend
671 * @b: divisor
672 * @c: quotient
1da177e4
LT
673 *
674 * c = a / b
675 *
676 * Returns non-zero if the value is changed, zero if not changed.
677 */
877211f5 678void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
679{
680 unsigned int r;
681 if (a->empty || b->empty) {
682 snd_interval_none(c);
683 return;
684 }
685 c->empty = 0;
686 c->min = div32(a->min, b->max, &r);
687 c->openmin = (r || a->openmin || b->openmax);
688 if (b->min > 0) {
689 c->max = div32(a->max, b->min, &r);
690 if (r) {
691 c->max++;
692 c->openmax = 1;
693 } else
694 c->openmax = (a->openmax || b->openmin);
695 } else {
696 c->max = UINT_MAX;
697 c->openmax = 0;
698 }
699 c->integer = 0;
700}
701
702/**
703 * snd_interval_muldivk - refine the interval value
df8db936
TI
704 * @a: dividend 1
705 * @b: dividend 2
706 * @k: divisor (as integer)
707 * @c: result
708 *
1da177e4
LT
709 * c = a * b / k
710 *
711 * Returns non-zero if the value is changed, zero if not changed.
712 */
877211f5
TI
713void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
714 unsigned int k, struct snd_interval *c)
1da177e4
LT
715{
716 unsigned int r;
717 if (a->empty || b->empty) {
718 snd_interval_none(c);
719 return;
720 }
721 c->empty = 0;
722 c->min = muldiv32(a->min, b->min, k, &r);
723 c->openmin = (r || a->openmin || b->openmin);
724 c->max = muldiv32(a->max, b->max, k, &r);
725 if (r) {
726 c->max++;
727 c->openmax = 1;
728 } else
729 c->openmax = (a->openmax || b->openmax);
730 c->integer = 0;
731}
732
733/**
734 * snd_interval_mulkdiv - refine the interval value
df8db936
TI
735 * @a: dividend 1
736 * @k: dividend 2 (as integer)
737 * @b: divisor
738 * @c: result
1da177e4
LT
739 *
740 * c = a * k / b
741 *
742 * Returns non-zero if the value is changed, zero if not changed.
743 */
877211f5
TI
744void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
745 const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
746{
747 unsigned int r;
748 if (a->empty || b->empty) {
749 snd_interval_none(c);
750 return;
751 }
752 c->empty = 0;
753 c->min = muldiv32(a->min, k, b->max, &r);
754 c->openmin = (r || a->openmin || b->openmax);
755 if (b->min > 0) {
756 c->max = muldiv32(a->max, k, b->min, &r);
757 if (r) {
758 c->max++;
759 c->openmax = 1;
760 } else
761 c->openmax = (a->openmax || b->openmin);
762 } else {
763 c->max = UINT_MAX;
764 c->openmax = 0;
765 }
766 c->integer = 0;
767}
768
1da177e4
LT
769/* ---- */
770
771
772/**
773 * snd_interval_ratnum - refine the interval value
df8db936
TI
774 * @i: interval to refine
775 * @rats_count: number of ratnum_t
776 * @rats: ratnum_t array
777 * @nump: pointer to store the resultant numerator
778 * @denp: pointer to store the resultant denominator
1da177e4 779 *
eb7c06e8
YB
780 * Return: Positive if the value is changed, zero if it's not changed, or a
781 * negative error code.
1da177e4 782 */
877211f5 783int snd_interval_ratnum(struct snd_interval *i,
e5e113cf 784 unsigned int rats_count, const struct snd_ratnum *rats,
877211f5 785 unsigned int *nump, unsigned int *denp)
1da177e4 786{
8374e24c
KH
787 unsigned int best_num, best_den;
788 int best_diff;
1da177e4 789 unsigned int k;
877211f5 790 struct snd_interval t;
1da177e4 791 int err;
8374e24c
KH
792 unsigned int result_num, result_den;
793 int result_diff;
1da177e4
LT
794
795 best_num = best_den = best_diff = 0;
796 for (k = 0; k < rats_count; ++k) {
797 unsigned int num = rats[k].num;
798 unsigned int den;
799 unsigned int q = i->min;
800 int diff;
801 if (q == 0)
802 q = 1;
40962d7c 803 den = div_up(num, q);
1da177e4
LT
804 if (den < rats[k].den_min)
805 continue;
806 if (den > rats[k].den_max)
807 den = rats[k].den_max;
808 else {
809 unsigned int r;
810 r = (den - rats[k].den_min) % rats[k].den_step;
811 if (r != 0)
812 den -= r;
813 }
814 diff = num - q * den;
8374e24c
KH
815 if (diff < 0)
816 diff = -diff;
1da177e4
LT
817 if (best_num == 0 ||
818 diff * best_den < best_diff * den) {
819 best_diff = diff;
820 best_den = den;
821 best_num = num;
822 }
823 }
824 if (best_den == 0) {
825 i->empty = 1;
826 return -EINVAL;
827 }
828 t.min = div_down(best_num, best_den);
829 t.openmin = !!(best_num % best_den);
830
8374e24c
KH
831 result_num = best_num;
832 result_diff = best_diff;
833 result_den = best_den;
1da177e4
LT
834 best_num = best_den = best_diff = 0;
835 for (k = 0; k < rats_count; ++k) {
836 unsigned int num = rats[k].num;
837 unsigned int den;
838 unsigned int q = i->max;
839 int diff;
840 if (q == 0) {
841 i->empty = 1;
842 return -EINVAL;
843 }
40962d7c 844 den = div_down(num, q);
1da177e4
LT
845 if (den > rats[k].den_max)
846 continue;
847 if (den < rats[k].den_min)
848 den = rats[k].den_min;
849 else {
850 unsigned int r;
851 r = (den - rats[k].den_min) % rats[k].den_step;
852 if (r != 0)
853 den += rats[k].den_step - r;
854 }
855 diff = q * den - num;
8374e24c
KH
856 if (diff < 0)
857 diff = -diff;
1da177e4
LT
858 if (best_num == 0 ||
859 diff * best_den < best_diff * den) {
860 best_diff = diff;
861 best_den = den;
862 best_num = num;
863 }
864 }
865 if (best_den == 0) {
866 i->empty = 1;
867 return -EINVAL;
868 }
869 t.max = div_up(best_num, best_den);
870 t.openmax = !!(best_num % best_den);
871 t.integer = 0;
872 err = snd_interval_refine(i, &t);
873 if (err < 0)
874 return err;
875
876 if (snd_interval_single(i)) {
8374e24c
KH
877 if (best_diff * result_den < result_diff * best_den) {
878 result_num = best_num;
879 result_den = best_den;
880 }
1da177e4 881 if (nump)
8374e24c 882 *nump = result_num;
1da177e4 883 if (denp)
8374e24c 884 *denp = result_den;
1da177e4
LT
885 }
886 return err;
887}
e88e8ae6
TI
888EXPORT_SYMBOL(snd_interval_ratnum);
889
1da177e4
LT
890/**
891 * snd_interval_ratden - refine the interval value
df8db936 892 * @i: interval to refine
877211f5
TI
893 * @rats_count: number of struct ratden
894 * @rats: struct ratden array
df8db936
TI
895 * @nump: pointer to store the resultant numerator
896 * @denp: pointer to store the resultant denominator
1da177e4 897 *
eb7c06e8
YB
898 * Return: Positive if the value is changed, zero if it's not changed, or a
899 * negative error code.
1da177e4 900 */
877211f5 901static int snd_interval_ratden(struct snd_interval *i,
e5e113cf
LPC
902 unsigned int rats_count,
903 const struct snd_ratden *rats,
1da177e4
LT
904 unsigned int *nump, unsigned int *denp)
905{
906 unsigned int best_num, best_diff, best_den;
907 unsigned int k;
877211f5 908 struct snd_interval t;
1da177e4
LT
909 int err;
910
911 best_num = best_den = best_diff = 0;
912 for (k = 0; k < rats_count; ++k) {
913 unsigned int num;
914 unsigned int den = rats[k].den;
915 unsigned int q = i->min;
916 int diff;
917 num = mul(q, den);
918 if (num > rats[k].num_max)
919 continue;
920 if (num < rats[k].num_min)
921 num = rats[k].num_max;
922 else {
923 unsigned int r;
924 r = (num - rats[k].num_min) % rats[k].num_step;
925 if (r != 0)
926 num += rats[k].num_step - r;
927 }
928 diff = num - q * den;
929 if (best_num == 0 ||
930 diff * best_den < best_diff * den) {
931 best_diff = diff;
932 best_den = den;
933 best_num = num;
934 }
935 }
936 if (best_den == 0) {
937 i->empty = 1;
938 return -EINVAL;
939 }
940 t.min = div_down(best_num, best_den);
941 t.openmin = !!(best_num % best_den);
942
943 best_num = best_den = best_diff = 0;
944 for (k = 0; k < rats_count; ++k) {
945 unsigned int num;
946 unsigned int den = rats[k].den;
947 unsigned int q = i->max;
948 int diff;
949 num = mul(q, den);
950 if (num < rats[k].num_min)
951 continue;
952 if (num > rats[k].num_max)
953 num = rats[k].num_max;
954 else {
955 unsigned int r;
956 r = (num - rats[k].num_min) % rats[k].num_step;
957 if (r != 0)
958 num -= r;
959 }
960 diff = q * den - num;
961 if (best_num == 0 ||
962 diff * best_den < best_diff * den) {
963 best_diff = diff;
964 best_den = den;
965 best_num = num;
966 }
967 }
968 if (best_den == 0) {
969 i->empty = 1;
970 return -EINVAL;
971 }
972 t.max = div_up(best_num, best_den);
973 t.openmax = !!(best_num % best_den);
974 t.integer = 0;
975 err = snd_interval_refine(i, &t);
976 if (err < 0)
977 return err;
978
979 if (snd_interval_single(i)) {
980 if (nump)
981 *nump = best_num;
982 if (denp)
983 *denp = best_den;
984 }
985 return err;
986}
987
988/**
989 * snd_interval_list - refine the interval value from the list
990 * @i: the interval value to refine
991 * @count: the number of elements in the list
992 * @list: the value list
993 * @mask: the bit-mask to evaluate
994 *
995 * Refines the interval value from the list.
996 * When mask is non-zero, only the elements corresponding to bit 1 are
997 * evaluated.
998 *
eb7c06e8
YB
999 * Return: Positive if the value is changed, zero if it's not changed, or a
1000 * negative error code.
1da177e4 1001 */
4af87a93
MB
1002int snd_interval_list(struct snd_interval *i, unsigned int count,
1003 const unsigned int *list, unsigned int mask)
1da177e4
LT
1004{
1005 unsigned int k;
b1ddaf68 1006 struct snd_interval list_range;
0981a260
TI
1007
1008 if (!count) {
1009 i->empty = 1;
1010 return -EINVAL;
1011 }
b1ddaf68
CL
1012 snd_interval_any(&list_range);
1013 list_range.min = UINT_MAX;
1014 list_range.max = 0;
1da177e4
LT
1015 for (k = 0; k < count; k++) {
1016 if (mask && !(mask & (1 << k)))
1017 continue;
b1ddaf68 1018 if (!snd_interval_test(i, list[k]))
1da177e4 1019 continue;
b1ddaf68
CL
1020 list_range.min = min(list_range.min, list[k]);
1021 list_range.max = max(list_range.max, list[k]);
1da177e4 1022 }
b1ddaf68 1023 return snd_interval_refine(i, &list_range);
1da177e4 1024}
e88e8ae6
TI
1025EXPORT_SYMBOL(snd_interval_list);
1026
f66f898e
PR
1027/**
1028 * snd_interval_ranges - refine the interval value from the list of ranges
1029 * @i: the interval value to refine
1030 * @count: the number of elements in the list of ranges
1031 * @ranges: the ranges list
1032 * @mask: the bit-mask to evaluate
1033 *
1034 * Refines the interval value from the list of ranges.
1035 * When mask is non-zero, only the elements corresponding to bit 1 are
1036 * evaluated.
1037 *
1038 * Return: Positive if the value is changed, zero if it's not changed, or a
1039 * negative error code.
1040 */
1041int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1042 const struct snd_interval *ranges, unsigned int mask)
1043{
1044 unsigned int k;
1045 struct snd_interval range_union;
1046 struct snd_interval range;
1047
1048 if (!count) {
1049 snd_interval_none(i);
1050 return -EINVAL;
1051 }
1052 snd_interval_any(&range_union);
1053 range_union.min = UINT_MAX;
1054 range_union.max = 0;
1055 for (k = 0; k < count; k++) {
1056 if (mask && !(mask & (1 << k)))
1057 continue;
1058 snd_interval_copy(&range, &ranges[k]);
1059 if (snd_interval_refine(&range, i) < 0)
1060 continue;
1061 if (snd_interval_empty(&range))
1062 continue;
1063
1064 if (range.min < range_union.min) {
1065 range_union.min = range.min;
1066 range_union.openmin = 1;
1067 }
1068 if (range.min == range_union.min && !range.openmin)
1069 range_union.openmin = 0;
1070 if (range.max > range_union.max) {
1071 range_union.max = range.max;
1072 range_union.openmax = 1;
1073 }
1074 if (range.max == range_union.max && !range.openmax)
1075 range_union.openmax = 0;
1076 }
1077 return snd_interval_refine(i, &range_union);
1078}
1079EXPORT_SYMBOL(snd_interval_ranges);
1080
0f519b62 1081static int snd_interval_step(struct snd_interval *i, unsigned int step)
1da177e4
LT
1082{
1083 unsigned int n;
1084 int changed = 0;
0f519b62 1085 n = i->min % step;
1da177e4
LT
1086 if (n != 0 || i->openmin) {
1087 i->min += step - n;
df1e4719 1088 i->openmin = 0;
1da177e4
LT
1089 changed = 1;
1090 }
0f519b62 1091 n = i->max % step;
1da177e4
LT
1092 if (n != 0 || i->openmax) {
1093 i->max -= n;
df1e4719 1094 i->openmax = 0;
1da177e4
LT
1095 changed = 1;
1096 }
1097 if (snd_interval_checkempty(i)) {
1098 i->empty = 1;
1099 return -EINVAL;
1100 }
1101 return changed;
1102}
1103
1104/* Info constraints helpers */
1105
1106/**
1107 * snd_pcm_hw_rule_add - add the hw-constraint rule
1108 * @runtime: the pcm runtime instance
1109 * @cond: condition bits
1110 * @var: the variable to evaluate
1111 * @func: the evaluation function
1112 * @private: the private data pointer passed to function
1113 * @dep: the dependent variables
1114 *
eb7c06e8 1115 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1116 */
877211f5 1117int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1da177e4
LT
1118 int var,
1119 snd_pcm_hw_rule_func_t func, void *private,
1120 int dep, ...)
1121{
877211f5
TI
1122 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1123 struct snd_pcm_hw_rule *c;
1da177e4
LT
1124 unsigned int k;
1125 va_list args;
1126 va_start(args, dep);
1127 if (constrs->rules_num >= constrs->rules_all) {
877211f5 1128 struct snd_pcm_hw_rule *new;
1da177e4 1129 unsigned int new_rules = constrs->rules_all + 16;
09b9ddfa
TI
1130 new = krealloc(constrs->rules, new_rules * sizeof(*c),
1131 GFP_KERNEL);
87a1c8aa
JJ
1132 if (!new) {
1133 va_end(args);
1da177e4 1134 return -ENOMEM;
87a1c8aa 1135 }
1da177e4
LT
1136 constrs->rules = new;
1137 constrs->rules_all = new_rules;
1138 }
1139 c = &constrs->rules[constrs->rules_num];
1140 c->cond = cond;
1141 c->func = func;
1142 c->var = var;
1143 c->private = private;
1144 k = 0;
1145 while (1) {
87a1c8aa
JJ
1146 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1147 va_end(args);
7eaa943c 1148 return -EINVAL;
87a1c8aa 1149 }
1da177e4
LT
1150 c->deps[k++] = dep;
1151 if (dep < 0)
1152 break;
1153 dep = va_arg(args, int);
1154 }
1155 constrs->rules_num++;
1156 va_end(args);
1157 return 0;
87a1c8aa 1158}
e88e8ae6
TI
1159EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1160
1da177e4 1161/**
1c85cc64 1162 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
df8db936
TI
1163 * @runtime: PCM runtime instance
1164 * @var: hw_params variable to apply the mask
1165 * @mask: the bitmap mask
1166 *
1c85cc64 1167 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
eb7c06e8
YB
1168 *
1169 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1170 */
877211f5 1171int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1172 u_int32_t mask)
1173{
877211f5
TI
1174 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1175 struct snd_mask *maskp = constrs_mask(constrs, var);
1da177e4
LT
1176 *maskp->bits &= mask;
1177 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1178 if (*maskp->bits == 0)
1179 return -EINVAL;
1180 return 0;
1181}
1182
1183/**
1c85cc64 1184 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
df8db936
TI
1185 * @runtime: PCM runtime instance
1186 * @var: hw_params variable to apply the mask
1187 * @mask: the 64bit bitmap mask
1188 *
1c85cc64 1189 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
eb7c06e8
YB
1190 *
1191 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1192 */
877211f5 1193int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1194 u_int64_t mask)
1195{
877211f5
TI
1196 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1197 struct snd_mask *maskp = constrs_mask(constrs, var);
1da177e4
LT
1198 maskp->bits[0] &= (u_int32_t)mask;
1199 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1200 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1201 if (! maskp->bits[0] && ! maskp->bits[1])
1202 return -EINVAL;
1203 return 0;
1204}
63a5d4c6 1205EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1da177e4
LT
1206
1207/**
1c85cc64 1208 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
df8db936
TI
1209 * @runtime: PCM runtime instance
1210 * @var: hw_params variable to apply the integer constraint
1211 *
1212 * Apply the constraint of integer to an interval parameter.
eb7c06e8
YB
1213 *
1214 * Return: Positive if the value is changed, zero if it's not changed, or a
1215 * negative error code.
1da177e4 1216 */
877211f5 1217int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1da177e4 1218{
877211f5 1219 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1da177e4
LT
1220 return snd_interval_setinteger(constrs_interval(constrs, var));
1221}
e88e8ae6
TI
1222EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1223
1da177e4 1224/**
1c85cc64 1225 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
df8db936
TI
1226 * @runtime: PCM runtime instance
1227 * @var: hw_params variable to apply the range
1228 * @min: the minimal value
1229 * @max: the maximal value
1230 *
1231 * Apply the min/max range constraint to an interval parameter.
eb7c06e8
YB
1232 *
1233 * Return: Positive if the value is changed, zero if it's not changed, or a
1234 * negative error code.
1da177e4 1235 */
877211f5 1236int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1237 unsigned int min, unsigned int max)
1238{
877211f5
TI
1239 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1240 struct snd_interval t;
1da177e4
LT
1241 t.min = min;
1242 t.max = max;
1243 t.openmin = t.openmax = 0;
1244 t.integer = 0;
1245 return snd_interval_refine(constrs_interval(constrs, var), &t);
1246}
e88e8ae6
TI
1247EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1248
877211f5
TI
1249static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1250 struct snd_pcm_hw_rule *rule)
1da177e4 1251{
877211f5 1252 struct snd_pcm_hw_constraint_list *list = rule->private;
1da177e4
LT
1253 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1254}
1255
1256
1257/**
1c85cc64 1258 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
df8db936
TI
1259 * @runtime: PCM runtime instance
1260 * @cond: condition bits
1261 * @var: hw_params variable to apply the list constraint
1262 * @l: list
1263 *
1264 * Apply the list of constraints to an interval parameter.
eb7c06e8
YB
1265 *
1266 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1267 */
877211f5 1268int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1da177e4
LT
1269 unsigned int cond,
1270 snd_pcm_hw_param_t var,
1464189f 1271 const struct snd_pcm_hw_constraint_list *l)
1da177e4
LT
1272{
1273 return snd_pcm_hw_rule_add(runtime, cond, var,
1464189f 1274 snd_pcm_hw_rule_list, (void *)l,
1da177e4
LT
1275 var, -1);
1276}
e88e8ae6
TI
1277EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1278
f66f898e
PR
1279static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1280 struct snd_pcm_hw_rule *rule)
1281{
1282 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1283 return snd_interval_ranges(hw_param_interval(params, rule->var),
1284 r->count, r->ranges, r->mask);
1285}
1286
1287
1288/**
1289 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1290 * @runtime: PCM runtime instance
1291 * @cond: condition bits
1292 * @var: hw_params variable to apply the list of range constraints
1293 * @r: ranges
1294 *
1295 * Apply the list of range constraints to an interval parameter.
1296 *
1297 * Return: Zero if successful, or a negative error code on failure.
1298 */
1299int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1300 unsigned int cond,
1301 snd_pcm_hw_param_t var,
1302 const struct snd_pcm_hw_constraint_ranges *r)
1303{
1304 return snd_pcm_hw_rule_add(runtime, cond, var,
1305 snd_pcm_hw_rule_ranges, (void *)r,
1306 var, -1);
1307}
1308EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1309
877211f5
TI
1310static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1311 struct snd_pcm_hw_rule *rule)
1da177e4 1312{
e5e113cf 1313 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1da177e4
LT
1314 unsigned int num = 0, den = 0;
1315 int err;
1316 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1317 r->nrats, r->rats, &num, &den);
1318 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1319 params->rate_num = num;
1320 params->rate_den = den;
1321 }
1322 return err;
1323}
1324
1325/**
1c85cc64 1326 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
df8db936
TI
1327 * @runtime: PCM runtime instance
1328 * @cond: condition bits
1329 * @var: hw_params variable to apply the ratnums constraint
877211f5 1330 * @r: struct snd_ratnums constriants
eb7c06e8
YB
1331 *
1332 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1333 */
877211f5 1334int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1da177e4
LT
1335 unsigned int cond,
1336 snd_pcm_hw_param_t var,
e5e113cf 1337 const struct snd_pcm_hw_constraint_ratnums *r)
1da177e4
LT
1338{
1339 return snd_pcm_hw_rule_add(runtime, cond, var,
e5e113cf 1340 snd_pcm_hw_rule_ratnums, (void *)r,
1da177e4
LT
1341 var, -1);
1342}
e88e8ae6
TI
1343EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1344
877211f5
TI
1345static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1346 struct snd_pcm_hw_rule *rule)
1da177e4 1347{
e5e113cf 1348 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1da177e4
LT
1349 unsigned int num = 0, den = 0;
1350 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1351 r->nrats, r->rats, &num, &den);
1352 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1353 params->rate_num = num;
1354 params->rate_den = den;
1355 }
1356 return err;
1357}
1358
1359/**
1c85cc64 1360 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
df8db936
TI
1361 * @runtime: PCM runtime instance
1362 * @cond: condition bits
1363 * @var: hw_params variable to apply the ratdens constraint
877211f5 1364 * @r: struct snd_ratdens constriants
eb7c06e8
YB
1365 *
1366 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1367 */
877211f5 1368int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1da177e4
LT
1369 unsigned int cond,
1370 snd_pcm_hw_param_t var,
e5e113cf 1371 const struct snd_pcm_hw_constraint_ratdens *r)
1da177e4
LT
1372{
1373 return snd_pcm_hw_rule_add(runtime, cond, var,
e5e113cf 1374 snd_pcm_hw_rule_ratdens, (void *)r,
1da177e4
LT
1375 var, -1);
1376}
e88e8ae6
TI
1377EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1378
877211f5
TI
1379static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1380 struct snd_pcm_hw_rule *rule)
1da177e4
LT
1381{
1382 unsigned int l = (unsigned long) rule->private;
1383 int width = l & 0xffff;
1384 unsigned int msbits = l >> 16;
b55f9fdc
TS
1385 const struct snd_interval *i =
1386 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
8ef9df55
LPC
1387
1388 if (!snd_interval_single(i))
1389 return 0;
1390
1391 if ((snd_interval_value(i) == width) ||
1392 (width == 0 && snd_interval_value(i) > msbits))
19f52fae 1393 params->msbits = min_not_zero(params->msbits, msbits);
8ef9df55 1394
1da177e4
LT
1395 return 0;
1396}
1397
1398/**
1c85cc64 1399 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
df8db936
TI
1400 * @runtime: PCM runtime instance
1401 * @cond: condition bits
1402 * @width: sample bits width
1403 * @msbits: msbits width
eb7c06e8 1404 *
8ef9df55
LPC
1405 * This constraint will set the number of most significant bits (msbits) if a
1406 * sample format with the specified width has been select. If width is set to 0
1407 * the msbits will be set for any sample format with a width larger than the
1408 * specified msbits.
1409 *
eb7c06e8 1410 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1411 */
877211f5 1412int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1da177e4
LT
1413 unsigned int cond,
1414 unsigned int width,
1415 unsigned int msbits)
1416{
1417 unsigned long l = (msbits << 16) | width;
1418 return snd_pcm_hw_rule_add(runtime, cond, -1,
1419 snd_pcm_hw_rule_msbits,
1420 (void*) l,
1421 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1422}
e88e8ae6
TI
1423EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1424
877211f5
TI
1425static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1426 struct snd_pcm_hw_rule *rule)
1da177e4
LT
1427{
1428 unsigned long step = (unsigned long) rule->private;
0f519b62 1429 return snd_interval_step(hw_param_interval(params, rule->var), step);
1da177e4
LT
1430}
1431
1432/**
1c85cc64 1433 * snd_pcm_hw_constraint_step - add a hw constraint step rule
df8db936
TI
1434 * @runtime: PCM runtime instance
1435 * @cond: condition bits
1436 * @var: hw_params variable to apply the step constraint
1437 * @step: step size
eb7c06e8
YB
1438 *
1439 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1440 */
877211f5 1441int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1da177e4
LT
1442 unsigned int cond,
1443 snd_pcm_hw_param_t var,
1444 unsigned long step)
1445{
1446 return snd_pcm_hw_rule_add(runtime, cond, var,
1447 snd_pcm_hw_rule_step, (void *) step,
1448 var, -1);
1449}
e88e8ae6
TI
1450EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1451
877211f5 1452static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1da177e4 1453{
67c39317 1454 static unsigned int pow2_sizes[] = {
1da177e4
LT
1455 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1456 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1457 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1458 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1459 };
1460 return snd_interval_list(hw_param_interval(params, rule->var),
1461 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1462}
1463
1464/**
1c85cc64 1465 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
df8db936
TI
1466 * @runtime: PCM runtime instance
1467 * @cond: condition bits
1468 * @var: hw_params variable to apply the power-of-2 constraint
eb7c06e8
YB
1469 *
1470 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1471 */
877211f5 1472int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1da177e4
LT
1473 unsigned int cond,
1474 snd_pcm_hw_param_t var)
1475{
1476 return snd_pcm_hw_rule_add(runtime, cond, var,
1477 snd_pcm_hw_rule_pow2, NULL,
1478 var, -1);
1479}
e88e8ae6
TI
1480EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1481
d5b702a6
CL
1482static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1483 struct snd_pcm_hw_rule *rule)
1484{
1485 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1486 struct snd_interval *rate;
1487
1488 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1489 return snd_interval_list(rate, 1, &base_rate, 0);
1490}
1491
1492/**
1493 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1494 * @runtime: PCM runtime instance
1495 * @base_rate: the rate at which the hardware does not resample
eb7c06e8
YB
1496 *
1497 * Return: Zero if successful, or a negative error code on failure.
d5b702a6
CL
1498 */
1499int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1500 unsigned int base_rate)
1501{
1502 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1503 SNDRV_PCM_HW_PARAM_RATE,
1504 snd_pcm_hw_rule_noresample_func,
1505 (void *)(uintptr_t)base_rate,
1506 SNDRV_PCM_HW_PARAM_RATE, -1);
1507}
1508EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1509
877211f5 1510static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
123992f7 1511 snd_pcm_hw_param_t var)
1da177e4
LT
1512{
1513 if (hw_is_mask(var)) {
1514 snd_mask_any(hw_param_mask(params, var));
1515 params->cmask |= 1 << var;
1516 params->rmask |= 1 << var;
1517 return;
1518 }
1519 if (hw_is_interval(var)) {
1520 snd_interval_any(hw_param_interval(params, var));
1521 params->cmask |= 1 << var;
1522 params->rmask |= 1 << var;
1523 return;
1524 }
1525 snd_BUG();
1526}
1527
877211f5 1528void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1da177e4
LT
1529{
1530 unsigned int k;
1531 memset(params, 0, sizeof(*params));
1532 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1533 _snd_pcm_hw_param_any(params, k);
1534 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1535 _snd_pcm_hw_param_any(params, k);
1536 params->info = ~0U;
1537}
e88e8ae6 1538EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1da177e4
LT
1539
1540/**
1c85cc64 1541 * snd_pcm_hw_param_value - return @params field @var value
df8db936
TI
1542 * @params: the hw_params instance
1543 * @var: parameter to retrieve
1c85cc64 1544 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1545 *
eb7c06e8
YB
1546 * Return: The value for field @var if it's fixed in configuration space
1547 * defined by @params. -%EINVAL otherwise.
1da177e4 1548 */
e88e8ae6
TI
1549int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1550 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1551{
1552 if (hw_is_mask(var)) {
877211f5 1553 const struct snd_mask *mask = hw_param_mask_c(params, var);
1da177e4
LT
1554 if (!snd_mask_single(mask))
1555 return -EINVAL;
1556 if (dir)
1557 *dir = 0;
1558 return snd_mask_value(mask);
1559 }
1560 if (hw_is_interval(var)) {
877211f5 1561 const struct snd_interval *i = hw_param_interval_c(params, var);
1da177e4
LT
1562 if (!snd_interval_single(i))
1563 return -EINVAL;
1564 if (dir)
1565 *dir = i->openmin;
1566 return snd_interval_value(i);
1567 }
1da177e4
LT
1568 return -EINVAL;
1569}
e88e8ae6 1570EXPORT_SYMBOL(snd_pcm_hw_param_value);
1da177e4 1571
877211f5 1572void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1da177e4
LT
1573 snd_pcm_hw_param_t var)
1574{
1575 if (hw_is_mask(var)) {
1576 snd_mask_none(hw_param_mask(params, var));
1577 params->cmask |= 1 << var;
1578 params->rmask |= 1 << var;
1579 } else if (hw_is_interval(var)) {
1580 snd_interval_none(hw_param_interval(params, var));
1581 params->cmask |= 1 << var;
1582 params->rmask |= 1 << var;
1583 } else {
1584 snd_BUG();
1585 }
1586}
e88e8ae6 1587EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1da177e4 1588
877211f5 1589static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
123992f7 1590 snd_pcm_hw_param_t var)
1da177e4
LT
1591{
1592 int changed;
1593 if (hw_is_mask(var))
1594 changed = snd_mask_refine_first(hw_param_mask(params, var));
1595 else if (hw_is_interval(var))
1596 changed = snd_interval_refine_first(hw_param_interval(params, var));
2f4ca8e5 1597 else
1da177e4 1598 return -EINVAL;
7a0a8716 1599 if (changed > 0) {
1da177e4
LT
1600 params->cmask |= 1 << var;
1601 params->rmask |= 1 << var;
1602 }
1603 return changed;
1604}
1605
1606
1607/**
1c85cc64 1608 * snd_pcm_hw_param_first - refine config space and return minimum value
df8db936
TI
1609 * @pcm: PCM instance
1610 * @params: the hw_params instance
1611 * @var: parameter to retrieve
1c85cc64 1612 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1613 *
1c85cc64 1614 * Inside configuration space defined by @params remove from @var all
1da177e4 1615 * values > minimum. Reduce configuration space accordingly.
eb7c06e8
YB
1616 *
1617 * Return: The minimum, or a negative error code on failure.
1da177e4 1618 */
e88e8ae6
TI
1619int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1620 struct snd_pcm_hw_params *params,
1621 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1622{
1623 int changed = _snd_pcm_hw_param_first(params, var);
1624 if (changed < 0)
1625 return changed;
1626 if (params->rmask) {
1627 int err = snd_pcm_hw_refine(pcm, params);
fe08f34d 1628 if (err < 0)
7eaa943c 1629 return err;
1da177e4
LT
1630 }
1631 return snd_pcm_hw_param_value(params, var, dir);
1632}
e88e8ae6
TI
1633EXPORT_SYMBOL(snd_pcm_hw_param_first);
1634
877211f5 1635static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
123992f7 1636 snd_pcm_hw_param_t var)
1da177e4
LT
1637{
1638 int changed;
1639 if (hw_is_mask(var))
1640 changed = snd_mask_refine_last(hw_param_mask(params, var));
1641 else if (hw_is_interval(var))
1642 changed = snd_interval_refine_last(hw_param_interval(params, var));
2f4ca8e5 1643 else
1da177e4 1644 return -EINVAL;
7a0a8716 1645 if (changed > 0) {
1da177e4
LT
1646 params->cmask |= 1 << var;
1647 params->rmask |= 1 << var;
1648 }
1649 return changed;
1650}
1651
1652
1653/**
1c85cc64 1654 * snd_pcm_hw_param_last - refine config space and return maximum value
df8db936
TI
1655 * @pcm: PCM instance
1656 * @params: the hw_params instance
1657 * @var: parameter to retrieve
1c85cc64 1658 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1659 *
1c85cc64 1660 * Inside configuration space defined by @params remove from @var all
1da177e4 1661 * values < maximum. Reduce configuration space accordingly.
eb7c06e8
YB
1662 *
1663 * Return: The maximum, or a negative error code on failure.
1da177e4 1664 */
e88e8ae6
TI
1665int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1666 struct snd_pcm_hw_params *params,
1667 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1668{
1669 int changed = _snd_pcm_hw_param_last(params, var);
1670 if (changed < 0)
1671 return changed;
1672 if (params->rmask) {
1673 int err = snd_pcm_hw_refine(pcm, params);
fe08f34d 1674 if (err < 0)
7eaa943c 1675 return err;
1da177e4
LT
1676 }
1677 return snd_pcm_hw_param_value(params, var, dir);
1678}
e88e8ae6 1679EXPORT_SYMBOL(snd_pcm_hw_param_last);
1da177e4 1680
877211f5 1681static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1da177e4
LT
1682 void *arg)
1683{
877211f5 1684 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1685 unsigned long flags;
1686 snd_pcm_stream_lock_irqsave(substream, flags);
1687 if (snd_pcm_running(substream) &&
1688 snd_pcm_update_hw_ptr(substream) >= 0)
1689 runtime->status->hw_ptr %= runtime->buffer_size;
0e8014d7 1690 else {
1da177e4 1691 runtime->status->hw_ptr = 0;
0e8014d7
PLB
1692 runtime->hw_ptr_wrap = 0;
1693 }
1da177e4
LT
1694 snd_pcm_stream_unlock_irqrestore(substream, flags);
1695 return 0;
1696}
1697
877211f5 1698static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1da177e4
LT
1699 void *arg)
1700{
877211f5
TI
1701 struct snd_pcm_channel_info *info = arg;
1702 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1703 int width;
1704 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1705 info->offset = -1;
1706 return 0;
1707 }
1708 width = snd_pcm_format_physical_width(runtime->format);
1709 if (width < 0)
1710 return width;
1711 info->offset = 0;
1712 switch (runtime->access) {
1713 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1714 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1715 info->first = info->channel * width;
1716 info->step = runtime->channels * width;
1717 break;
1718 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1719 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1720 {
1721 size_t size = runtime->dma_bytes / runtime->channels;
1722 info->first = info->channel * size * 8;
1723 info->step = width;
1724 break;
1725 }
1726 default:
1727 snd_BUG();
1728 break;
1729 }
1730 return 0;
1731}
1732
8bea869c
JK
1733static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1734 void *arg)
1735{
1736 struct snd_pcm_hw_params *params = arg;
1737 snd_pcm_format_t format;
a9960e6a
CL
1738 int channels;
1739 ssize_t frame_size;
8bea869c
JK
1740
1741 params->fifo_size = substream->runtime->hw.fifo_size;
1742 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1743 format = params_format(params);
1744 channels = params_channels(params);
a9960e6a
CL
1745 frame_size = snd_pcm_format_size(format, channels);
1746 if (frame_size > 0)
1747 params->fifo_size /= (unsigned)frame_size;
8bea869c
JK
1748 }
1749 return 0;
1750}
1751
1da177e4
LT
1752/**
1753 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1754 * @substream: the pcm substream instance
1755 * @cmd: ioctl command
1756 * @arg: ioctl argument
1757 *
1758 * Processes the generic ioctl commands for PCM.
1759 * Can be passed as the ioctl callback for PCM ops.
1760 *
eb7c06e8 1761 * Return: Zero if successful, or a negative error code on failure.
1da177e4 1762 */
877211f5 1763int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
1764 unsigned int cmd, void *arg)
1765{
1766 switch (cmd) {
1da177e4
LT
1767 case SNDRV_PCM_IOCTL1_RESET:
1768 return snd_pcm_lib_ioctl_reset(substream, arg);
1769 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1770 return snd_pcm_lib_ioctl_channel_info(substream, arg);
8bea869c
JK
1771 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1772 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1da177e4
LT
1773 }
1774 return -ENXIO;
1775}
e88e8ae6
TI
1776EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1777
1da177e4
LT
1778/**
1779 * snd_pcm_period_elapsed - update the pcm status for the next period
1780 * @substream: the pcm substream instance
1781 *
1782 * This function is called from the interrupt handler when the
1783 * PCM has processed the period size. It will update the current
31e8960b 1784 * pointer, wake up sleepers, etc.
1da177e4
LT
1785 *
1786 * Even if more than one periods have elapsed since the last call, you
1787 * have to call this only once.
1788 */
877211f5 1789void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1da177e4 1790{
877211f5 1791 struct snd_pcm_runtime *runtime;
1da177e4
LT
1792 unsigned long flags;
1793
7eaa943c
TI
1794 if (PCM_RUNTIME_CHECK(substream))
1795 return;
1da177e4 1796 runtime = substream->runtime;
1da177e4 1797
1da177e4
LT
1798 snd_pcm_stream_lock_irqsave(substream, flags);
1799 if (!snd_pcm_running(substream) ||
f240406b 1800 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1da177e4
LT
1801 goto _end;
1802
90bbaf66 1803#ifdef CONFIG_SND_PCM_TIMER
1da177e4
LT
1804 if (substream->timer_running)
1805 snd_timer_interrupt(substream->timer, 1);
90bbaf66 1806#endif
1da177e4 1807 _end:
1da177e4 1808 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
3aa02cb6 1809 snd_pcm_stream_unlock_irqrestore(substream, flags);
1da177e4 1810}
e88e8ae6
TI
1811EXPORT_SYMBOL(snd_pcm_period_elapsed);
1812
13075510
TI
1813/*
1814 * Wait until avail_min data becomes available
1815 * Returns a negative error code if any error occurs during operation.
1816 * The available space is stored on availp. When err = 0 and avail = 0
1817 * on the capture stream, it indicates the stream is in DRAINING state.
1818 */
5daeba34 1819static int wait_for_avail(struct snd_pcm_substream *substream,
13075510
TI
1820 snd_pcm_uframes_t *availp)
1821{
1822 struct snd_pcm_runtime *runtime = substream->runtime;
1823 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
ac6424b9 1824 wait_queue_entry_t wait;
13075510
TI
1825 int err = 0;
1826 snd_pcm_uframes_t avail = 0;
f2b3614c
TI
1827 long wait_time, tout;
1828
763437a9
AV
1829 init_waitqueue_entry(&wait, current);
1830 set_current_state(TASK_INTERRUPTIBLE);
1831 add_wait_queue(&runtime->tsleep, &wait);
1832
f2b3614c
TI
1833 if (runtime->no_period_wakeup)
1834 wait_time = MAX_SCHEDULE_TIMEOUT;
1835 else {
1836 wait_time = 10;
1837 if (runtime->rate) {
1838 long t = runtime->period_size * 2 / runtime->rate;
1839 wait_time = max(t, wait_time);
1840 }
1841 wait_time = msecs_to_jiffies(wait_time * 1000);
1842 }
763437a9 1843
13075510
TI
1844 for (;;) {
1845 if (signal_pending(current)) {
1846 err = -ERESTARTSYS;
1847 break;
1848 }
763437a9
AV
1849
1850 /*
1851 * We need to check if space became available already
1852 * (and thus the wakeup happened already) first to close
1853 * the race of space already having become available.
1854 * This check must happen after been added to the waitqueue
1855 * and having current state be INTERRUPTIBLE.
1856 */
763e5067 1857 avail = snd_pcm_avail(substream);
763437a9
AV
1858 if (avail >= runtime->twake)
1859 break;
13075510 1860 snd_pcm_stream_unlock_irq(substream);
763437a9
AV
1861
1862 tout = schedule_timeout(wait_time);
1863
13075510 1864 snd_pcm_stream_lock_irq(substream);
763437a9 1865 set_current_state(TASK_INTERRUPTIBLE);
13075510
TI
1866 switch (runtime->status->state) {
1867 case SNDRV_PCM_STATE_SUSPENDED:
1868 err = -ESTRPIPE;
1869 goto _endloop;
1870 case SNDRV_PCM_STATE_XRUN:
1871 err = -EPIPE;
1872 goto _endloop;
1873 case SNDRV_PCM_STATE_DRAINING:
1874 if (is_playback)
1875 err = -EPIPE;
1876 else
1877 avail = 0; /* indicate draining */
1878 goto _endloop;
1879 case SNDRV_PCM_STATE_OPEN:
1880 case SNDRV_PCM_STATE_SETUP:
1881 case SNDRV_PCM_STATE_DISCONNECTED:
1882 err = -EBADFD;
1883 goto _endloop;
ed697e1a
JK
1884 case SNDRV_PCM_STATE_PAUSED:
1885 continue;
13075510
TI
1886 }
1887 if (!tout) {
09e56df8
TI
1888 pcm_dbg(substream->pcm,
1889 "%s write error (DMA or IRQ trouble?)\n",
1890 is_playback ? "playback" : "capture");
13075510
TI
1891 err = -EIO;
1892 break;
1893 }
13075510
TI
1894 }
1895 _endloop:
763437a9 1896 set_current_state(TASK_RUNNING);
c91a988d 1897 remove_wait_queue(&runtime->tsleep, &wait);
13075510
TI
1898 *availp = avail;
1899 return err;
1900}
1901
9f600630
TI
1902typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
1903 int channel, unsigned long hwoff,
1904 void *buf, unsigned long bytes);
bdc4acf7 1905
9f600630
TI
1906typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1907 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
1908
1909/* calculate the target DMA-buffer position to be written/read */
1910static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
1911 int channel, unsigned long hwoff)
1da177e4 1912{
9f600630
TI
1913 return runtime->dma_area + hwoff +
1914 channel * (runtime->dma_bytes / runtime->channels);
1915}
1916
5c7264cf
TI
1917/* default copy_user ops for write; used for both interleaved and non- modes */
1918static int default_write_copy(struct snd_pcm_substream *substream,
1919 int channel, unsigned long hwoff,
1920 void *buf, unsigned long bytes)
9f600630
TI
1921{
1922 if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
5c7264cf 1923 (void __user *)buf, bytes))
9f600630
TI
1924 return -EFAULT;
1925 return 0;
1926}
1927
68541213
TI
1928/* default copy_kernel ops for write */
1929static int default_write_copy_kernel(struct snd_pcm_substream *substream,
1930 int channel, unsigned long hwoff,
1931 void *buf, unsigned long bytes)
1932{
1933 memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
1da177e4
LT
1934 return 0;
1935}
1da177e4 1936
9f600630
TI
1937/* fill silence instead of copy data; called as a transfer helper
1938 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
1939 * a NULL buffer is passed
1940 */
1941static int fill_silence(struct snd_pcm_substream *substream, int channel,
1942 unsigned long hwoff, void *buf, unsigned long bytes)
1da177e4 1943{
877211f5 1944 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1945
9f600630 1946 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1da177e4 1947 return 0;
9f600630
TI
1948 if (substream->ops->fill_silence)
1949 return substream->ops->fill_silence(substream, channel,
1950 hwoff, bytes);
1da177e4 1951
9f600630
TI
1952 snd_pcm_format_set_silence(runtime->format,
1953 get_dma_ptr(runtime, channel, hwoff),
1954 bytes_to_samples(runtime, bytes));
1da177e4
LT
1955 return 0;
1956}
1da177e4 1957
5c7264cf
TI
1958/* default copy_user ops for read; used for both interleaved and non- modes */
1959static int default_read_copy(struct snd_pcm_substream *substream,
1960 int channel, unsigned long hwoff,
1961 void *buf, unsigned long bytes)
1962{
1963 if (copy_to_user((void __user *)buf,
1964 get_dma_ptr(substream->runtime, channel, hwoff),
1965 bytes))
1966 return -EFAULT;
1967 return 0;
1968}
1da177e4 1969
68541213
TI
1970/* default copy_kernel ops for read */
1971static int default_read_copy_kernel(struct snd_pcm_substream *substream,
1972 int channel, unsigned long hwoff,
1973 void *buf, unsigned long bytes)
1974{
1975 memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
1976 return 0;
1977}
1978
9f600630
TI
1979/* call transfer function with the converted pointers and sizes;
1980 * for interleaved mode, it's one shot for all samples
1981 */
1982static int interleaved_copy(struct snd_pcm_substream *substream,
1983 snd_pcm_uframes_t hwoff, void *data,
1984 snd_pcm_uframes_t off,
1985 snd_pcm_uframes_t frames,
1986 pcm_transfer_f transfer)
1987{
1988 struct snd_pcm_runtime *runtime = substream->runtime;
1989
1990 /* convert to bytes */
1991 hwoff = frames_to_bytes(runtime, hwoff);
1992 off = frames_to_bytes(runtime, off);
1993 frames = frames_to_bytes(runtime, frames);
1994 return transfer(substream, 0, hwoff, data + off, frames);
1995}
1996
1997/* call transfer function with the converted pointers and sizes for each
1998 * non-interleaved channel; when buffer is NULL, silencing instead of copying
1999 */
2000static int noninterleaved_copy(struct snd_pcm_substream *substream,
2001 snd_pcm_uframes_t hwoff, void *data,
2002 snd_pcm_uframes_t off,
2003 snd_pcm_uframes_t frames,
2004 pcm_transfer_f transfer)
bdc4acf7
TI
2005{
2006 struct snd_pcm_runtime *runtime = substream->runtime;
bdc4acf7 2007 int channels = runtime->channels;
9f600630
TI
2008 void **bufs = data;
2009 int c, err;
2010
2011 /* convert to bytes; note that it's not frames_to_bytes() here.
2012 * in non-interleaved mode, we copy for each channel, thus
2013 * each copy is n_samples bytes x channels = whole frames.
2014 */
2015 off = samples_to_bytes(runtime, off);
2016 frames = samples_to_bytes(runtime, frames);
2017 hwoff = samples_to_bytes(runtime, hwoff);
2018 for (c = 0; c < channels; ++c, ++bufs) {
2019 if (!data || !*bufs)
2020 err = fill_silence(substream, c, hwoff, NULL, frames);
2021 else
2022 err = transfer(substream, c, hwoff, *bufs + off,
2023 frames);
2024 if (err < 0)
2025 return err;
1da177e4 2026 }
bdc4acf7
TI
2027 return 0;
2028}
2029
a9cd29e7
TI
2030/* fill silence on the given buffer position;
2031 * called from snd_pcm_playback_silence()
2032 */
2033static int fill_silence_frames(struct snd_pcm_substream *substream,
2034 snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2035{
2036 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2037 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2038 return interleaved_copy(substream, off, NULL, 0, frames,
2039 fill_silence);
2040 else
2041 return noninterleaved_copy(substream, off, NULL, 0, frames,
2042 fill_silence);
1da177e4
LT
2043}
2044
7eaa943c
TI
2045/* sanity-check for read/write methods */
2046static int pcm_sanity_check(struct snd_pcm_substream *substream)
1da177e4 2047{
877211f5 2048 struct snd_pcm_runtime *runtime;
7eaa943c
TI
2049 if (PCM_RUNTIME_CHECK(substream))
2050 return -ENXIO;
1da177e4 2051 runtime = substream->runtime;
bdc4acf7 2052 if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
7eaa943c 2053 return -EINVAL;
1da177e4
LT
2054 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2055 return -EBADFD;
7eaa943c
TI
2056 return 0;
2057}
2058
6ba63929 2059static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
7eaa943c 2060{
6ba63929
TI
2061 switch (runtime->status->state) {
2062 case SNDRV_PCM_STATE_PREPARED:
2063 case SNDRV_PCM_STATE_RUNNING:
2064 case SNDRV_PCM_STATE_PAUSED:
2065 return 0;
2066 case SNDRV_PCM_STATE_XRUN:
2067 return -EPIPE;
2068 case SNDRV_PCM_STATE_SUSPENDED:
2069 return -ESTRPIPE;
2070 default:
2071 return -EBADFD;
2072 }
1da177e4
LT
2073}
2074
66e01a5c
TS
2075/* update to the given appl_ptr and call ack callback if needed;
2076 * when an error is returned, take back to the original value
2077 */
2078int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
2079 snd_pcm_uframes_t appl_ptr)
1da177e4 2080{
877211f5 2081 struct snd_pcm_runtime *runtime = substream->runtime;
66e01a5c
TS
2082 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2083 int ret;
2084
f8ff2f28
TI
2085 if (old_appl_ptr == appl_ptr)
2086 return 0;
2087
66e01a5c
TS
2088 runtime->control->appl_ptr = appl_ptr;
2089 if (substream->ops->ack) {
2090 ret = substream->ops->ack(substream);
2091 if (ret < 0) {
2092 runtime->control->appl_ptr = old_appl_ptr;
2093 return ret;
1da177e4
LT
2094 }
2095 }
fccf5388
TS
2096
2097 trace_applptr(substream, old_appl_ptr, appl_ptr);
2098
1da177e4
LT
2099 return 0;
2100}
66e01a5c 2101
5c7264cf
TI
2102/* the common loop for read/write data */
2103snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2104 void *data, bool interleaved,
68541213 2105 snd_pcm_uframes_t size, bool in_kernel)
1da177e4 2106{
877211f5 2107 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2108 snd_pcm_uframes_t xfer = 0;
2109 snd_pcm_uframes_t offset = 0;
0910c216 2110 snd_pcm_uframes_t avail;
9f600630
TI
2111 pcm_copy_f writer;
2112 pcm_transfer_f transfer;
c48f12ee 2113 bool nonblock;
5c7264cf 2114 bool is_playback;
7eaa943c 2115 int err;
1da177e4 2116
7eaa943c
TI
2117 err = pcm_sanity_check(substream);
2118 if (err < 0)
2119 return err;
e88e8ae6 2120
5c7264cf 2121 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
c48f12ee
TI
2122 if (interleaved) {
2123 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2124 runtime->channels > 1)
2125 return -EINVAL;
9f600630 2126 writer = interleaved_copy;
1da177e4 2127 } else {
c48f12ee
TI
2128 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2129 return -EINVAL;
9f600630 2130 writer = noninterleaved_copy;
1da177e4 2131 }
1da177e4 2132
9f600630 2133 if (!data) {
5c7264cf
TI
2134 if (is_playback)
2135 transfer = fill_silence;
2136 else
2137 return -EINVAL;
68541213
TI
2138 } else if (in_kernel) {
2139 if (substream->ops->copy_kernel)
2140 transfer = substream->ops->copy_kernel;
2141 else
2142 transfer = is_playback ?
2143 default_write_copy_kernel : default_read_copy_kernel;
9f600630
TI
2144 } else {
2145 if (substream->ops->copy_user)
2146 transfer = (pcm_transfer_f)substream->ops->copy_user;
2147 else
5c7264cf
TI
2148 transfer = is_playback ?
2149 default_write_copy : default_read_copy;
c48f12ee 2150 }
1da177e4
LT
2151
2152 if (size == 0)
2153 return 0;
1da177e4 2154
c48f12ee
TI
2155 nonblock = !!(substream->f_flags & O_NONBLOCK);
2156
1da177e4 2157 snd_pcm_stream_lock_irq(substream);
6ba63929
TI
2158 err = pcm_accessible_state(runtime);
2159 if (err < 0)
1da177e4 2160 goto _end_unlock;
1da177e4 2161
5c7264cf
TI
2162 if (!is_playback &&
2163 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
6ba63929
TI
2164 size >= runtime->start_threshold) {
2165 err = snd_pcm_start(substream);
2166 if (err < 0)
2167 goto _end_unlock;
1da177e4
LT
2168 }
2169
5daeba34 2170 runtime->twake = runtime->control->avail_min ? : 1;
0910c216
TI
2171 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2172 snd_pcm_update_hw_ptr(substream);
763e5067 2173 avail = snd_pcm_avail(substream);
1da177e4
LT
2174 while (size > 0) {
2175 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1da177e4 2176 snd_pcm_uframes_t cont;
13075510 2177 if (!avail) {
5c7264cf
TI
2178 if (!is_playback &&
2179 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
13075510 2180 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1da177e4
LT
2181 goto _end_unlock;
2182 }
1da177e4
LT
2183 if (nonblock) {
2184 err = -EAGAIN;
2185 goto _end_unlock;
2186 }
5daeba34
DD
2187 runtime->twake = min_t(snd_pcm_uframes_t, size,
2188 runtime->control->avail_min ? : 1);
2189 err = wait_for_avail(substream, &avail);
13075510 2190 if (err < 0)
443feb88 2191 goto _end_unlock;
13075510
TI
2192 if (!avail)
2193 continue; /* draining */
1da177e4 2194 }
1da177e4 2195 frames = size > avail ? avail : size;
aa30db06
TI
2196 appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2197 appl_ofs = appl_ptr % runtime->buffer_size;
2198 cont = runtime->buffer_size - appl_ofs;
1da177e4
LT
2199 if (frames > cont)
2200 frames = cont;
7eaa943c 2201 if (snd_BUG_ON(!frames)) {
c91a988d 2202 runtime->twake = 0;
7eaa943c
TI
2203 snd_pcm_stream_unlock_irq(substream);
2204 return -EINVAL;
2205 }
1da177e4 2206 snd_pcm_stream_unlock_irq(substream);
5c7264cf 2207 err = writer(substream, appl_ofs, data, offset, frames,
9f600630 2208 transfer);
1da177e4 2209 snd_pcm_stream_lock_irq(substream);
1250932e
JK
2210 if (err < 0)
2211 goto _end_unlock;
6ba63929
TI
2212 err = pcm_accessible_state(runtime);
2213 if (err < 0)
1da177e4 2214 goto _end_unlock;
1da177e4
LT
2215 appl_ptr += frames;
2216 if (appl_ptr >= runtime->boundary)
2217 appl_ptr -= runtime->boundary;
66e01a5c
TS
2218 err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2219 if (err < 0)
2220 goto _end_unlock;
1da177e4
LT
2221
2222 offset += frames;
2223 size -= frames;
2224 xfer += frames;
0910c216 2225 avail -= frames;
5c7264cf
TI
2226 if (is_playback &&
2227 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2228 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2229 err = snd_pcm_start(substream);
2230 if (err < 0)
2231 goto _end_unlock;
2232 }
1da177e4
LT
2233 }
2234 _end_unlock:
c91a988d 2235 runtime->twake = 0;
1250932e
JK
2236 if (xfer > 0 && err >= 0)
2237 snd_pcm_update_state(substream, runtime);
1da177e4 2238 snd_pcm_stream_unlock_irq(substream);
1da177e4
LT
2239 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2240}
5c7264cf 2241EXPORT_SYMBOL(__snd_pcm_lib_xfer);
2d3391ec
TI
2242
2243/*
2244 * standard channel mapping helpers
2245 */
2246
2247/* default channel maps for multi-channel playbacks, up to 8 channels */
2248const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2249 { .channels = 1,
5efbc261 2250 .map = { SNDRV_CHMAP_MONO } },
2d3391ec
TI
2251 { .channels = 2,
2252 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2253 { .channels = 4,
2254 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2255 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2256 { .channels = 6,
2257 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2258 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2259 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2260 { .channels = 8,
2261 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2262 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2263 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2264 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2265 { }
2266};
2267EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2268
2269/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2270const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2271 { .channels = 1,
5efbc261 2272 .map = { SNDRV_CHMAP_MONO } },
2d3391ec
TI
2273 { .channels = 2,
2274 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2275 { .channels = 4,
2276 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2277 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2278 { .channels = 6,
2279 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2280 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2281 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2282 { .channels = 8,
2283 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2284 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2285 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2286 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2287 { }
2288};
2289EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2290
2291static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2292{
2293 if (ch > info->max_channels)
2294 return false;
2295 return !info->channel_mask || (info->channel_mask & (1U << ch));
2296}
2297
2298static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_info *uinfo)
2300{
2301 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2302
2303 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2304 uinfo->count = 0;
2305 uinfo->count = info->max_channels;
2306 uinfo->value.integer.min = 0;
2307 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2308 return 0;
2309}
2310
2311/* get callback for channel map ctl element
2312 * stores the channel position firstly matching with the current channels
2313 */
2314static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_value *ucontrol)
2316{
2317 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2318 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2319 struct snd_pcm_substream *substream;
2320 const struct snd_pcm_chmap_elem *map;
2321
2deaeaf1 2322 if (!info->chmap)
2d3391ec
TI
2323 return -EINVAL;
2324 substream = snd_pcm_chmap_substream(info, idx);
2325 if (!substream)
2326 return -ENODEV;
2327 memset(ucontrol->value.integer.value, 0,
2328 sizeof(ucontrol->value.integer.value));
2329 if (!substream->runtime)
2330 return 0; /* no channels set */
2331 for (map = info->chmap; map->channels; map++) {
2332 int i;
2333 if (map->channels == substream->runtime->channels &&
2334 valid_chmap_channels(info, map->channels)) {
2335 for (i = 0; i < map->channels; i++)
2336 ucontrol->value.integer.value[i] = map->map[i];
2337 return 0;
2338 }
2339 }
2340 return -EINVAL;
2341}
2342
2343/* tlv callback for channel map ctl element
2344 * expands the pre-defined channel maps in a form of TLV
2345 */
2346static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2347 unsigned int size, unsigned int __user *tlv)
2348{
2349 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2350 const struct snd_pcm_chmap_elem *map;
2351 unsigned int __user *dst;
2352 int c, count = 0;
2353
2deaeaf1 2354 if (!info->chmap)
2d3391ec
TI
2355 return -EINVAL;
2356 if (size < 8)
2357 return -ENOMEM;
2358 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2359 return -EFAULT;
2360 size -= 8;
2361 dst = tlv + 2;
2362 for (map = info->chmap; map->channels; map++) {
2363 int chs_bytes = map->channels * 4;
2364 if (!valid_chmap_channels(info, map->channels))
2365 continue;
2366 if (size < 8)
2367 return -ENOMEM;
2368 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2369 put_user(chs_bytes, dst + 1))
2370 return -EFAULT;
2371 dst += 2;
2372 size -= 8;
2373 count += 8;
2374 if (size < chs_bytes)
2375 return -ENOMEM;
2376 size -= chs_bytes;
2377 count += chs_bytes;
2378 for (c = 0; c < map->channels; c++) {
2379 if (put_user(map->map[c], dst))
2380 return -EFAULT;
2381 dst++;
2382 }
2383 }
2384 if (put_user(count, tlv + 1))
2385 return -EFAULT;
2386 return 0;
2387}
2388
2389static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2390{
2391 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2392 info->pcm->streams[info->stream].chmap_kctl = NULL;
2393 kfree(info);
2394}
2395
2396/**
2397 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2398 * @pcm: the assigned PCM instance
2399 * @stream: stream direction
2400 * @chmap: channel map elements (for query)
2401 * @max_channels: the max number of channels for the stream
2402 * @private_value: the value passed to each kcontrol's private_value field
2403 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2404 *
2405 * Create channel-mapping control elements assigned to the given PCM stream(s).
eb7c06e8 2406 * Return: Zero if successful, or a negative error value.
2d3391ec
TI
2407 */
2408int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2409 const struct snd_pcm_chmap_elem *chmap,
2410 int max_channels,
2411 unsigned long private_value,
2412 struct snd_pcm_chmap **info_ret)
2413{
2414 struct snd_pcm_chmap *info;
2415 struct snd_kcontrol_new knew = {
2416 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2417 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2d3391ec
TI
2418 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2419 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2420 .info = pcm_chmap_ctl_info,
2421 .get = pcm_chmap_ctl_get,
2422 .tlv.c = pcm_chmap_ctl_tlv,
2423 };
2424 int err;
2425
8d879be8
TI
2426 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2427 return -EBUSY;
2d3391ec
TI
2428 info = kzalloc(sizeof(*info), GFP_KERNEL);
2429 if (!info)
2430 return -ENOMEM;
2431 info->pcm = pcm;
2432 info->stream = stream;
2433 info->chmap = chmap;
2434 info->max_channels = max_channels;
2435 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2436 knew.name = "Playback Channel Map";
2437 else
2438 knew.name = "Capture Channel Map";
2439 knew.device = pcm->device;
2440 knew.count = pcm->streams[stream].substream_count;
2441 knew.private_value = private_value;
2442 info->kctl = snd_ctl_new1(&knew, info);
2443 if (!info->kctl) {
2444 kfree(info);
2445 return -ENOMEM;
2446 }
2447 info->kctl->private_free = pcm_chmap_ctl_private_free;
2448 err = snd_ctl_add(pcm->card, info->kctl);
2449 if (err < 0)
2450 return err;
2451 pcm->streams[stream].chmap_kctl = info->kctl;
2452 if (info_ret)
2453 *info_ret = info;
2454 return 0;
2455}
2456EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);