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