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