sound: pcm_lib: fix unsorted list constraint handling
[deliverable/linux.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
LT
23#include <linux/slab.h>
24#include <linux/time.h>
3f7440a6 25#include <linux/math64.h>
1da177e4
LT
26#include <sound/core.h>
27#include <sound/control.h>
28#include <sound/info.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/timer.h>
32
33/*
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
39 *
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 */
877211f5 42void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
1da177e4 43{
877211f5 44 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
45 snd_pcm_uframes_t frames, ofs, transfer;
46
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
51 if (n < 0)
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
55 else
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
58 }
235475cb 59 if (runtime->silence_filled >= runtime->buffer_size)
1da177e4 60 return;
1da177e4
LT
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 return;
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
67 } else {
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
73 runtime->boundary;
74 } else {
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
9a826ddb 82 runtime->silence_start = new_hw_ptr;
1da177e4 83 } else {
9a826ddb 84 runtime->silence_start = ofs;
1da177e4 85 }
1da177e4
LT
86 }
87 frames = runtime->buffer_size - runtime->silence_filled;
88 }
7eaa943c
TI
89 if (snd_BUG_ON(frames > runtime->buffer_size))
90 return;
1da177e4
LT
91 if (frames == 0)
92 return;
9a826ddb 93 ofs = runtime->silence_start % runtime->buffer_size;
1da177e4
LT
94 while (frames > 0) {
95 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
96 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
97 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
98 if (substream->ops->silence) {
99 int err;
100 err = substream->ops->silence(substream, -1, ofs, transfer);
7eaa943c 101 snd_BUG_ON(err < 0);
1da177e4
LT
102 } else {
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
105 }
106 } else {
107 unsigned int c;
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
111 int err;
112 err = substream->ops->silence(substream, c, ofs, transfer);
7eaa943c 113 snd_BUG_ON(err < 0);
1da177e4
LT
114 }
115 } else {
116 size_t dma_csize = runtime->dma_bytes / channels;
117 for (c = 0; c < channels; ++c) {
118 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
119 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
120 }
121 }
122 }
123 runtime->silence_filled += transfer;
124 frames -= transfer;
125 ofs = 0;
126 }
127}
128
ed3da3d9 129#ifdef CONFIG_SND_PCM_XRUN_DEBUG
c62a01ad 130#define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
ed3da3d9 131#else
c62a01ad 132#define xrun_debug(substream, mask) 0
ed3da3d9
TI
133#endif
134
c62a01ad
JK
135#define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
137 dump_stack(); \
ed3da3d9
TI
138 } while (0)
139
c0070110
TI
140static void pcm_debug_name(struct snd_pcm_substream *substream,
141 char *name, size_t len)
142{
143 snprintf(name, len, "pcmC%dD%d%c:%d",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p',
147 substream->number);
148}
149
877211f5 150static void xrun(struct snd_pcm_substream *substream)
1da177e4 151{
13f040f9
JK
152 struct snd_pcm_runtime *runtime = substream->runtime;
153
154 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
155 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
1da177e4 156 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
c62a01ad 157 if (xrun_debug(substream, 1)) {
c0070110
TI
158 char name[16];
159 pcm_debug_name(substream, name, sizeof(name));
160 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
ed3da3d9 161 dump_stack_on_xrun(substream);
1da177e4 162 }
1da177e4
LT
163}
164
98204646
TI
165static snd_pcm_uframes_t
166snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
167 struct snd_pcm_runtime *runtime)
1da177e4
LT
168{
169 snd_pcm_uframes_t pos;
170
171 pos = substream->ops->pointer(substream);
172 if (pos == SNDRV_PCM_POS_XRUN)
173 return pos; /* XRUN */
1da177e4 174 if (pos >= runtime->buffer_size) {
5f513e11 175 if (printk_ratelimit()) {
c0070110
TI
176 char name[16];
177 pcm_debug_name(substream, name, sizeof(name));
178 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
5f513e11 179 "buffer size = 0x%lx, period size = 0x%lx\n",
c0070110 180 name, pos, runtime->buffer_size,
5f513e11
TI
181 runtime->period_size);
182 }
183 pos = 0;
7c22f1aa 184 }
1da177e4
LT
185 pos -= pos % runtime->min_align;
186 return pos;
187}
188
98204646
TI
189static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
1da177e4
LT
191{
192 snd_pcm_uframes_t avail;
193
194 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
195 avail = snd_pcm_playback_avail(runtime);
196 else
197 avail = snd_pcm_capture_avail(runtime);
198 if (avail > runtime->avail_max)
199 runtime->avail_max = avail;
200 if (avail >= runtime->stop_threshold) {
201 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
202 snd_pcm_drain_done(substream);
203 else
204 xrun(substream);
205 return -EPIPE;
206 }
207 if (avail >= runtime->control->avail_min)
208 wake_up(&runtime->sleep);
209 return 0;
210}
211
ed3da3d9
TI
212#define hw_ptr_error(substream, fmt, args...) \
213 do { \
c62a01ad 214 if (xrun_debug(substream, 1)) { \
ed3da3d9 215 if (printk_ratelimit()) { \
cad377ac 216 snd_printd("PCM: " fmt, ##args); \
ed3da3d9
TI
217 } \
218 dump_stack_on_xrun(substream); \
219 } \
220 } while (0)
221
98204646 222static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
1da177e4 223{
877211f5 224 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 225 snd_pcm_uframes_t pos;
bbf6ad13
JK
226 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
227 snd_pcm_sframes_t hdelta, delta;
228 unsigned long jdelta;
1da177e4 229
bbf6ad13 230 old_hw_ptr = runtime->status->hw_ptr;
1da177e4
LT
231 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
232 if (pos == SNDRV_PCM_POS_XRUN) {
233 xrun(substream);
234 return -EPIPE;
235 }
ed3da3d9
TI
236 hw_base = runtime->hw_ptr_base;
237 new_hw_ptr = hw_base + pos;
1da177e4 238 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
ed3da3d9 239 delta = new_hw_ptr - hw_ptr_interrupt;
ded652f7 240 if (hw_ptr_interrupt >= runtime->boundary) {
8b22d943
TI
241 hw_ptr_interrupt -= runtime->boundary;
242 if (hw_base < runtime->boundary / 2)
243 /* hw_base was already lapped; recalc delta */
ded652f7
TI
244 delta = new_hw_ptr - hw_ptr_interrupt;
245 }
ed3da3d9
TI
246 if (delta < 0) {
247 delta += runtime->buffer_size;
248 if (delta < 0) {
249 hw_ptr_error(substream,
250 "Unexpected hw_pointer value "
251 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
252 substream->stream, (long)pos,
253 (long)hw_ptr_interrupt);
254 /* rebase to interrupt position */
255 hw_base = new_hw_ptr = hw_ptr_interrupt;
ded652f7
TI
256 /* align hw_base to buffer_size */
257 hw_base -= hw_base % runtime->buffer_size;
ed3da3d9
TI
258 delta = 0;
259 } else {
260 hw_base += runtime->buffer_size;
8b22d943 261 if (hw_base >= runtime->boundary)
ed3da3d9
TI
262 hw_base = 0;
263 new_hw_ptr = hw_base + pos;
1da177e4 264 }
1da177e4 265 }
c87d9732
TI
266
267 /* Do jiffies check only in xrun_debug mode */
c62a01ad 268 if (!xrun_debug(substream, 4))
c87d9732
TI
269 goto no_jiffies_check;
270
3e5b5016
TI
271 /* Skip the jiffies check for hardwares with BATCH flag.
272 * Such hardware usually just increases the position at each IRQ,
273 * thus it can't give any strange position.
274 */
275 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
276 goto no_jiffies_check;
bbf6ad13 277 hdelta = new_hw_ptr - old_hw_ptr;
a4444da3
JK
278 if (hdelta < runtime->delay)
279 goto no_jiffies_check;
280 hdelta -= runtime->delay;
bbf6ad13
JK
281 jdelta = jiffies - runtime->hw_ptr_jiffies;
282 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
283 delta = jdelta /
284 (((runtime->period_size * HZ) / runtime->rate)
285 + HZ/100);
286 hw_ptr_error(substream,
287 "hw_ptr skipping! [Q] "
288 "(pos=%ld, delta=%ld, period=%ld, "
289 "jdelta=%lu/%lu/%lu)\n",
290 (long)pos, (long)hdelta,
291 (long)runtime->period_size, jdelta,
292 ((hdelta * HZ) / runtime->rate), delta);
293 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
294 runtime->period_size * delta;
295 if (hw_ptr_interrupt >= runtime->boundary)
296 hw_ptr_interrupt -= runtime->boundary;
297 /* rebase to interrupt position */
298 hw_base = new_hw_ptr = hw_ptr_interrupt;
299 /* align hw_base to buffer_size */
300 hw_base -= hw_base % runtime->buffer_size;
301 delta = 0;
302 }
3e5b5016 303 no_jiffies_check:
bbf6ad13 304 if (delta > runtime->period_size + runtime->period_size / 2) {
ed3da3d9
TI
305 hw_ptr_error(substream,
306 "Lost interrupts? "
307 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
308 substream->stream, (long)delta,
309 (long)hw_ptr_interrupt);
310 /* rebase hw_ptr_interrupt */
311 hw_ptr_interrupt =
312 new_hw_ptr - new_hw_ptr % runtime->period_size;
313 }
ab1863fc
TI
314 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
315
1da177e4
LT
316 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
317 runtime->silence_size > 0)
318 snd_pcm_playback_silence(substream, new_hw_ptr);
319
13f040f9
JK
320 if (runtime->status->hw_ptr == new_hw_ptr)
321 return 0;
322
ed3da3d9 323 runtime->hw_ptr_base = hw_base;
1da177e4 324 runtime->status->hw_ptr = new_hw_ptr;
bbf6ad13 325 runtime->hw_ptr_jiffies = jiffies;
13f040f9
JK
326 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
327 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
1da177e4
LT
328
329 return snd_pcm_update_hw_ptr_post(substream, runtime);
330}
331
332/* CAUTION: call it with irq disabled */
877211f5 333int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
1da177e4 334{
877211f5 335 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 336 snd_pcm_uframes_t pos;
ed3da3d9 337 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
1da177e4 338 snd_pcm_sframes_t delta;
bbf6ad13 339 unsigned long jdelta;
1da177e4
LT
340
341 old_hw_ptr = runtime->status->hw_ptr;
342 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
343 if (pos == SNDRV_PCM_POS_XRUN) {
344 xrun(substream);
345 return -EPIPE;
346 }
ed3da3d9
TI
347 hw_base = runtime->hw_ptr_base;
348 new_hw_ptr = hw_base + pos;
1da177e4 349
ed3da3d9 350 delta = new_hw_ptr - old_hw_ptr;
bbf6ad13 351 jdelta = jiffies - runtime->hw_ptr_jiffies;
ed3da3d9
TI
352 if (delta < 0) {
353 delta += runtime->buffer_size;
354 if (delta < 0) {
355 hw_ptr_error(substream,
356 "Unexpected hw_pointer value [2] "
bbf6ad13 357 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
ed3da3d9 358 substream->stream, (long)pos,
bbf6ad13 359 (long)old_hw_ptr, jdelta);
1da177e4
LT
360 return 0;
361 }
ed3da3d9 362 hw_base += runtime->buffer_size;
8b22d943 363 if (hw_base >= runtime->boundary)
ed3da3d9
TI
364 hw_base = 0;
365 new_hw_ptr = hw_base + pos;
366 }
c87d9732 367 /* Do jiffies check only in xrun_debug mode */
a4444da3
JK
368 if (!xrun_debug(substream, 4))
369 goto no_jiffies_check;
370 if (delta < runtime->delay)
371 goto no_jiffies_check;
372 delta -= runtime->delay;
373 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
ed3da3d9
TI
374 hw_ptr_error(substream,
375 "hw_ptr skipping! "
bbf6ad13 376 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
ed3da3d9 377 (long)pos, (long)delta,
bbf6ad13
JK
378 (long)runtime->period_size, jdelta,
379 ((delta * HZ) / runtime->rate));
ed3da3d9 380 return 0;
1da177e4 381 }
a4444da3 382 no_jiffies_check:
1da177e4
LT
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
384 runtime->silence_size > 0)
385 snd_pcm_playback_silence(substream, new_hw_ptr);
386
d86bf923 387 if (runtime->status->hw_ptr == new_hw_ptr)
13f040f9
JK
388 return 0;
389
ed3da3d9 390 runtime->hw_ptr_base = hw_base;
1da177e4 391 runtime->status->hw_ptr = new_hw_ptr;
bbf6ad13 392 runtime->hw_ptr_jiffies = jiffies;
13f040f9
JK
393 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
394 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
1da177e4
LT
395
396 return snd_pcm_update_hw_ptr_post(substream, runtime);
397}
398
399/**
400 * snd_pcm_set_ops - set the PCM operators
401 * @pcm: the pcm instance
402 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
403 * @ops: the operator table
404 *
405 * Sets the given PCM operators to the pcm instance.
406 */
877211f5 407void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
1da177e4 408{
877211f5
TI
409 struct snd_pcm_str *stream = &pcm->streams[direction];
410 struct snd_pcm_substream *substream;
1da177e4
LT
411
412 for (substream = stream->substream; substream != NULL; substream = substream->next)
413 substream->ops = ops;
414}
415
e88e8ae6 416EXPORT_SYMBOL(snd_pcm_set_ops);
1da177e4
LT
417
418/**
419 * snd_pcm_sync - set the PCM sync id
420 * @substream: the pcm substream
421 *
422 * Sets the PCM sync identifier for the card.
423 */
877211f5 424void snd_pcm_set_sync(struct snd_pcm_substream *substream)
1da177e4 425{
877211f5 426 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
427
428 runtime->sync.id32[0] = substream->pcm->card->number;
429 runtime->sync.id32[1] = -1;
430 runtime->sync.id32[2] = -1;
431 runtime->sync.id32[3] = -1;
432}
433
e88e8ae6
TI
434EXPORT_SYMBOL(snd_pcm_set_sync);
435
1da177e4
LT
436/*
437 * Standard ioctl routine
438 */
439
1da177e4
LT
440static inline unsigned int div32(unsigned int a, unsigned int b,
441 unsigned int *r)
442{
443 if (b == 0) {
444 *r = 0;
445 return UINT_MAX;
446 }
447 *r = a % b;
448 return a / b;
449}
450
451static inline unsigned int div_down(unsigned int a, unsigned int b)
452{
453 if (b == 0)
454 return UINT_MAX;
455 return a / b;
456}
457
458static inline unsigned int div_up(unsigned int a, unsigned int b)
459{
460 unsigned int r;
461 unsigned int q;
462 if (b == 0)
463 return UINT_MAX;
464 q = div32(a, b, &r);
465 if (r)
466 ++q;
467 return q;
468}
469
470static inline unsigned int mul(unsigned int a, unsigned int b)
471{
472 if (a == 0)
473 return 0;
474 if (div_down(UINT_MAX, a) < b)
475 return UINT_MAX;
476 return a * b;
477}
478
479static inline unsigned int muldiv32(unsigned int a, unsigned int b,
480 unsigned int c, unsigned int *r)
481{
482 u_int64_t n = (u_int64_t) a * b;
483 if (c == 0) {
7eaa943c 484 snd_BUG_ON(!n);
1da177e4
LT
485 *r = 0;
486 return UINT_MAX;
487 }
3f7440a6 488 n = div_u64_rem(n, c, r);
1da177e4
LT
489 if (n >= UINT_MAX) {
490 *r = 0;
491 return UINT_MAX;
492 }
493 return n;
494}
495
1da177e4
LT
496/**
497 * snd_interval_refine - refine the interval value of configurator
498 * @i: the interval value to refine
499 * @v: the interval value to refer to
500 *
501 * Refines the interval value with the reference value.
502 * The interval is changed to the range satisfying both intervals.
503 * The interval status (min, max, integer, etc.) are evaluated.
504 *
505 * Returns non-zero if the value is changed, zero if not changed.
506 */
877211f5 507int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
1da177e4
LT
508{
509 int changed = 0;
7eaa943c
TI
510 if (snd_BUG_ON(snd_interval_empty(i)))
511 return -EINVAL;
1da177e4
LT
512 if (i->min < v->min) {
513 i->min = v->min;
514 i->openmin = v->openmin;
515 changed = 1;
516 } else if (i->min == v->min && !i->openmin && v->openmin) {
517 i->openmin = 1;
518 changed = 1;
519 }
520 if (i->max > v->max) {
521 i->max = v->max;
522 i->openmax = v->openmax;
523 changed = 1;
524 } else if (i->max == v->max && !i->openmax && v->openmax) {
525 i->openmax = 1;
526 changed = 1;
527 }
528 if (!i->integer && v->integer) {
529 i->integer = 1;
530 changed = 1;
531 }
532 if (i->integer) {
533 if (i->openmin) {
534 i->min++;
535 i->openmin = 0;
536 }
537 if (i->openmax) {
538 i->max--;
539 i->openmax = 0;
540 }
541 } else if (!i->openmin && !i->openmax && i->min == i->max)
542 i->integer = 1;
543 if (snd_interval_checkempty(i)) {
544 snd_interval_none(i);
545 return -EINVAL;
546 }
547 return changed;
548}
549
e88e8ae6
TI
550EXPORT_SYMBOL(snd_interval_refine);
551
877211f5 552static int snd_interval_refine_first(struct snd_interval *i)
1da177e4 553{
7eaa943c
TI
554 if (snd_BUG_ON(snd_interval_empty(i)))
555 return -EINVAL;
1da177e4
LT
556 if (snd_interval_single(i))
557 return 0;
558 i->max = i->min;
559 i->openmax = i->openmin;
560 if (i->openmax)
561 i->max++;
562 return 1;
563}
564
877211f5 565static int snd_interval_refine_last(struct snd_interval *i)
1da177e4 566{
7eaa943c
TI
567 if (snd_BUG_ON(snd_interval_empty(i)))
568 return -EINVAL;
1da177e4
LT
569 if (snd_interval_single(i))
570 return 0;
571 i->min = i->max;
572 i->openmin = i->openmax;
573 if (i->openmin)
574 i->min--;
575 return 1;
576}
577
877211f5 578void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
579{
580 if (a->empty || b->empty) {
581 snd_interval_none(c);
582 return;
583 }
584 c->empty = 0;
585 c->min = mul(a->min, b->min);
586 c->openmin = (a->openmin || b->openmin);
587 c->max = mul(a->max, b->max);
588 c->openmax = (a->openmax || b->openmax);
589 c->integer = (a->integer && b->integer);
590}
591
592/**
593 * snd_interval_div - refine the interval value with division
df8db936
TI
594 * @a: dividend
595 * @b: divisor
596 * @c: quotient
1da177e4
LT
597 *
598 * c = a / b
599 *
600 * Returns non-zero if the value is changed, zero if not changed.
601 */
877211f5 602void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
603{
604 unsigned int r;
605 if (a->empty || b->empty) {
606 snd_interval_none(c);
607 return;
608 }
609 c->empty = 0;
610 c->min = div32(a->min, b->max, &r);
611 c->openmin = (r || a->openmin || b->openmax);
612 if (b->min > 0) {
613 c->max = div32(a->max, b->min, &r);
614 if (r) {
615 c->max++;
616 c->openmax = 1;
617 } else
618 c->openmax = (a->openmax || b->openmin);
619 } else {
620 c->max = UINT_MAX;
621 c->openmax = 0;
622 }
623 c->integer = 0;
624}
625
626/**
627 * snd_interval_muldivk - refine the interval value
df8db936
TI
628 * @a: dividend 1
629 * @b: dividend 2
630 * @k: divisor (as integer)
631 * @c: result
632 *
1da177e4
LT
633 * c = a * b / k
634 *
635 * Returns non-zero if the value is changed, zero if not changed.
636 */
877211f5
TI
637void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
638 unsigned int k, struct snd_interval *c)
1da177e4
LT
639{
640 unsigned int r;
641 if (a->empty || b->empty) {
642 snd_interval_none(c);
643 return;
644 }
645 c->empty = 0;
646 c->min = muldiv32(a->min, b->min, k, &r);
647 c->openmin = (r || a->openmin || b->openmin);
648 c->max = muldiv32(a->max, b->max, k, &r);
649 if (r) {
650 c->max++;
651 c->openmax = 1;
652 } else
653 c->openmax = (a->openmax || b->openmax);
654 c->integer = 0;
655}
656
657/**
658 * snd_interval_mulkdiv - refine the interval value
df8db936
TI
659 * @a: dividend 1
660 * @k: dividend 2 (as integer)
661 * @b: divisor
662 * @c: result
1da177e4
LT
663 *
664 * c = a * k / b
665 *
666 * Returns non-zero if the value is changed, zero if not changed.
667 */
877211f5
TI
668void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
669 const struct snd_interval *b, struct snd_interval *c)
1da177e4
LT
670{
671 unsigned int r;
672 if (a->empty || b->empty) {
673 snd_interval_none(c);
674 return;
675 }
676 c->empty = 0;
677 c->min = muldiv32(a->min, k, b->max, &r);
678 c->openmin = (r || a->openmin || b->openmax);
679 if (b->min > 0) {
680 c->max = muldiv32(a->max, k, b->min, &r);
681 if (r) {
682 c->max++;
683 c->openmax = 1;
684 } else
685 c->openmax = (a->openmax || b->openmin);
686 } else {
687 c->max = UINT_MAX;
688 c->openmax = 0;
689 }
690 c->integer = 0;
691}
692
1da177e4
LT
693/* ---- */
694
695
696/**
697 * snd_interval_ratnum - refine the interval value
df8db936
TI
698 * @i: interval to refine
699 * @rats_count: number of ratnum_t
700 * @rats: ratnum_t array
701 * @nump: pointer to store the resultant numerator
702 * @denp: pointer to store the resultant denominator
1da177e4
LT
703 *
704 * Returns non-zero if the value is changed, zero if not changed.
705 */
877211f5
TI
706int snd_interval_ratnum(struct snd_interval *i,
707 unsigned int rats_count, struct snd_ratnum *rats,
708 unsigned int *nump, unsigned int *denp)
1da177e4
LT
709{
710 unsigned int best_num, best_diff, best_den;
711 unsigned int k;
877211f5 712 struct snd_interval t;
1da177e4
LT
713 int err;
714
715 best_num = best_den = best_diff = 0;
716 for (k = 0; k < rats_count; ++k) {
717 unsigned int num = rats[k].num;
718 unsigned int den;
719 unsigned int q = i->min;
720 int diff;
721 if (q == 0)
722 q = 1;
723 den = div_down(num, q);
724 if (den < rats[k].den_min)
725 continue;
726 if (den > rats[k].den_max)
727 den = rats[k].den_max;
728 else {
729 unsigned int r;
730 r = (den - rats[k].den_min) % rats[k].den_step;
731 if (r != 0)
732 den -= r;
733 }
734 diff = num - q * den;
735 if (best_num == 0 ||
736 diff * best_den < best_diff * den) {
737 best_diff = diff;
738 best_den = den;
739 best_num = num;
740 }
741 }
742 if (best_den == 0) {
743 i->empty = 1;
744 return -EINVAL;
745 }
746 t.min = div_down(best_num, best_den);
747 t.openmin = !!(best_num % best_den);
748
749 best_num = best_den = best_diff = 0;
750 for (k = 0; k < rats_count; ++k) {
751 unsigned int num = rats[k].num;
752 unsigned int den;
753 unsigned int q = i->max;
754 int diff;
755 if (q == 0) {
756 i->empty = 1;
757 return -EINVAL;
758 }
759 den = div_up(num, q);
760 if (den > rats[k].den_max)
761 continue;
762 if (den < rats[k].den_min)
763 den = rats[k].den_min;
764 else {
765 unsigned int r;
766 r = (den - rats[k].den_min) % rats[k].den_step;
767 if (r != 0)
768 den += rats[k].den_step - r;
769 }
770 diff = q * den - num;
771 if (best_num == 0 ||
772 diff * best_den < best_diff * den) {
773 best_diff = diff;
774 best_den = den;
775 best_num = num;
776 }
777 }
778 if (best_den == 0) {
779 i->empty = 1;
780 return -EINVAL;
781 }
782 t.max = div_up(best_num, best_den);
783 t.openmax = !!(best_num % best_den);
784 t.integer = 0;
785 err = snd_interval_refine(i, &t);
786 if (err < 0)
787 return err;
788
789 if (snd_interval_single(i)) {
790 if (nump)
791 *nump = best_num;
792 if (denp)
793 *denp = best_den;
794 }
795 return err;
796}
797
e88e8ae6
TI
798EXPORT_SYMBOL(snd_interval_ratnum);
799
1da177e4
LT
800/**
801 * snd_interval_ratden - refine the interval value
df8db936 802 * @i: interval to refine
877211f5
TI
803 * @rats_count: number of struct ratden
804 * @rats: struct ratden array
df8db936
TI
805 * @nump: pointer to store the resultant numerator
806 * @denp: pointer to store the resultant denominator
1da177e4
LT
807 *
808 * Returns non-zero if the value is changed, zero if not changed.
809 */
877211f5
TI
810static int snd_interval_ratden(struct snd_interval *i,
811 unsigned int rats_count, struct snd_ratden *rats,
1da177e4
LT
812 unsigned int *nump, unsigned int *denp)
813{
814 unsigned int best_num, best_diff, best_den;
815 unsigned int k;
877211f5 816 struct snd_interval t;
1da177e4
LT
817 int err;
818
819 best_num = best_den = best_diff = 0;
820 for (k = 0; k < rats_count; ++k) {
821 unsigned int num;
822 unsigned int den = rats[k].den;
823 unsigned int q = i->min;
824 int diff;
825 num = mul(q, den);
826 if (num > rats[k].num_max)
827 continue;
828 if (num < rats[k].num_min)
829 num = rats[k].num_max;
830 else {
831 unsigned int r;
832 r = (num - rats[k].num_min) % rats[k].num_step;
833 if (r != 0)
834 num += rats[k].num_step - r;
835 }
836 diff = num - q * den;
837 if (best_num == 0 ||
838 diff * best_den < best_diff * den) {
839 best_diff = diff;
840 best_den = den;
841 best_num = num;
842 }
843 }
844 if (best_den == 0) {
845 i->empty = 1;
846 return -EINVAL;
847 }
848 t.min = div_down(best_num, best_den);
849 t.openmin = !!(best_num % best_den);
850
851 best_num = best_den = best_diff = 0;
852 for (k = 0; k < rats_count; ++k) {
853 unsigned int num;
854 unsigned int den = rats[k].den;
855 unsigned int q = i->max;
856 int diff;
857 num = mul(q, den);
858 if (num < rats[k].num_min)
859 continue;
860 if (num > rats[k].num_max)
861 num = rats[k].num_max;
862 else {
863 unsigned int r;
864 r = (num - rats[k].num_min) % rats[k].num_step;
865 if (r != 0)
866 num -= r;
867 }
868 diff = q * den - num;
869 if (best_num == 0 ||
870 diff * best_den < best_diff * den) {
871 best_diff = diff;
872 best_den = den;
873 best_num = num;
874 }
875 }
876 if (best_den == 0) {
877 i->empty = 1;
878 return -EINVAL;
879 }
880 t.max = div_up(best_num, best_den);
881 t.openmax = !!(best_num % best_den);
882 t.integer = 0;
883 err = snd_interval_refine(i, &t);
884 if (err < 0)
885 return err;
886
887 if (snd_interval_single(i)) {
888 if (nump)
889 *nump = best_num;
890 if (denp)
891 *denp = best_den;
892 }
893 return err;
894}
895
896/**
897 * snd_interval_list - refine the interval value from the list
898 * @i: the interval value to refine
899 * @count: the number of elements in the list
900 * @list: the value list
901 * @mask: the bit-mask to evaluate
902 *
903 * Refines the interval value from the list.
904 * When mask is non-zero, only the elements corresponding to bit 1 are
905 * evaluated.
906 *
907 * Returns non-zero if the value is changed, zero if not changed.
908 */
877211f5 909int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
1da177e4
LT
910{
911 unsigned int k;
b1ddaf68 912 struct snd_interval list_range;
0981a260
TI
913
914 if (!count) {
915 i->empty = 1;
916 return -EINVAL;
917 }
b1ddaf68
CL
918 snd_interval_any(&list_range);
919 list_range.min = UINT_MAX;
920 list_range.max = 0;
1da177e4
LT
921 for (k = 0; k < count; k++) {
922 if (mask && !(mask & (1 << k)))
923 continue;
b1ddaf68 924 if (!snd_interval_test(i, list[k]))
1da177e4 925 continue;
b1ddaf68
CL
926 list_range.min = min(list_range.min, list[k]);
927 list_range.max = max(list_range.max, list[k]);
1da177e4 928 }
b1ddaf68 929 return snd_interval_refine(i, &list_range);
1da177e4
LT
930}
931
e88e8ae6
TI
932EXPORT_SYMBOL(snd_interval_list);
933
877211f5 934static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
1da177e4
LT
935{
936 unsigned int n;
937 int changed = 0;
938 n = (i->min - min) % step;
939 if (n != 0 || i->openmin) {
940 i->min += step - n;
941 changed = 1;
942 }
943 n = (i->max - min) % step;
944 if (n != 0 || i->openmax) {
945 i->max -= n;
946 changed = 1;
947 }
948 if (snd_interval_checkempty(i)) {
949 i->empty = 1;
950 return -EINVAL;
951 }
952 return changed;
953}
954
955/* Info constraints helpers */
956
957/**
958 * snd_pcm_hw_rule_add - add the hw-constraint rule
959 * @runtime: the pcm runtime instance
960 * @cond: condition bits
961 * @var: the variable to evaluate
962 * @func: the evaluation function
963 * @private: the private data pointer passed to function
964 * @dep: the dependent variables
965 *
966 * Returns zero if successful, or a negative error code on failure.
967 */
877211f5 968int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1da177e4
LT
969 int var,
970 snd_pcm_hw_rule_func_t func, void *private,
971 int dep, ...)
972{
877211f5
TI
973 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
974 struct snd_pcm_hw_rule *c;
1da177e4
LT
975 unsigned int k;
976 va_list args;
977 va_start(args, dep);
978 if (constrs->rules_num >= constrs->rules_all) {
877211f5 979 struct snd_pcm_hw_rule *new;
1da177e4
LT
980 unsigned int new_rules = constrs->rules_all + 16;
981 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
982 if (!new)
983 return -ENOMEM;
984 if (constrs->rules) {
985 memcpy(new, constrs->rules,
986 constrs->rules_num * sizeof(*c));
987 kfree(constrs->rules);
988 }
989 constrs->rules = new;
990 constrs->rules_all = new_rules;
991 }
992 c = &constrs->rules[constrs->rules_num];
993 c->cond = cond;
994 c->func = func;
995 c->var = var;
996 c->private = private;
997 k = 0;
998 while (1) {
7eaa943c
TI
999 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1000 return -EINVAL;
1da177e4
LT
1001 c->deps[k++] = dep;
1002 if (dep < 0)
1003 break;
1004 dep = va_arg(args, int);
1005 }
1006 constrs->rules_num++;
1007 va_end(args);
1008 return 0;
1009}
1010
e88e8ae6
TI
1011EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1012
1da177e4 1013/**
1c85cc64 1014 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
df8db936
TI
1015 * @runtime: PCM runtime instance
1016 * @var: hw_params variable to apply the mask
1017 * @mask: the bitmap mask
1018 *
1c85cc64 1019 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1da177e4 1020 */
877211f5 1021int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1022 u_int32_t mask)
1023{
877211f5
TI
1024 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1025 struct snd_mask *maskp = constrs_mask(constrs, var);
1da177e4
LT
1026 *maskp->bits &= mask;
1027 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1028 if (*maskp->bits == 0)
1029 return -EINVAL;
1030 return 0;
1031}
1032
1033/**
1c85cc64 1034 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
df8db936
TI
1035 * @runtime: PCM runtime instance
1036 * @var: hw_params variable to apply the mask
1037 * @mask: the 64bit bitmap mask
1038 *
1c85cc64 1039 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1da177e4 1040 */
877211f5 1041int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1042 u_int64_t mask)
1043{
877211f5
TI
1044 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1045 struct snd_mask *maskp = constrs_mask(constrs, var);
1da177e4
LT
1046 maskp->bits[0] &= (u_int32_t)mask;
1047 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1048 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1049 if (! maskp->bits[0] && ! maskp->bits[1])
1050 return -EINVAL;
1051 return 0;
1052}
1053
1054/**
1c85cc64 1055 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
df8db936
TI
1056 * @runtime: PCM runtime instance
1057 * @var: hw_params variable to apply the integer constraint
1058 *
1059 * Apply the constraint of integer to an interval parameter.
1da177e4 1060 */
877211f5 1061int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1da177e4 1062{
877211f5 1063 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1da177e4
LT
1064 return snd_interval_setinteger(constrs_interval(constrs, var));
1065}
1066
e88e8ae6
TI
1067EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1068
1da177e4 1069/**
1c85cc64 1070 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
df8db936
TI
1071 * @runtime: PCM runtime instance
1072 * @var: hw_params variable to apply the range
1073 * @min: the minimal value
1074 * @max: the maximal value
1075 *
1076 * Apply the min/max range constraint to an interval parameter.
1da177e4 1077 */
877211f5 1078int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1da177e4
LT
1079 unsigned int min, unsigned int max)
1080{
877211f5
TI
1081 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1082 struct snd_interval t;
1da177e4
LT
1083 t.min = min;
1084 t.max = max;
1085 t.openmin = t.openmax = 0;
1086 t.integer = 0;
1087 return snd_interval_refine(constrs_interval(constrs, var), &t);
1088}
1089
e88e8ae6
TI
1090EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1091
877211f5
TI
1092static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1093 struct snd_pcm_hw_rule *rule)
1da177e4 1094{
877211f5 1095 struct snd_pcm_hw_constraint_list *list = rule->private;
1da177e4
LT
1096 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1097}
1098
1099
1100/**
1c85cc64 1101 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
df8db936
TI
1102 * @runtime: PCM runtime instance
1103 * @cond: condition bits
1104 * @var: hw_params variable to apply the list constraint
1105 * @l: list
1106 *
1107 * Apply the list of constraints to an interval parameter.
1da177e4 1108 */
877211f5 1109int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1da177e4
LT
1110 unsigned int cond,
1111 snd_pcm_hw_param_t var,
877211f5 1112 struct snd_pcm_hw_constraint_list *l)
1da177e4
LT
1113{
1114 return snd_pcm_hw_rule_add(runtime, cond, var,
1115 snd_pcm_hw_rule_list, l,
1116 var, -1);
1117}
1118
e88e8ae6
TI
1119EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1120
877211f5
TI
1121static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1122 struct snd_pcm_hw_rule *rule)
1da177e4 1123{
877211f5 1124 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1da177e4
LT
1125 unsigned int num = 0, den = 0;
1126 int err;
1127 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1128 r->nrats, r->rats, &num, &den);
1129 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1130 params->rate_num = num;
1131 params->rate_den = den;
1132 }
1133 return err;
1134}
1135
1136/**
1c85cc64 1137 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
df8db936
TI
1138 * @runtime: PCM runtime instance
1139 * @cond: condition bits
1140 * @var: hw_params variable to apply the ratnums constraint
877211f5 1141 * @r: struct snd_ratnums constriants
1da177e4 1142 */
877211f5 1143int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1da177e4
LT
1144 unsigned int cond,
1145 snd_pcm_hw_param_t var,
877211f5 1146 struct snd_pcm_hw_constraint_ratnums *r)
1da177e4
LT
1147{
1148 return snd_pcm_hw_rule_add(runtime, cond, var,
1149 snd_pcm_hw_rule_ratnums, r,
1150 var, -1);
1151}
1152
e88e8ae6
TI
1153EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1154
877211f5
TI
1155static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1156 struct snd_pcm_hw_rule *rule)
1da177e4 1157{
877211f5 1158 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1da177e4
LT
1159 unsigned int num = 0, den = 0;
1160 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1161 r->nrats, r->rats, &num, &den);
1162 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1163 params->rate_num = num;
1164 params->rate_den = den;
1165 }
1166 return err;
1167}
1168
1169/**
1c85cc64 1170 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
df8db936
TI
1171 * @runtime: PCM runtime instance
1172 * @cond: condition bits
1173 * @var: hw_params variable to apply the ratdens constraint
877211f5 1174 * @r: struct snd_ratdens constriants
1da177e4 1175 */
877211f5 1176int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1da177e4
LT
1177 unsigned int cond,
1178 snd_pcm_hw_param_t var,
877211f5 1179 struct snd_pcm_hw_constraint_ratdens *r)
1da177e4
LT
1180{
1181 return snd_pcm_hw_rule_add(runtime, cond, var,
1182 snd_pcm_hw_rule_ratdens, r,
1183 var, -1);
1184}
1185
e88e8ae6
TI
1186EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1187
877211f5
TI
1188static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1189 struct snd_pcm_hw_rule *rule)
1da177e4
LT
1190{
1191 unsigned int l = (unsigned long) rule->private;
1192 int width = l & 0xffff;
1193 unsigned int msbits = l >> 16;
877211f5 1194 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1da177e4
LT
1195 if (snd_interval_single(i) && snd_interval_value(i) == width)
1196 params->msbits = msbits;
1197 return 0;
1198}
1199
1200/**
1c85cc64 1201 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
df8db936
TI
1202 * @runtime: PCM runtime instance
1203 * @cond: condition bits
1204 * @width: sample bits width
1205 * @msbits: msbits width
1da177e4 1206 */
877211f5 1207int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1da177e4
LT
1208 unsigned int cond,
1209 unsigned int width,
1210 unsigned int msbits)
1211{
1212 unsigned long l = (msbits << 16) | width;
1213 return snd_pcm_hw_rule_add(runtime, cond, -1,
1214 snd_pcm_hw_rule_msbits,
1215 (void*) l,
1216 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1217}
1218
e88e8ae6
TI
1219EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1220
877211f5
TI
1221static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1222 struct snd_pcm_hw_rule *rule)
1da177e4
LT
1223{
1224 unsigned long step = (unsigned long) rule->private;
1225 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1226}
1227
1228/**
1c85cc64 1229 * snd_pcm_hw_constraint_step - add a hw constraint step rule
df8db936
TI
1230 * @runtime: PCM runtime instance
1231 * @cond: condition bits
1232 * @var: hw_params variable to apply the step constraint
1233 * @step: step size
1da177e4 1234 */
877211f5 1235int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1da177e4
LT
1236 unsigned int cond,
1237 snd_pcm_hw_param_t var,
1238 unsigned long step)
1239{
1240 return snd_pcm_hw_rule_add(runtime, cond, var,
1241 snd_pcm_hw_rule_step, (void *) step,
1242 var, -1);
1243}
1244
e88e8ae6
TI
1245EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1246
877211f5 1247static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1da177e4 1248{
67c39317 1249 static unsigned int pow2_sizes[] = {
1da177e4
LT
1250 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1251 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1252 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1253 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1254 };
1255 return snd_interval_list(hw_param_interval(params, rule->var),
1256 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1257}
1258
1259/**
1c85cc64 1260 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
df8db936
TI
1261 * @runtime: PCM runtime instance
1262 * @cond: condition bits
1263 * @var: hw_params variable to apply the power-of-2 constraint
1da177e4 1264 */
877211f5 1265int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1da177e4
LT
1266 unsigned int cond,
1267 snd_pcm_hw_param_t var)
1268{
1269 return snd_pcm_hw_rule_add(runtime, cond, var,
1270 snd_pcm_hw_rule_pow2, NULL,
1271 var, -1);
1272}
1273
e88e8ae6
TI
1274EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1275
877211f5 1276static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
123992f7 1277 snd_pcm_hw_param_t var)
1da177e4
LT
1278{
1279 if (hw_is_mask(var)) {
1280 snd_mask_any(hw_param_mask(params, var));
1281 params->cmask |= 1 << var;
1282 params->rmask |= 1 << var;
1283 return;
1284 }
1285 if (hw_is_interval(var)) {
1286 snd_interval_any(hw_param_interval(params, var));
1287 params->cmask |= 1 << var;
1288 params->rmask |= 1 << var;
1289 return;
1290 }
1291 snd_BUG();
1292}
1293
877211f5 1294void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1da177e4
LT
1295{
1296 unsigned int k;
1297 memset(params, 0, sizeof(*params));
1298 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1299 _snd_pcm_hw_param_any(params, k);
1300 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1301 _snd_pcm_hw_param_any(params, k);
1302 params->info = ~0U;
1303}
1304
e88e8ae6 1305EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1da177e4
LT
1306
1307/**
1c85cc64 1308 * snd_pcm_hw_param_value - return @params field @var value
df8db936
TI
1309 * @params: the hw_params instance
1310 * @var: parameter to retrieve
1c85cc64 1311 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1312 *
1c85cc64
RD
1313 * Return the value for field @var if it's fixed in configuration space
1314 * defined by @params. Return -%EINVAL otherwise.
1da177e4 1315 */
e88e8ae6
TI
1316int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1317 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1318{
1319 if (hw_is_mask(var)) {
877211f5 1320 const struct snd_mask *mask = hw_param_mask_c(params, var);
1da177e4
LT
1321 if (!snd_mask_single(mask))
1322 return -EINVAL;
1323 if (dir)
1324 *dir = 0;
1325 return snd_mask_value(mask);
1326 }
1327 if (hw_is_interval(var)) {
877211f5 1328 const struct snd_interval *i = hw_param_interval_c(params, var);
1da177e4
LT
1329 if (!snd_interval_single(i))
1330 return -EINVAL;
1331 if (dir)
1332 *dir = i->openmin;
1333 return snd_interval_value(i);
1334 }
1da177e4
LT
1335 return -EINVAL;
1336}
1337
e88e8ae6 1338EXPORT_SYMBOL(snd_pcm_hw_param_value);
1da177e4 1339
877211f5 1340void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1da177e4
LT
1341 snd_pcm_hw_param_t var)
1342{
1343 if (hw_is_mask(var)) {
1344 snd_mask_none(hw_param_mask(params, var));
1345 params->cmask |= 1 << var;
1346 params->rmask |= 1 << var;
1347 } else if (hw_is_interval(var)) {
1348 snd_interval_none(hw_param_interval(params, var));
1349 params->cmask |= 1 << var;
1350 params->rmask |= 1 << var;
1351 } else {
1352 snd_BUG();
1353 }
1354}
1355
e88e8ae6 1356EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1da177e4 1357
877211f5 1358static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
123992f7 1359 snd_pcm_hw_param_t var)
1da177e4
LT
1360{
1361 int changed;
1362 if (hw_is_mask(var))
1363 changed = snd_mask_refine_first(hw_param_mask(params, var));
1364 else if (hw_is_interval(var))
1365 changed = snd_interval_refine_first(hw_param_interval(params, var));
2f4ca8e5 1366 else
1da177e4 1367 return -EINVAL;
1da177e4
LT
1368 if (changed) {
1369 params->cmask |= 1 << var;
1370 params->rmask |= 1 << var;
1371 }
1372 return changed;
1373}
1374
1375
1376/**
1c85cc64 1377 * snd_pcm_hw_param_first - refine config space and return minimum value
df8db936
TI
1378 * @pcm: PCM instance
1379 * @params: the hw_params instance
1380 * @var: parameter to retrieve
1c85cc64 1381 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1382 *
1c85cc64 1383 * Inside configuration space defined by @params remove from @var all
1da177e4
LT
1384 * values > minimum. Reduce configuration space accordingly.
1385 * Return the minimum.
1386 */
e88e8ae6
TI
1387int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1388 struct snd_pcm_hw_params *params,
1389 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1390{
1391 int changed = _snd_pcm_hw_param_first(params, var);
1392 if (changed < 0)
1393 return changed;
1394 if (params->rmask) {
1395 int err = snd_pcm_hw_refine(pcm, params);
7eaa943c
TI
1396 if (snd_BUG_ON(err < 0))
1397 return err;
1da177e4
LT
1398 }
1399 return snd_pcm_hw_param_value(params, var, dir);
1400}
1401
e88e8ae6
TI
1402EXPORT_SYMBOL(snd_pcm_hw_param_first);
1403
877211f5 1404static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
123992f7 1405 snd_pcm_hw_param_t var)
1da177e4
LT
1406{
1407 int changed;
1408 if (hw_is_mask(var))
1409 changed = snd_mask_refine_last(hw_param_mask(params, var));
1410 else if (hw_is_interval(var))
1411 changed = snd_interval_refine_last(hw_param_interval(params, var));
2f4ca8e5 1412 else
1da177e4 1413 return -EINVAL;
1da177e4
LT
1414 if (changed) {
1415 params->cmask |= 1 << var;
1416 params->rmask |= 1 << var;
1417 }
1418 return changed;
1419}
1420
1421
1422/**
1c85cc64 1423 * snd_pcm_hw_param_last - refine config space and return maximum value
df8db936
TI
1424 * @pcm: PCM instance
1425 * @params: the hw_params instance
1426 * @var: parameter to retrieve
1c85cc64 1427 * @dir: pointer to the direction (-1,0,1) or %NULL
1da177e4 1428 *
1c85cc64 1429 * Inside configuration space defined by @params remove from @var all
1da177e4
LT
1430 * values < maximum. Reduce configuration space accordingly.
1431 * Return the maximum.
1432 */
e88e8ae6
TI
1433int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1434 struct snd_pcm_hw_params *params,
1435 snd_pcm_hw_param_t var, int *dir)
1da177e4
LT
1436{
1437 int changed = _snd_pcm_hw_param_last(params, var);
1438 if (changed < 0)
1439 return changed;
1440 if (params->rmask) {
1441 int err = snd_pcm_hw_refine(pcm, params);
7eaa943c
TI
1442 if (snd_BUG_ON(err < 0))
1443 return err;
1da177e4
LT
1444 }
1445 return snd_pcm_hw_param_value(params, var, dir);
1446}
1447
e88e8ae6 1448EXPORT_SYMBOL(snd_pcm_hw_param_last);
1da177e4
LT
1449
1450/**
1c85cc64 1451 * snd_pcm_hw_param_choose - choose a configuration defined by @params
df8db936
TI
1452 * @pcm: PCM instance
1453 * @params: the hw_params instance
1da177e4 1454 *
1c85cc64 1455 * Choose one configuration from configuration space defined by @params.
1da177e4
LT
1456 * The configuration chosen is that obtained fixing in this order:
1457 * first access, first format, first subformat, min channels,
1458 * min rate, min period time, max buffer size, min tick time
1459 */
2f4ca8e5
TI
1460int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1461 struct snd_pcm_hw_params *params)
1da177e4 1462{
2f4ca8e5
TI
1463 static int vars[] = {
1464 SNDRV_PCM_HW_PARAM_ACCESS,
1465 SNDRV_PCM_HW_PARAM_FORMAT,
1466 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1467 SNDRV_PCM_HW_PARAM_CHANNELS,
1468 SNDRV_PCM_HW_PARAM_RATE,
1469 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1470 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1471 SNDRV_PCM_HW_PARAM_TICK_TIME,
1472 -1
1473 };
1474 int err, *v;
1da177e4 1475
2f4ca8e5
TI
1476 for (v = vars; *v != -1; v++) {
1477 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1478 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1479 else
1480 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
7eaa943c
TI
1481 if (snd_BUG_ON(err < 0))
1482 return err;
2f4ca8e5 1483 }
1da177e4
LT
1484 return 0;
1485}
1486
877211f5 1487static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1da177e4
LT
1488 void *arg)
1489{
877211f5 1490 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1491 unsigned long flags;
1492 snd_pcm_stream_lock_irqsave(substream, flags);
1493 if (snd_pcm_running(substream) &&
1494 snd_pcm_update_hw_ptr(substream) >= 0)
1495 runtime->status->hw_ptr %= runtime->buffer_size;
1496 else
1497 runtime->status->hw_ptr = 0;
1498 snd_pcm_stream_unlock_irqrestore(substream, flags);
1499 return 0;
1500}
1501
877211f5 1502static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1da177e4
LT
1503 void *arg)
1504{
877211f5
TI
1505 struct snd_pcm_channel_info *info = arg;
1506 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1507 int width;
1508 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1509 info->offset = -1;
1510 return 0;
1511 }
1512 width = snd_pcm_format_physical_width(runtime->format);
1513 if (width < 0)
1514 return width;
1515 info->offset = 0;
1516 switch (runtime->access) {
1517 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1518 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1519 info->first = info->channel * width;
1520 info->step = runtime->channels * width;
1521 break;
1522 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1523 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1524 {
1525 size_t size = runtime->dma_bytes / runtime->channels;
1526 info->first = info->channel * size * 8;
1527 info->step = width;
1528 break;
1529 }
1530 default:
1531 snd_BUG();
1532 break;
1533 }
1534 return 0;
1535}
1536
8bea869c
JK
1537static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1538 void *arg)
1539{
1540 struct snd_pcm_hw_params *params = arg;
1541 snd_pcm_format_t format;
1542 int channels, width;
1543
1544 params->fifo_size = substream->runtime->hw.fifo_size;
1545 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1546 format = params_format(params);
1547 channels = params_channels(params);
1548 width = snd_pcm_format_physical_width(format);
1549 params->fifo_size /= width * channels;
1550 }
1551 return 0;
1552}
1553
1da177e4
LT
1554/**
1555 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1556 * @substream: the pcm substream instance
1557 * @cmd: ioctl command
1558 * @arg: ioctl argument
1559 *
1560 * Processes the generic ioctl commands for PCM.
1561 * Can be passed as the ioctl callback for PCM ops.
1562 *
1563 * Returns zero if successful, or a negative error code on failure.
1564 */
877211f5 1565int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
1566 unsigned int cmd, void *arg)
1567{
1568 switch (cmd) {
1569 case SNDRV_PCM_IOCTL1_INFO:
1570 return 0;
1571 case SNDRV_PCM_IOCTL1_RESET:
1572 return snd_pcm_lib_ioctl_reset(substream, arg);
1573 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1574 return snd_pcm_lib_ioctl_channel_info(substream, arg);
8bea869c
JK
1575 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1576 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1da177e4
LT
1577 }
1578 return -ENXIO;
1579}
1580
e88e8ae6
TI
1581EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1582
1da177e4
LT
1583/**
1584 * snd_pcm_period_elapsed - update the pcm status for the next period
1585 * @substream: the pcm substream instance
1586 *
1587 * This function is called from the interrupt handler when the
1588 * PCM has processed the period size. It will update the current
31e8960b 1589 * pointer, wake up sleepers, etc.
1da177e4
LT
1590 *
1591 * Even if more than one periods have elapsed since the last call, you
1592 * have to call this only once.
1593 */
877211f5 1594void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1da177e4 1595{
877211f5 1596 struct snd_pcm_runtime *runtime;
1da177e4
LT
1597 unsigned long flags;
1598
7eaa943c
TI
1599 if (PCM_RUNTIME_CHECK(substream))
1600 return;
1da177e4 1601 runtime = substream->runtime;
1da177e4
LT
1602
1603 if (runtime->transfer_ack_begin)
1604 runtime->transfer_ack_begin(substream);
1605
1606 snd_pcm_stream_lock_irqsave(substream, flags);
1607 if (!snd_pcm_running(substream) ||
1608 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1609 goto _end;
1610
1611 if (substream->timer_running)
1612 snd_timer_interrupt(substream->timer, 1);
1da177e4
LT
1613 _end:
1614 snd_pcm_stream_unlock_irqrestore(substream, flags);
1615 if (runtime->transfer_ack_end)
1616 runtime->transfer_ack_end(substream);
1617 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1618}
1619
e88e8ae6
TI
1620EXPORT_SYMBOL(snd_pcm_period_elapsed);
1621
13075510
TI
1622/*
1623 * Wait until avail_min data becomes available
1624 * Returns a negative error code if any error occurs during operation.
1625 * The available space is stored on availp. When err = 0 and avail = 0
1626 * on the capture stream, it indicates the stream is in DRAINING state.
1627 */
1628static int wait_for_avail_min(struct snd_pcm_substream *substream,
1629 snd_pcm_uframes_t *availp)
1630{
1631 struct snd_pcm_runtime *runtime = substream->runtime;
1632 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1633 wait_queue_t wait;
1634 int err = 0;
1635 snd_pcm_uframes_t avail = 0;
1636 long tout;
1637
1638 init_waitqueue_entry(&wait, current);
1639 add_wait_queue(&runtime->sleep, &wait);
1640 for (;;) {
1641 if (signal_pending(current)) {
1642 err = -ERESTARTSYS;
1643 break;
1644 }
1645 set_current_state(TASK_INTERRUPTIBLE);
1646 snd_pcm_stream_unlock_irq(substream);
1647 tout = schedule_timeout(msecs_to_jiffies(10000));
1648 snd_pcm_stream_lock_irq(substream);
1649 switch (runtime->status->state) {
1650 case SNDRV_PCM_STATE_SUSPENDED:
1651 err = -ESTRPIPE;
1652 goto _endloop;
1653 case SNDRV_PCM_STATE_XRUN:
1654 err = -EPIPE;
1655 goto _endloop;
1656 case SNDRV_PCM_STATE_DRAINING:
1657 if (is_playback)
1658 err = -EPIPE;
1659 else
1660 avail = 0; /* indicate draining */
1661 goto _endloop;
1662 case SNDRV_PCM_STATE_OPEN:
1663 case SNDRV_PCM_STATE_SETUP:
1664 case SNDRV_PCM_STATE_DISCONNECTED:
1665 err = -EBADFD;
1666 goto _endloop;
1667 }
1668 if (!tout) {
1669 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1670 is_playback ? "playback" : "capture");
1671 err = -EIO;
1672 break;
1673 }
1674 if (is_playback)
1675 avail = snd_pcm_playback_avail(runtime);
1676 else
1677 avail = snd_pcm_capture_avail(runtime);
1678 if (avail >= runtime->control->avail_min)
1679 break;
1680 }
1681 _endloop:
1682 remove_wait_queue(&runtime->sleep, &wait);
1683 *availp = avail;
1684 return err;
1685}
1686
877211f5 1687static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1da177e4
LT
1688 unsigned int hwoff,
1689 unsigned long data, unsigned int off,
1690 snd_pcm_uframes_t frames)
1691{
877211f5 1692 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1693 int err;
1694 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1695 if (substream->ops->copy) {
1696 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1697 return err;
1698 } else {
1699 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1da177e4
LT
1700 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1701 return -EFAULT;
1702 }
1703 return 0;
1704}
1705
877211f5 1706typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1da177e4
LT
1707 unsigned long data, unsigned int off,
1708 snd_pcm_uframes_t size);
1709
877211f5 1710static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1da177e4
LT
1711 unsigned long data,
1712 snd_pcm_uframes_t size,
1713 int nonblock,
1714 transfer_f transfer)
1715{
877211f5 1716 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1717 snd_pcm_uframes_t xfer = 0;
1718 snd_pcm_uframes_t offset = 0;
1719 int err = 0;
1720
1721 if (size == 0)
1722 return 0;
1da177e4
LT
1723
1724 snd_pcm_stream_lock_irq(substream);
1725 switch (runtime->status->state) {
1726 case SNDRV_PCM_STATE_PREPARED:
1727 case SNDRV_PCM_STATE_RUNNING:
1728 case SNDRV_PCM_STATE_PAUSED:
1729 break;
1730 case SNDRV_PCM_STATE_XRUN:
1731 err = -EPIPE;
1732 goto _end_unlock;
1733 case SNDRV_PCM_STATE_SUSPENDED:
1734 err = -ESTRPIPE;
1735 goto _end_unlock;
1736 default:
1737 err = -EBADFD;
1738 goto _end_unlock;
1739 }
1740
1741 while (size > 0) {
1742 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1743 snd_pcm_uframes_t avail;
1744 snd_pcm_uframes_t cont;
31e8960b 1745 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1da177e4
LT
1746 snd_pcm_update_hw_ptr(substream);
1747 avail = snd_pcm_playback_avail(runtime);
13075510 1748 if (!avail) {
1da177e4
LT
1749 if (nonblock) {
1750 err = -EAGAIN;
1751 goto _end_unlock;
1752 }
13075510
TI
1753 err = wait_for_avail_min(substream, &avail);
1754 if (err < 0)
443feb88 1755 goto _end_unlock;
1da177e4 1756 }
1da177e4
LT
1757 frames = size > avail ? avail : size;
1758 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1759 if (frames > cont)
1760 frames = cont;
7eaa943c
TI
1761 if (snd_BUG_ON(!frames)) {
1762 snd_pcm_stream_unlock_irq(substream);
1763 return -EINVAL;
1764 }
1da177e4
LT
1765 appl_ptr = runtime->control->appl_ptr;
1766 appl_ofs = appl_ptr % runtime->buffer_size;
1767 snd_pcm_stream_unlock_irq(substream);
1768 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1769 goto _end;
1770 snd_pcm_stream_lock_irq(substream);
1771 switch (runtime->status->state) {
1772 case SNDRV_PCM_STATE_XRUN:
1773 err = -EPIPE;
1774 goto _end_unlock;
1775 case SNDRV_PCM_STATE_SUSPENDED:
1776 err = -ESTRPIPE;
1777 goto _end_unlock;
1778 default:
1779 break;
1780 }
1781 appl_ptr += frames;
1782 if (appl_ptr >= runtime->boundary)
1783 appl_ptr -= runtime->boundary;
1784 runtime->control->appl_ptr = appl_ptr;
1785 if (substream->ops->ack)
1786 substream->ops->ack(substream);
1787
1788 offset += frames;
1789 size -= frames;
1790 xfer += frames;
1791 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1792 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1793 err = snd_pcm_start(substream);
1794 if (err < 0)
1795 goto _end_unlock;
1796 }
1da177e4
LT
1797 }
1798 _end_unlock:
1799 snd_pcm_stream_unlock_irq(substream);
1800 _end:
1801 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1802}
1803
7eaa943c
TI
1804/* sanity-check for read/write methods */
1805static int pcm_sanity_check(struct snd_pcm_substream *substream)
1da177e4 1806{
877211f5 1807 struct snd_pcm_runtime *runtime;
7eaa943c
TI
1808 if (PCM_RUNTIME_CHECK(substream))
1809 return -ENXIO;
1da177e4 1810 runtime = substream->runtime;
7eaa943c
TI
1811 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1812 return -EINVAL;
1da177e4
LT
1813 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1814 return -EBADFD;
7eaa943c
TI
1815 return 0;
1816}
1817
1818snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1819{
1820 struct snd_pcm_runtime *runtime;
1821 int nonblock;
1822 int err;
1da177e4 1823
7eaa943c
TI
1824 err = pcm_sanity_check(substream);
1825 if (err < 0)
1826 return err;
1827 runtime = substream->runtime;
0df63e44 1828 nonblock = !!(substream->f_flags & O_NONBLOCK);
1da177e4
LT
1829
1830 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1831 runtime->channels > 1)
1832 return -EINVAL;
1833 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1834 snd_pcm_lib_write_transfer);
1835}
1836
e88e8ae6
TI
1837EXPORT_SYMBOL(snd_pcm_lib_write);
1838
877211f5 1839static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1da177e4
LT
1840 unsigned int hwoff,
1841 unsigned long data, unsigned int off,
1842 snd_pcm_uframes_t frames)
1843{
877211f5 1844 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1845 int err;
1846 void __user **bufs = (void __user **)data;
1847 int channels = runtime->channels;
1848 int c;
1849 if (substream->ops->copy) {
7eaa943c
TI
1850 if (snd_BUG_ON(!substream->ops->silence))
1851 return -EINVAL;
1da177e4
LT
1852 for (c = 0; c < channels; ++c, ++bufs) {
1853 if (*bufs == NULL) {
1854 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1855 return err;
1856 } else {
1857 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1858 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1859 return err;
1860 }
1861 }
1862 } else {
1863 /* default transfer behaviour */
1864 size_t dma_csize = runtime->dma_bytes / channels;
1da177e4
LT
1865 for (c = 0; c < channels; ++c, ++bufs) {
1866 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1867 if (*bufs == NULL) {
1868 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1869 } else {
1870 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1871 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1872 return -EFAULT;
1873 }
1874 }
1875 }
1876 return 0;
1877}
1878
877211f5 1879snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1da177e4
LT
1880 void __user **bufs,
1881 snd_pcm_uframes_t frames)
1882{
877211f5 1883 struct snd_pcm_runtime *runtime;
1da177e4 1884 int nonblock;
7eaa943c 1885 int err;
1da177e4 1886
7eaa943c
TI
1887 err = pcm_sanity_check(substream);
1888 if (err < 0)
1889 return err;
1da177e4 1890 runtime = substream->runtime;
0df63e44 1891 nonblock = !!(substream->f_flags & O_NONBLOCK);
1da177e4
LT
1892
1893 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1894 return -EINVAL;
1895 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1896 nonblock, snd_pcm_lib_writev_transfer);
1897}
1898
e88e8ae6
TI
1899EXPORT_SYMBOL(snd_pcm_lib_writev);
1900
877211f5 1901static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1da177e4
LT
1902 unsigned int hwoff,
1903 unsigned long data, unsigned int off,
1904 snd_pcm_uframes_t frames)
1905{
877211f5 1906 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1907 int err;
1908 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1909 if (substream->ops->copy) {
1910 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1911 return err;
1912 } else {
1913 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1da177e4
LT
1914 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1915 return -EFAULT;
1916 }
1917 return 0;
1918}
1919
877211f5 1920static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1da177e4
LT
1921 unsigned long data,
1922 snd_pcm_uframes_t size,
1923 int nonblock,
1924 transfer_f transfer)
1925{
877211f5 1926 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1927 snd_pcm_uframes_t xfer = 0;
1928 snd_pcm_uframes_t offset = 0;
1929 int err = 0;
1930
1931 if (size == 0)
1932 return 0;
1da177e4
LT
1933
1934 snd_pcm_stream_lock_irq(substream);
1935 switch (runtime->status->state) {
1936 case SNDRV_PCM_STATE_PREPARED:
1937 if (size >= runtime->start_threshold) {
1938 err = snd_pcm_start(substream);
1939 if (err < 0)
1940 goto _end_unlock;
1941 }
1942 break;
1943 case SNDRV_PCM_STATE_DRAINING:
1944 case SNDRV_PCM_STATE_RUNNING:
1945 case SNDRV_PCM_STATE_PAUSED:
1946 break;
1947 case SNDRV_PCM_STATE_XRUN:
1948 err = -EPIPE;
1949 goto _end_unlock;
1950 case SNDRV_PCM_STATE_SUSPENDED:
1951 err = -ESTRPIPE;
1952 goto _end_unlock;
1953 default:
1954 err = -EBADFD;
1955 goto _end_unlock;
1956 }
1957
1958 while (size > 0) {
1959 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1960 snd_pcm_uframes_t avail;
1961 snd_pcm_uframes_t cont;
31e8960b 1962 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1da177e4 1963 snd_pcm_update_hw_ptr(substream);
1da177e4 1964 avail = snd_pcm_capture_avail(runtime);
13075510
TI
1965 if (!avail) {
1966 if (runtime->status->state ==
1967 SNDRV_PCM_STATE_DRAINING) {
1968 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1da177e4
LT
1969 goto _end_unlock;
1970 }
1da177e4
LT
1971 if (nonblock) {
1972 err = -EAGAIN;
1973 goto _end_unlock;
1974 }
13075510
TI
1975 err = wait_for_avail_min(substream, &avail);
1976 if (err < 0)
443feb88 1977 goto _end_unlock;
13075510
TI
1978 if (!avail)
1979 continue; /* draining */
1da177e4 1980 }
1da177e4
LT
1981 frames = size > avail ? avail : size;
1982 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1983 if (frames > cont)
1984 frames = cont;
7eaa943c
TI
1985 if (snd_BUG_ON(!frames)) {
1986 snd_pcm_stream_unlock_irq(substream);
1987 return -EINVAL;
1988 }
1da177e4
LT
1989 appl_ptr = runtime->control->appl_ptr;
1990 appl_ofs = appl_ptr % runtime->buffer_size;
1991 snd_pcm_stream_unlock_irq(substream);
1992 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1993 goto _end;
1994 snd_pcm_stream_lock_irq(substream);
1995 switch (runtime->status->state) {
1996 case SNDRV_PCM_STATE_XRUN:
1997 err = -EPIPE;
1998 goto _end_unlock;
1999 case SNDRV_PCM_STATE_SUSPENDED:
2000 err = -ESTRPIPE;
2001 goto _end_unlock;
2002 default:
2003 break;
2004 }
2005 appl_ptr += frames;
2006 if (appl_ptr >= runtime->boundary)
2007 appl_ptr -= runtime->boundary;
2008 runtime->control->appl_ptr = appl_ptr;
2009 if (substream->ops->ack)
2010 substream->ops->ack(substream);
2011
2012 offset += frames;
2013 size -= frames;
2014 xfer += frames;
1da177e4
LT
2015 }
2016 _end_unlock:
2017 snd_pcm_stream_unlock_irq(substream);
2018 _end:
2019 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2020}
2021
877211f5 2022snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1da177e4 2023{
877211f5 2024 struct snd_pcm_runtime *runtime;
1da177e4 2025 int nonblock;
7eaa943c 2026 int err;
1da177e4 2027
7eaa943c
TI
2028 err = pcm_sanity_check(substream);
2029 if (err < 0)
2030 return err;
1da177e4 2031 runtime = substream->runtime;
0df63e44 2032 nonblock = !!(substream->f_flags & O_NONBLOCK);
1da177e4
LT
2033 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2034 return -EINVAL;
2035 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2036}
2037
e88e8ae6
TI
2038EXPORT_SYMBOL(snd_pcm_lib_read);
2039
877211f5 2040static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1da177e4
LT
2041 unsigned int hwoff,
2042 unsigned long data, unsigned int off,
2043 snd_pcm_uframes_t frames)
2044{
877211f5 2045 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2046 int err;
2047 void __user **bufs = (void __user **)data;
2048 int channels = runtime->channels;
2049 int c;
2050 if (substream->ops->copy) {
2051 for (c = 0; c < channels; ++c, ++bufs) {
2052 char __user *buf;
2053 if (*bufs == NULL)
2054 continue;
2055 buf = *bufs + samples_to_bytes(runtime, off);
2056 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2057 return err;
2058 }
2059 } else {
2060 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1da177e4
LT
2061 for (c = 0; c < channels; ++c, ++bufs) {
2062 char *hwbuf;
2063 char __user *buf;
2064 if (*bufs == NULL)
2065 continue;
2066
2067 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2068 buf = *bufs + samples_to_bytes(runtime, off);
2069 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2070 return -EFAULT;
2071 }
2072 }
2073 return 0;
2074}
2075
877211f5 2076snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1da177e4
LT
2077 void __user **bufs,
2078 snd_pcm_uframes_t frames)
2079{
877211f5 2080 struct snd_pcm_runtime *runtime;
1da177e4 2081 int nonblock;
7eaa943c 2082 int err;
1da177e4 2083
7eaa943c
TI
2084 err = pcm_sanity_check(substream);
2085 if (err < 0)
2086 return err;
1da177e4 2087 runtime = substream->runtime;
1da177e4
LT
2088 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2089 return -EBADFD;
2090
0df63e44 2091 nonblock = !!(substream->f_flags & O_NONBLOCK);
1da177e4
LT
2092 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2093 return -EINVAL;
2094 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2095}
2096
1da177e4 2097EXPORT_SYMBOL(snd_pcm_lib_readv);
This page took 1.148724 seconds and 5 git commands to generate.