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