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