blob: 6ea5cfb83998ad04fa0678ab522b4e734a737180 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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 */
Takashi Iwai877211f2005-11-17 13:59:38 +010041void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai877211f2005-11-17 13:59:38 +010043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 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 }
Takashi Iwai235475c2005-12-07 15:28:07 +010058 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 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;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020081 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020083 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 frames = runtime->buffer_size - runtime->silence_filled;
87 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020088 if (snd_BUG_ON(frames > runtime->buffer_size))
89 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (frames == 0)
91 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020092 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 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);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200100 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } 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);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200112 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
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
Takashi Iwai877211f2005-11-17 13:59:38 +0100128static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200131#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (substream->pstr->xrun_debug) {
133 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
134 substream->pcm->card->number,
135 substream->pcm->device,
136 substream->stream ? 'c' : 'p');
137 if (substream->pstr->xrun_debug > 1)
138 dump_stack();
139 }
140#endif
141}
142
Takashi Iwai877211f2005-11-17 13:59:38 +0100143static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
144 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 snd_pcm_uframes_t pos;
147
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100148 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
149 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 pos = substream->ops->pointer(substream);
151 if (pos == SNDRV_PCM_POS_XRUN)
152 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#ifdef CONFIG_SND_DEBUG
154 if (pos >= runtime->buffer_size) {
155 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);
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 pos -= pos % runtime->min_align;
159 return pos;
160}
161
Takashi Iwai877211f2005-11-17 13:59:38 +0100162static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
163 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 snd_pcm_uframes_t avail;
166
167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
168 avail = snd_pcm_playback_avail(runtime);
169 else
170 avail = snd_pcm_capture_avail(runtime);
171 if (avail > runtime->avail_max)
172 runtime->avail_max = avail;
173 if (avail >= runtime->stop_threshold) {
174 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
175 snd_pcm_drain_done(substream);
176 else
177 xrun(substream);
178 return -EPIPE;
179 }
180 if (avail >= runtime->control->avail_min)
181 wake_up(&runtime->sleep);
182 return 0;
183}
184
Takashi Iwai877211f2005-11-17 13:59:38 +0100185static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Takashi Iwai877211f2005-11-17 13:59:38 +0100187 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 snd_pcm_uframes_t pos;
189 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
190 snd_pcm_sframes_t delta;
191
192 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
193 if (pos == SNDRV_PCM_POS_XRUN) {
194 xrun(substream);
195 return -EPIPE;
196 }
197 if (runtime->period_size == runtime->buffer_size)
198 goto __next_buf;
199 new_hw_ptr = runtime->hw_ptr_base + pos;
200 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
201
202 delta = hw_ptr_interrupt - new_hw_ptr;
203 if (delta > 0) {
204 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200205#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
207 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);
208 if (substream->pstr->xrun_debug > 1)
209 dump_stack();
210 }
211#endif
212 return 0;
213 }
214 __next_buf:
215 runtime->hw_ptr_base += runtime->buffer_size;
216 if (runtime->hw_ptr_base == runtime->boundary)
217 runtime->hw_ptr_base = 0;
218 new_hw_ptr = runtime->hw_ptr_base + pos;
219 }
220
221 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
222 runtime->silence_size > 0)
223 snd_pcm_playback_silence(substream, new_hw_ptr);
224
225 runtime->status->hw_ptr = new_hw_ptr;
226 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
227
228 return snd_pcm_update_hw_ptr_post(substream, runtime);
229}
230
231/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100232int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Takashi Iwai877211f2005-11-17 13:59:38 +0100234 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 snd_pcm_uframes_t pos;
236 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
237 snd_pcm_sframes_t delta;
238
239 old_hw_ptr = runtime->status->hw_ptr;
240 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
241 if (pos == SNDRV_PCM_POS_XRUN) {
242 xrun(substream);
243 return -EPIPE;
244 }
245 new_hw_ptr = runtime->hw_ptr_base + pos;
246
247 delta = old_hw_ptr - new_hw_ptr;
248 if (delta > 0) {
249 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200250#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
252 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);
253 if (substream->pstr->xrun_debug > 1)
254 dump_stack();
255 }
256#endif
257 return 0;
258 }
259 runtime->hw_ptr_base += runtime->buffer_size;
260 if (runtime->hw_ptr_base == runtime->boundary)
261 runtime->hw_ptr_base = 0;
262 new_hw_ptr = runtime->hw_ptr_base + pos;
263 }
264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
265 runtime->silence_size > 0)
266 snd_pcm_playback_silence(substream, new_hw_ptr);
267
268 runtime->status->hw_ptr = new_hw_ptr;
269
270 return snd_pcm_update_hw_ptr_post(substream, runtime);
271}
272
273/**
274 * snd_pcm_set_ops - set the PCM operators
275 * @pcm: the pcm instance
276 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
277 * @ops: the operator table
278 *
279 * Sets the given PCM operators to the pcm instance.
280 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100281void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Takashi Iwai877211f2005-11-17 13:59:38 +0100283 struct snd_pcm_str *stream = &pcm->streams[direction];
284 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 for (substream = stream->substream; substream != NULL; substream = substream->next)
287 substream->ops = ops;
288}
289
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200290EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
293 * snd_pcm_sync - set the PCM sync id
294 * @substream: the pcm substream
295 *
296 * Sets the PCM sync identifier for the card.
297 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100298void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Takashi Iwai877211f2005-11-17 13:59:38 +0100300 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 runtime->sync.id32[0] = substream->pcm->card->number;
303 runtime->sync.id32[1] = -1;
304 runtime->sync.id32[2] = -1;
305 runtime->sync.id32[3] = -1;
306}
307
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200308EXPORT_SYMBOL(snd_pcm_set_sync);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * Standard ioctl routine
312 */
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static inline unsigned int div32(unsigned int a, unsigned int b,
315 unsigned int *r)
316{
317 if (b == 0) {
318 *r = 0;
319 return UINT_MAX;
320 }
321 *r = a % b;
322 return a / b;
323}
324
325static inline unsigned int div_down(unsigned int a, unsigned int b)
326{
327 if (b == 0)
328 return UINT_MAX;
329 return a / b;
330}
331
332static inline unsigned int div_up(unsigned int a, unsigned int b)
333{
334 unsigned int r;
335 unsigned int q;
336 if (b == 0)
337 return UINT_MAX;
338 q = div32(a, b, &r);
339 if (r)
340 ++q;
341 return q;
342}
343
344static inline unsigned int mul(unsigned int a, unsigned int b)
345{
346 if (a == 0)
347 return 0;
348 if (div_down(UINT_MAX, a) < b)
349 return UINT_MAX;
350 return a * b;
351}
352
353static inline unsigned int muldiv32(unsigned int a, unsigned int b,
354 unsigned int c, unsigned int *r)
355{
356 u_int64_t n = (u_int64_t) a * b;
357 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200358 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *r = 0;
360 return UINT_MAX;
361 }
362 div64_32(&n, c, r);
363 if (n >= UINT_MAX) {
364 *r = 0;
365 return UINT_MAX;
366 }
367 return n;
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
371 * snd_interval_refine - refine the interval value of configurator
372 * @i: the interval value to refine
373 * @v: the interval value to refer to
374 *
375 * Refines the interval value with the reference value.
376 * The interval is changed to the range satisfying both intervals.
377 * The interval status (min, max, integer, etc.) are evaluated.
378 *
379 * Returns non-zero if the value is changed, zero if not changed.
380 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100381int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200384 if (snd_BUG_ON(snd_interval_empty(i)))
385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (i->min < v->min) {
387 i->min = v->min;
388 i->openmin = v->openmin;
389 changed = 1;
390 } else if (i->min == v->min && !i->openmin && v->openmin) {
391 i->openmin = 1;
392 changed = 1;
393 }
394 if (i->max > v->max) {
395 i->max = v->max;
396 i->openmax = v->openmax;
397 changed = 1;
398 } else if (i->max == v->max && !i->openmax && v->openmax) {
399 i->openmax = 1;
400 changed = 1;
401 }
402 if (!i->integer && v->integer) {
403 i->integer = 1;
404 changed = 1;
405 }
406 if (i->integer) {
407 if (i->openmin) {
408 i->min++;
409 i->openmin = 0;
410 }
411 if (i->openmax) {
412 i->max--;
413 i->openmax = 0;
414 }
415 } else if (!i->openmin && !i->openmax && i->min == i->max)
416 i->integer = 1;
417 if (snd_interval_checkempty(i)) {
418 snd_interval_none(i);
419 return -EINVAL;
420 }
421 return changed;
422}
423
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200424EXPORT_SYMBOL(snd_interval_refine);
425
Takashi Iwai877211f2005-11-17 13:59:38 +0100426static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200428 if (snd_BUG_ON(snd_interval_empty(i)))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 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
Takashi Iwai877211f2005-11-17 13:59:38 +0100439static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200441 if (snd_BUG_ON(snd_interval_empty(i)))
442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (snd_interval_single(i))
444 return 0;
445 i->min = i->max;
446 i->openmin = i->openmax;
447 if (i->openmin)
448 i->min--;
449 return 1;
450}
451
Takashi Iwai877211f2005-11-17 13:59:38 +0100452void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 if (a->empty || b->empty) {
455 snd_interval_none(c);
456 return;
457 }
458 c->empty = 0;
459 c->min = mul(a->min, b->min);
460 c->openmin = (a->openmin || b->openmin);
461 c->max = mul(a->max, b->max);
462 c->openmax = (a->openmax || b->openmax);
463 c->integer = (a->integer && b->integer);
464}
465
466/**
467 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200468 * @a: dividend
469 * @b: divisor
470 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
472 * c = a / b
473 *
474 * Returns non-zero if the value is changed, zero if not changed.
475 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100476void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned int r;
479 if (a->empty || b->empty) {
480 snd_interval_none(c);
481 return;
482 }
483 c->empty = 0;
484 c->min = div32(a->min, b->max, &r);
485 c->openmin = (r || a->openmin || b->openmax);
486 if (b->min > 0) {
487 c->max = div32(a->max, b->min, &r);
488 if (r) {
489 c->max++;
490 c->openmax = 1;
491 } else
492 c->openmax = (a->openmax || b->openmin);
493 } else {
494 c->max = UINT_MAX;
495 c->openmax = 0;
496 }
497 c->integer = 0;
498}
499
500/**
501 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200502 * @a: dividend 1
503 * @b: dividend 2
504 * @k: divisor (as integer)
505 * @c: result
506 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * c = a * b / k
508 *
509 * Returns non-zero if the value is changed, zero if not changed.
510 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100511void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
512 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int r;
515 if (a->empty || b->empty) {
516 snd_interval_none(c);
517 return;
518 }
519 c->empty = 0;
520 c->min = muldiv32(a->min, b->min, k, &r);
521 c->openmin = (r || a->openmin || b->openmin);
522 c->max = muldiv32(a->max, b->max, k, &r);
523 if (r) {
524 c->max++;
525 c->openmax = 1;
526 } else
527 c->openmax = (a->openmax || b->openmax);
528 c->integer = 0;
529}
530
531/**
532 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200533 * @a: dividend 1
534 * @k: dividend 2 (as integer)
535 * @b: divisor
536 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *
538 * c = a * k / b
539 *
540 * Returns non-zero if the value is changed, zero if not changed.
541 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100542void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
543 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 unsigned int r;
546 if (a->empty || b->empty) {
547 snd_interval_none(c);
548 return;
549 }
550 c->empty = 0;
551 c->min = muldiv32(a->min, k, b->max, &r);
552 c->openmin = (r || a->openmin || b->openmax);
553 if (b->min > 0) {
554 c->max = muldiv32(a->max, k, b->min, &r);
555 if (r) {
556 c->max++;
557 c->openmax = 1;
558 } else
559 c->openmax = (a->openmax || b->openmin);
560 } else {
561 c->max = UINT_MAX;
562 c->openmax = 0;
563 }
564 c->integer = 0;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/* ---- */
568
569
570/**
571 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200572 * @i: interval to refine
573 * @rats_count: number of ratnum_t
574 * @rats: ratnum_t array
575 * @nump: pointer to store the resultant numerator
576 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * Returns non-zero if the value is changed, zero if not changed.
579 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100580int snd_interval_ratnum(struct snd_interval *i,
581 unsigned int rats_count, struct snd_ratnum *rats,
582 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 unsigned int best_num, best_diff, best_den;
585 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100586 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int err;
588
589 best_num = best_den = best_diff = 0;
590 for (k = 0; k < rats_count; ++k) {
591 unsigned int num = rats[k].num;
592 unsigned int den;
593 unsigned int q = i->min;
594 int diff;
595 if (q == 0)
596 q = 1;
597 den = div_down(num, q);
598 if (den < rats[k].den_min)
599 continue;
600 if (den > rats[k].den_max)
601 den = rats[k].den_max;
602 else {
603 unsigned int r;
604 r = (den - rats[k].den_min) % rats[k].den_step;
605 if (r != 0)
606 den -= r;
607 }
608 diff = num - q * den;
609 if (best_num == 0 ||
610 diff * best_den < best_diff * den) {
611 best_diff = diff;
612 best_den = den;
613 best_num = num;
614 }
615 }
616 if (best_den == 0) {
617 i->empty = 1;
618 return -EINVAL;
619 }
620 t.min = div_down(best_num, best_den);
621 t.openmin = !!(best_num % best_den);
622
623 best_num = best_den = best_diff = 0;
624 for (k = 0; k < rats_count; ++k) {
625 unsigned int num = rats[k].num;
626 unsigned int den;
627 unsigned int q = i->max;
628 int diff;
629 if (q == 0) {
630 i->empty = 1;
631 return -EINVAL;
632 }
633 den = div_up(num, q);
634 if (den > rats[k].den_max)
635 continue;
636 if (den < rats[k].den_min)
637 den = rats[k].den_min;
638 else {
639 unsigned int r;
640 r = (den - rats[k].den_min) % rats[k].den_step;
641 if (r != 0)
642 den += rats[k].den_step - r;
643 }
644 diff = q * den - num;
645 if (best_num == 0 ||
646 diff * best_den < best_diff * den) {
647 best_diff = diff;
648 best_den = den;
649 best_num = num;
650 }
651 }
652 if (best_den == 0) {
653 i->empty = 1;
654 return -EINVAL;
655 }
656 t.max = div_up(best_num, best_den);
657 t.openmax = !!(best_num % best_den);
658 t.integer = 0;
659 err = snd_interval_refine(i, &t);
660 if (err < 0)
661 return err;
662
663 if (snd_interval_single(i)) {
664 if (nump)
665 *nump = best_num;
666 if (denp)
667 *denp = best_den;
668 }
669 return err;
670}
671
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200672EXPORT_SYMBOL(snd_interval_ratnum);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
675 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200676 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 * @rats_count: number of struct ratden
678 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200679 * @nump: pointer to store the resultant numerator
680 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *
682 * Returns non-zero if the value is changed, zero if not changed.
683 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100684static int snd_interval_ratden(struct snd_interval *i,
685 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned int *nump, unsigned int *denp)
687{
688 unsigned int best_num, best_diff, best_den;
689 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100690 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int err;
692
693 best_num = best_den = best_diff = 0;
694 for (k = 0; k < rats_count; ++k) {
695 unsigned int num;
696 unsigned int den = rats[k].den;
697 unsigned int q = i->min;
698 int diff;
699 num = mul(q, den);
700 if (num > rats[k].num_max)
701 continue;
702 if (num < rats[k].num_min)
703 num = rats[k].num_max;
704 else {
705 unsigned int r;
706 r = (num - rats[k].num_min) % rats[k].num_step;
707 if (r != 0)
708 num += rats[k].num_step - r;
709 }
710 diff = num - q * den;
711 if (best_num == 0 ||
712 diff * best_den < best_diff * den) {
713 best_diff = diff;
714 best_den = den;
715 best_num = num;
716 }
717 }
718 if (best_den == 0) {
719 i->empty = 1;
720 return -EINVAL;
721 }
722 t.min = div_down(best_num, best_den);
723 t.openmin = !!(best_num % best_den);
724
725 best_num = best_den = best_diff = 0;
726 for (k = 0; k < rats_count; ++k) {
727 unsigned int num;
728 unsigned int den = rats[k].den;
729 unsigned int q = i->max;
730 int diff;
731 num = mul(q, den);
732 if (num < rats[k].num_min)
733 continue;
734 if (num > rats[k].num_max)
735 num = rats[k].num_max;
736 else {
737 unsigned int r;
738 r = (num - rats[k].num_min) % rats[k].num_step;
739 if (r != 0)
740 num -= r;
741 }
742 diff = q * den - num;
743 if (best_num == 0 ||
744 diff * best_den < best_diff * den) {
745 best_diff = diff;
746 best_den = den;
747 best_num = num;
748 }
749 }
750 if (best_den == 0) {
751 i->empty = 1;
752 return -EINVAL;
753 }
754 t.max = div_up(best_num, best_den);
755 t.openmax = !!(best_num % best_den);
756 t.integer = 0;
757 err = snd_interval_refine(i, &t);
758 if (err < 0)
759 return err;
760
761 if (snd_interval_single(i)) {
762 if (nump)
763 *nump = best_num;
764 if (denp)
765 *denp = best_den;
766 }
767 return err;
768}
769
770/**
771 * snd_interval_list - refine the interval value from the list
772 * @i: the interval value to refine
773 * @count: the number of elements in the list
774 * @list: the value list
775 * @mask: the bit-mask to evaluate
776 *
777 * Refines the interval value from the list.
778 * When mask is non-zero, only the elements corresponding to bit 1 are
779 * evaluated.
780 *
781 * Returns non-zero if the value is changed, zero if not changed.
782 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100783int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 unsigned int k;
786 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100787
788 if (!count) {
789 i->empty = 1;
790 return -EINVAL;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 for (k = 0; k < count; k++) {
793 if (mask && !(mask & (1 << k)))
794 continue;
795 if (i->min == list[k] && !i->openmin)
796 goto _l1;
797 if (i->min < list[k]) {
798 i->min = list[k];
799 i->openmin = 0;
800 changed = 1;
801 goto _l1;
802 }
803 }
804 i->empty = 1;
805 return -EINVAL;
806 _l1:
807 for (k = count; k-- > 0;) {
808 if (mask && !(mask & (1 << k)))
809 continue;
810 if (i->max == list[k] && !i->openmax)
811 goto _l2;
812 if (i->max > list[k]) {
813 i->max = list[k];
814 i->openmax = 0;
815 changed = 1;
816 goto _l2;
817 }
818 }
819 i->empty = 1;
820 return -EINVAL;
821 _l2:
822 if (snd_interval_checkempty(i)) {
823 i->empty = 1;
824 return -EINVAL;
825 }
826 return changed;
827}
828
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200829EXPORT_SYMBOL(snd_interval_list);
830
Takashi Iwai877211f2005-11-17 13:59:38 +0100831static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 unsigned int n;
834 int changed = 0;
835 n = (i->min - min) % step;
836 if (n != 0 || i->openmin) {
837 i->min += step - n;
838 changed = 1;
839 }
840 n = (i->max - min) % step;
841 if (n != 0 || i->openmax) {
842 i->max -= n;
843 changed = 1;
844 }
845 if (snd_interval_checkempty(i)) {
846 i->empty = 1;
847 return -EINVAL;
848 }
849 return changed;
850}
851
852/* Info constraints helpers */
853
854/**
855 * snd_pcm_hw_rule_add - add the hw-constraint rule
856 * @runtime: the pcm runtime instance
857 * @cond: condition bits
858 * @var: the variable to evaluate
859 * @func: the evaluation function
860 * @private: the private data pointer passed to function
861 * @dep: the dependent variables
862 *
863 * Returns zero if successful, or a negative error code on failure.
864 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100865int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int var,
867 snd_pcm_hw_rule_func_t func, void *private,
868 int dep, ...)
869{
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
871 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned int k;
873 va_list args;
874 va_start(args, dep);
875 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100876 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 unsigned int new_rules = constrs->rules_all + 16;
878 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
879 if (!new)
880 return -ENOMEM;
881 if (constrs->rules) {
882 memcpy(new, constrs->rules,
883 constrs->rules_num * sizeof(*c));
884 kfree(constrs->rules);
885 }
886 constrs->rules = new;
887 constrs->rules_all = new_rules;
888 }
889 c = &constrs->rules[constrs->rules_num];
890 c->cond = cond;
891 c->func = func;
892 c->var = var;
893 c->private = private;
894 k = 0;
895 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200896 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 c->deps[k++] = dep;
899 if (dep < 0)
900 break;
901 dep = va_arg(args, int);
902 }
903 constrs->rules_num++;
904 va_end(args);
905 return 0;
906}
907
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200908EXPORT_SYMBOL(snd_pcm_hw_rule_add);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/**
911 * snd_pcm_hw_constraint_mask
Takashi Iwaidf8db932005-09-07 13:38:19 +0200912 * @runtime: PCM runtime instance
913 * @var: hw_params variable to apply the mask
914 * @mask: the bitmap mask
915 *
916 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100918int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 u_int32_t mask)
920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
922 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *maskp->bits &= mask;
924 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
925 if (*maskp->bits == 0)
926 return -EINVAL;
927 return 0;
928}
929
930/**
931 * snd_pcm_hw_constraint_mask64
Takashi Iwaidf8db932005-09-07 13:38:19 +0200932 * @runtime: PCM runtime instance
933 * @var: hw_params variable to apply the mask
934 * @mask: the 64bit bitmap mask
935 *
936 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100938int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 u_int64_t mask)
940{
Takashi Iwai877211f2005-11-17 13:59:38 +0100941 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
942 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 maskp->bits[0] &= (u_int32_t)mask;
944 maskp->bits[1] &= (u_int32_t)(mask >> 32);
945 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
946 if (! maskp->bits[0] && ! maskp->bits[1])
947 return -EINVAL;
948 return 0;
949}
950
951/**
952 * snd_pcm_hw_constraint_integer
Takashi Iwaidf8db932005-09-07 13:38:19 +0200953 * @runtime: PCM runtime instance
954 * @var: hw_params variable to apply the integer constraint
955 *
956 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100958int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Takashi Iwai877211f2005-11-17 13:59:38 +0100960 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return snd_interval_setinteger(constrs_interval(constrs, var));
962}
963
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200964EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/**
967 * snd_pcm_hw_constraint_minmax
Takashi Iwaidf8db932005-09-07 13:38:19 +0200968 * @runtime: PCM runtime instance
969 * @var: hw_params variable to apply the range
970 * @min: the minimal value
971 * @max: the maximal value
972 *
973 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100975int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned int min, unsigned int max)
977{
Takashi Iwai877211f2005-11-17 13:59:38 +0100978 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
979 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 t.min = min;
981 t.max = max;
982 t.openmin = t.openmax = 0;
983 t.integer = 0;
984 return snd_interval_refine(constrs_interval(constrs, var), &t);
985}
986
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200987EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
988
Takashi Iwai877211f2005-11-17 13:59:38 +0100989static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
990 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Takashi Iwai877211f2005-11-17 13:59:38 +0100992 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
994}
995
996
997/**
998 * snd_pcm_hw_constraint_list
Takashi Iwaidf8db932005-09-07 13:38:19 +0200999 * @runtime: PCM runtime instance
1000 * @cond: condition bits
1001 * @var: hw_params variable to apply the list constraint
1002 * @l: list
1003 *
1004 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001006int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 unsigned int cond,
1008 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 return snd_pcm_hw_rule_add(runtime, cond, var,
1012 snd_pcm_hw_rule_list, l,
1013 var, -1);
1014}
1015
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001016EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1017
Takashi Iwai877211f2005-11-17 13:59:38 +01001018static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1019 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Takashi Iwai877211f2005-11-17 13:59:38 +01001021 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 unsigned int num = 0, den = 0;
1023 int err;
1024 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1025 r->nrats, r->rats, &num, &den);
1026 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1027 params->rate_num = num;
1028 params->rate_den = den;
1029 }
1030 return err;
1031}
1032
1033/**
1034 * snd_pcm_hw_constraint_ratnums
Takashi Iwaidf8db932005-09-07 13:38:19 +02001035 * @runtime: PCM runtime instance
1036 * @cond: condition bits
1037 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001038 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001040int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned int cond,
1042 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001043 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 return snd_pcm_hw_rule_add(runtime, cond, var,
1046 snd_pcm_hw_rule_ratnums, r,
1047 var, -1);
1048}
1049
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001050EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1051
Takashi Iwai877211f2005-11-17 13:59:38 +01001052static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1053 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Takashi Iwai877211f2005-11-17 13:59:38 +01001055 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unsigned int num = 0, den = 0;
1057 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1058 r->nrats, r->rats, &num, &den);
1059 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1060 params->rate_num = num;
1061 params->rate_den = den;
1062 }
1063 return err;
1064}
1065
1066/**
1067 * snd_pcm_hw_constraint_ratdens
Takashi Iwaidf8db932005-09-07 13:38:19 +02001068 * @runtime: PCM runtime instance
1069 * @cond: condition bits
1070 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001073int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned int cond,
1075 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001076 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 return snd_pcm_hw_rule_add(runtime, cond, var,
1079 snd_pcm_hw_rule_ratdens, r,
1080 var, -1);
1081}
1082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001083EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1084
Takashi Iwai877211f2005-11-17 13:59:38 +01001085static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1086 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 unsigned int l = (unsigned long) rule->private;
1089 int width = l & 0xffff;
1090 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001091 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (snd_interval_single(i) && snd_interval_value(i) == width)
1093 params->msbits = msbits;
1094 return 0;
1095}
1096
1097/**
1098 * snd_pcm_hw_constraint_msbits
Takashi Iwaidf8db932005-09-07 13:38:19 +02001099 * @runtime: PCM runtime instance
1100 * @cond: condition bits
1101 * @width: sample bits width
1102 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001104int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unsigned int cond,
1106 unsigned int width,
1107 unsigned int msbits)
1108{
1109 unsigned long l = (msbits << 16) | width;
1110 return snd_pcm_hw_rule_add(runtime, cond, -1,
1111 snd_pcm_hw_rule_msbits,
1112 (void*) l,
1113 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1114}
1115
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001116EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1117
Takashi Iwai877211f2005-11-17 13:59:38 +01001118static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1119 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 unsigned long step = (unsigned long) rule->private;
1122 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1123}
1124
1125/**
1126 * snd_pcm_hw_constraint_step
Takashi Iwaidf8db932005-09-07 13:38:19 +02001127 * @runtime: PCM runtime instance
1128 * @cond: condition bits
1129 * @var: hw_params variable to apply the step constraint
1130 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001132int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned int cond,
1134 snd_pcm_hw_param_t var,
1135 unsigned long step)
1136{
1137 return snd_pcm_hw_rule_add(runtime, cond, var,
1138 snd_pcm_hw_rule_step, (void *) step,
1139 var, -1);
1140}
1141
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001142EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1143
Takashi Iwai877211f2005-11-17 13:59:38 +01001144static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001146 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1148 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1149 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1150 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1151 };
1152 return snd_interval_list(hw_param_interval(params, rule->var),
1153 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1154}
1155
1156/**
1157 * snd_pcm_hw_constraint_pow2
Takashi Iwaidf8db932005-09-07 13:38:19 +02001158 * @runtime: PCM runtime instance
1159 * @cond: condition bits
1160 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001162int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unsigned int cond,
1164 snd_pcm_hw_param_t var)
1165{
1166 return snd_pcm_hw_rule_add(runtime, cond, var,
1167 snd_pcm_hw_rule_pow2, NULL,
1168 var, -1);
1169}
1170
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001171EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1172
Takashi Iwai877211f2005-11-17 13:59:38 +01001173static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001174 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 if (hw_is_mask(var)) {
1177 snd_mask_any(hw_param_mask(params, var));
1178 params->cmask |= 1 << var;
1179 params->rmask |= 1 << var;
1180 return;
1181 }
1182 if (hw_is_interval(var)) {
1183 snd_interval_any(hw_param_interval(params, var));
1184 params->cmask |= 1 << var;
1185 params->rmask |= 1 << var;
1186 return;
1187 }
1188 snd_BUG();
1189}
1190
Takashi Iwai877211f2005-11-17 13:59:38 +01001191void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 unsigned int k;
1194 memset(params, 0, sizeof(*params));
1195 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1196 _snd_pcm_hw_param_any(params, k);
1197 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1198 _snd_pcm_hw_param_any(params, k);
1199 params->info = ~0U;
1200}
1201
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001202EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204/**
1205 * snd_pcm_hw_param_value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001206 * @params: the hw_params instance
1207 * @var: parameter to retrieve
1208 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *
1210 * Return the value for field PAR if it's fixed in configuration space
1211 * defined by PARAMS. Return -EINVAL otherwise
1212 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001213int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1214 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001217 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (!snd_mask_single(mask))
1219 return -EINVAL;
1220 if (dir)
1221 *dir = 0;
1222 return snd_mask_value(mask);
1223 }
1224 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001225 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!snd_interval_single(i))
1227 return -EINVAL;
1228 if (dir)
1229 *dir = i->openmin;
1230 return snd_interval_value(i);
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EINVAL;
1233}
1234
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001235EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Takashi Iwai877211f2005-11-17 13:59:38 +01001237void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001253EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Takashi Iwai877211f2005-11-17 13:59:38 +01001255static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001256 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
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));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001263 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (changed) {
1266 params->cmask |= 1 << var;
1267 params->rmask |= 1 << var;
1268 }
1269 return changed;
1270}
1271
1272
1273/**
1274 * snd_pcm_hw_param_first
Takashi Iwaidf8db932005-09-07 13:38:19 +02001275 * @pcm: PCM instance
1276 * @params: the hw_params instance
1277 * @var: parameter to retrieve
1278 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 *
1280 * Inside configuration space defined by PARAMS remove from PAR all
1281 * values > minimum. Reduce configuration space accordingly.
1282 * Return the minimum.
1283 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001284int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1285 struct snd_pcm_hw_params *params,
1286 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 int changed = _snd_pcm_hw_param_first(params, var);
1289 if (changed < 0)
1290 return changed;
1291 if (params->rmask) {
1292 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001293 if (snd_BUG_ON(err < 0))
1294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 return snd_pcm_hw_param_value(params, var, dir);
1297}
1298
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001299EXPORT_SYMBOL(snd_pcm_hw_param_first);
1300
Takashi Iwai877211f2005-11-17 13:59:38 +01001301static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001302 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 int changed;
1305 if (hw_is_mask(var))
1306 changed = snd_mask_refine_last(hw_param_mask(params, var));
1307 else if (hw_is_interval(var))
1308 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001309 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (changed) {
1312 params->cmask |= 1 << var;
1313 params->rmask |= 1 << var;
1314 }
1315 return changed;
1316}
1317
1318
1319/**
1320 * snd_pcm_hw_param_last
Takashi Iwaidf8db932005-09-07 13:38:19 +02001321 * @pcm: PCM instance
1322 * @params: the hw_params instance
1323 * @var: parameter to retrieve
1324 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 *
1326 * Inside configuration space defined by PARAMS remove from PAR all
1327 * values < maximum. Reduce configuration space accordingly.
1328 * Return the maximum.
1329 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001330int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1331 struct snd_pcm_hw_params *params,
1332 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 int changed = _snd_pcm_hw_param_last(params, var);
1335 if (changed < 0)
1336 return changed;
1337 if (params->rmask) {
1338 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001339 if (snd_BUG_ON(err < 0))
1340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 return snd_pcm_hw_param_value(params, var, dir);
1343}
1344
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001345EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347/**
1348 * snd_pcm_hw_param_choose
Takashi Iwaidf8db932005-09-07 13:38:19 +02001349 * @pcm: PCM instance
1350 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
1352 * Choose one configuration from configuration space defined by PARAMS
1353 * The configuration chosen is that obtained fixing in this order:
1354 * first access, first format, first subformat, min channels,
1355 * min rate, min period time, max buffer size, min tick time
1356 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001357int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1358 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001360 static int vars[] = {
1361 SNDRV_PCM_HW_PARAM_ACCESS,
1362 SNDRV_PCM_HW_PARAM_FORMAT,
1363 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1364 SNDRV_PCM_HW_PARAM_CHANNELS,
1365 SNDRV_PCM_HW_PARAM_RATE,
1366 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1367 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1368 SNDRV_PCM_HW_PARAM_TICK_TIME,
1369 -1
1370 };
1371 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001373 for (v = vars; *v != -1; v++) {
1374 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1375 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1376 else
1377 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001378 if (snd_BUG_ON(err < 0))
1379 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382}
1383
Takashi Iwai877211f2005-11-17 13:59:38 +01001384static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 void *arg)
1386{
Takashi Iwai877211f2005-11-17 13:59:38 +01001387 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned long flags;
1389 snd_pcm_stream_lock_irqsave(substream, flags);
1390 if (snd_pcm_running(substream) &&
1391 snd_pcm_update_hw_ptr(substream) >= 0)
1392 runtime->status->hw_ptr %= runtime->buffer_size;
1393 else
1394 runtime->status->hw_ptr = 0;
1395 snd_pcm_stream_unlock_irqrestore(substream, flags);
1396 return 0;
1397}
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 void *arg)
1401{
Takashi Iwai877211f2005-11-17 13:59:38 +01001402 struct snd_pcm_channel_info *info = arg;
1403 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int width;
1405 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1406 info->offset = -1;
1407 return 0;
1408 }
1409 width = snd_pcm_format_physical_width(runtime->format);
1410 if (width < 0)
1411 return width;
1412 info->offset = 0;
1413 switch (runtime->access) {
1414 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1415 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1416 info->first = info->channel * width;
1417 info->step = runtime->channels * width;
1418 break;
1419 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1420 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1421 {
1422 size_t size = runtime->dma_bytes / runtime->channels;
1423 info->first = info->channel * size * 8;
1424 info->step = width;
1425 break;
1426 }
1427 default:
1428 snd_BUG();
1429 break;
1430 }
1431 return 0;
1432}
1433
1434/**
1435 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1436 * @substream: the pcm substream instance
1437 * @cmd: ioctl command
1438 * @arg: ioctl argument
1439 *
1440 * Processes the generic ioctl commands for PCM.
1441 * Can be passed as the ioctl callback for PCM ops.
1442 *
1443 * Returns zero if successful, or a negative error code on failure.
1444 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001445int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 unsigned int cmd, void *arg)
1447{
1448 switch (cmd) {
1449 case SNDRV_PCM_IOCTL1_INFO:
1450 return 0;
1451 case SNDRV_PCM_IOCTL1_RESET:
1452 return snd_pcm_lib_ioctl_reset(substream, arg);
1453 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1454 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1455 }
1456 return -ENXIO;
1457}
1458
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001459EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/**
1462 * snd_pcm_period_elapsed - update the pcm status for the next period
1463 * @substream: the pcm substream instance
1464 *
1465 * This function is called from the interrupt handler when the
1466 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001467 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
1469 * Even if more than one periods have elapsed since the last call, you
1470 * have to call this only once.
1471 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001472void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwai877211f2005-11-17 13:59:38 +01001474 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unsigned long flags;
1476
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001477 if (PCM_RUNTIME_CHECK(substream))
1478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (runtime->transfer_ack_begin)
1482 runtime->transfer_ack_begin(substream);
1483
1484 snd_pcm_stream_lock_irqsave(substream, flags);
1485 if (!snd_pcm_running(substream) ||
1486 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1487 goto _end;
1488
1489 if (substream->timer_running)
1490 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 _end:
1492 snd_pcm_stream_unlock_irqrestore(substream, flags);
1493 if (runtime->transfer_ack_end)
1494 runtime->transfer_ack_end(substream);
1495 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1496}
1497
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001498EXPORT_SYMBOL(snd_pcm_period_elapsed);
1499
Takashi Iwai13075512008-01-08 18:08:14 +01001500/*
1501 * Wait until avail_min data becomes available
1502 * Returns a negative error code if any error occurs during operation.
1503 * The available space is stored on availp. When err = 0 and avail = 0
1504 * on the capture stream, it indicates the stream is in DRAINING state.
1505 */
1506static int wait_for_avail_min(struct snd_pcm_substream *substream,
1507 snd_pcm_uframes_t *availp)
1508{
1509 struct snd_pcm_runtime *runtime = substream->runtime;
1510 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1511 wait_queue_t wait;
1512 int err = 0;
1513 snd_pcm_uframes_t avail = 0;
1514 long tout;
1515
1516 init_waitqueue_entry(&wait, current);
1517 add_wait_queue(&runtime->sleep, &wait);
1518 for (;;) {
1519 if (signal_pending(current)) {
1520 err = -ERESTARTSYS;
1521 break;
1522 }
1523 set_current_state(TASK_INTERRUPTIBLE);
1524 snd_pcm_stream_unlock_irq(substream);
1525 tout = schedule_timeout(msecs_to_jiffies(10000));
1526 snd_pcm_stream_lock_irq(substream);
1527 switch (runtime->status->state) {
1528 case SNDRV_PCM_STATE_SUSPENDED:
1529 err = -ESTRPIPE;
1530 goto _endloop;
1531 case SNDRV_PCM_STATE_XRUN:
1532 err = -EPIPE;
1533 goto _endloop;
1534 case SNDRV_PCM_STATE_DRAINING:
1535 if (is_playback)
1536 err = -EPIPE;
1537 else
1538 avail = 0; /* indicate draining */
1539 goto _endloop;
1540 case SNDRV_PCM_STATE_OPEN:
1541 case SNDRV_PCM_STATE_SETUP:
1542 case SNDRV_PCM_STATE_DISCONNECTED:
1543 err = -EBADFD;
1544 goto _endloop;
1545 }
1546 if (!tout) {
1547 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1548 is_playback ? "playback" : "capture");
1549 err = -EIO;
1550 break;
1551 }
1552 if (is_playback)
1553 avail = snd_pcm_playback_avail(runtime);
1554 else
1555 avail = snd_pcm_capture_avail(runtime);
1556 if (avail >= runtime->control->avail_min)
1557 break;
1558 }
1559 _endloop:
1560 remove_wait_queue(&runtime->sleep, &wait);
1561 *availp = avail;
1562 return err;
1563}
1564
Takashi Iwai877211f2005-11-17 13:59:38 +01001565static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 unsigned int hwoff,
1567 unsigned long data, unsigned int off,
1568 snd_pcm_uframes_t frames)
1569{
Takashi Iwai877211f2005-11-17 13:59:38 +01001570 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int err;
1572 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1573 if (substream->ops->copy) {
1574 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1575 return err;
1576 } else {
1577 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1579 return -EFAULT;
1580 }
1581 return 0;
1582}
1583
Takashi Iwai877211f2005-11-17 13:59:38 +01001584typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 unsigned long data, unsigned int off,
1586 snd_pcm_uframes_t size);
1587
Takashi Iwai877211f2005-11-17 13:59:38 +01001588static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 unsigned long data,
1590 snd_pcm_uframes_t size,
1591 int nonblock,
1592 transfer_f transfer)
1593{
Takashi Iwai877211f2005-11-17 13:59:38 +01001594 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 snd_pcm_uframes_t xfer = 0;
1596 snd_pcm_uframes_t offset = 0;
1597 int err = 0;
1598
1599 if (size == 0)
1600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 snd_pcm_stream_lock_irq(substream);
1603 switch (runtime->status->state) {
1604 case SNDRV_PCM_STATE_PREPARED:
1605 case SNDRV_PCM_STATE_RUNNING:
1606 case SNDRV_PCM_STATE_PAUSED:
1607 break;
1608 case SNDRV_PCM_STATE_XRUN:
1609 err = -EPIPE;
1610 goto _end_unlock;
1611 case SNDRV_PCM_STATE_SUSPENDED:
1612 err = -ESTRPIPE;
1613 goto _end_unlock;
1614 default:
1615 err = -EBADFD;
1616 goto _end_unlock;
1617 }
1618
1619 while (size > 0) {
1620 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1621 snd_pcm_uframes_t avail;
1622 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001623 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 snd_pcm_update_hw_ptr(substream);
1625 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001626 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (nonblock) {
1628 err = -EAGAIN;
1629 goto _end_unlock;
1630 }
Takashi Iwai13075512008-01-08 18:08:14 +01001631 err = wait_for_avail_min(substream, &avail);
1632 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 frames = size > avail ? avail : size;
1636 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1637 if (frames > cont)
1638 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001639 if (snd_BUG_ON(!frames)) {
1640 snd_pcm_stream_unlock_irq(substream);
1641 return -EINVAL;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 appl_ptr = runtime->control->appl_ptr;
1644 appl_ofs = appl_ptr % runtime->buffer_size;
1645 snd_pcm_stream_unlock_irq(substream);
1646 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1647 goto _end;
1648 snd_pcm_stream_lock_irq(substream);
1649 switch (runtime->status->state) {
1650 case SNDRV_PCM_STATE_XRUN:
1651 err = -EPIPE;
1652 goto _end_unlock;
1653 case SNDRV_PCM_STATE_SUSPENDED:
1654 err = -ESTRPIPE;
1655 goto _end_unlock;
1656 default:
1657 break;
1658 }
1659 appl_ptr += frames;
1660 if (appl_ptr >= runtime->boundary)
1661 appl_ptr -= runtime->boundary;
1662 runtime->control->appl_ptr = appl_ptr;
1663 if (substream->ops->ack)
1664 substream->ops->ack(substream);
1665
1666 offset += frames;
1667 size -= frames;
1668 xfer += frames;
1669 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1670 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1671 err = snd_pcm_start(substream);
1672 if (err < 0)
1673 goto _end_unlock;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 _end_unlock:
1677 snd_pcm_stream_unlock_irq(substream);
1678 _end:
1679 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1680}
1681
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001682/* sanity-check for read/write methods */
1683static int pcm_sanity_check(struct snd_pcm_substream *substream)
1684{
1685 struct snd_pcm_runtime *runtime;
1686 if (PCM_RUNTIME_CHECK(substream))
1687 return -ENXIO;
1688 runtime = substream->runtime;
1689 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1690 return -EINVAL;
1691 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1692 return -EBADFD;
1693 return 0;
1694}
1695
Takashi Iwai877211f2005-11-17 13:59:38 +01001696snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwai877211f2005-11-17 13:59:38 +01001698 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001700 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001702 err = pcm_sanity_check(substream);
1703 if (err < 0)
1704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001706 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1709 runtime->channels > 1)
1710 return -EINVAL;
1711 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1712 snd_pcm_lib_write_transfer);
1713}
1714
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001715EXPORT_SYMBOL(snd_pcm_lib_write);
1716
Takashi Iwai877211f2005-11-17 13:59:38 +01001717static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 unsigned int hwoff,
1719 unsigned long data, unsigned int off,
1720 snd_pcm_uframes_t frames)
1721{
Takashi Iwai877211f2005-11-17 13:59:38 +01001722 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int err;
1724 void __user **bufs = (void __user **)data;
1725 int channels = runtime->channels;
1726 int c;
1727 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001728 if (snd_BUG_ON(!substream->ops->silence))
1729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 for (c = 0; c < channels; ++c, ++bufs) {
1731 if (*bufs == NULL) {
1732 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1733 return err;
1734 } else {
1735 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1736 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1737 return err;
1738 }
1739 }
1740 } else {
1741 /* default transfer behaviour */
1742 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 for (c = 0; c < channels; ++c, ++bufs) {
1744 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1745 if (*bufs == NULL) {
1746 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1747 } else {
1748 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1749 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1750 return -EFAULT;
1751 }
1752 }
1753 }
1754 return 0;
1755}
1756
Takashi Iwai877211f2005-11-17 13:59:38 +01001757snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 void __user **bufs,
1759 snd_pcm_uframes_t frames)
1760{
Takashi Iwai877211f2005-11-17 13:59:38 +01001761 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001763 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001765 err = pcm_sanity_check(substream);
1766 if (err < 0)
1767 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001769 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1772 return -EINVAL;
1773 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1774 nonblock, snd_pcm_lib_writev_transfer);
1775}
1776
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001777EXPORT_SYMBOL(snd_pcm_lib_writev);
1778
Takashi Iwai877211f2005-11-17 13:59:38 +01001779static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 unsigned int hwoff,
1781 unsigned long data, unsigned int off,
1782 snd_pcm_uframes_t frames)
1783{
Takashi Iwai877211f2005-11-17 13:59:38 +01001784 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int err;
1786 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1787 if (substream->ops->copy) {
1788 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1789 return err;
1790 } else {
1791 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1793 return -EFAULT;
1794 }
1795 return 0;
1796}
1797
Takashi Iwai877211f2005-11-17 13:59:38 +01001798static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 unsigned long data,
1800 snd_pcm_uframes_t size,
1801 int nonblock,
1802 transfer_f transfer)
1803{
Takashi Iwai877211f2005-11-17 13:59:38 +01001804 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 snd_pcm_uframes_t xfer = 0;
1806 snd_pcm_uframes_t offset = 0;
1807 int err = 0;
1808
1809 if (size == 0)
1810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 snd_pcm_stream_lock_irq(substream);
1813 switch (runtime->status->state) {
1814 case SNDRV_PCM_STATE_PREPARED:
1815 if (size >= runtime->start_threshold) {
1816 err = snd_pcm_start(substream);
1817 if (err < 0)
1818 goto _end_unlock;
1819 }
1820 break;
1821 case SNDRV_PCM_STATE_DRAINING:
1822 case SNDRV_PCM_STATE_RUNNING:
1823 case SNDRV_PCM_STATE_PAUSED:
1824 break;
1825 case SNDRV_PCM_STATE_XRUN:
1826 err = -EPIPE;
1827 goto _end_unlock;
1828 case SNDRV_PCM_STATE_SUSPENDED:
1829 err = -ESTRPIPE;
1830 goto _end_unlock;
1831 default:
1832 err = -EBADFD;
1833 goto _end_unlock;
1834 }
1835
1836 while (size > 0) {
1837 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1838 snd_pcm_uframes_t avail;
1839 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001840 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001843 if (!avail) {
1844 if (runtime->status->state ==
1845 SNDRV_PCM_STATE_DRAINING) {
1846 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto _end_unlock;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (nonblock) {
1850 err = -EAGAIN;
1851 goto _end_unlock;
1852 }
Takashi Iwai13075512008-01-08 18:08:14 +01001853 err = wait_for_avail_min(substream, &avail);
1854 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001856 if (!avail)
1857 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 frames = size > avail ? avail : size;
1860 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1861 if (frames > cont)
1862 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001863 if (snd_BUG_ON(!frames)) {
1864 snd_pcm_stream_unlock_irq(substream);
1865 return -EINVAL;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 appl_ptr = runtime->control->appl_ptr;
1868 appl_ofs = appl_ptr % runtime->buffer_size;
1869 snd_pcm_stream_unlock_irq(substream);
1870 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1871 goto _end;
1872 snd_pcm_stream_lock_irq(substream);
1873 switch (runtime->status->state) {
1874 case SNDRV_PCM_STATE_XRUN:
1875 err = -EPIPE;
1876 goto _end_unlock;
1877 case SNDRV_PCM_STATE_SUSPENDED:
1878 err = -ESTRPIPE;
1879 goto _end_unlock;
1880 default:
1881 break;
1882 }
1883 appl_ptr += frames;
1884 if (appl_ptr >= runtime->boundary)
1885 appl_ptr -= runtime->boundary;
1886 runtime->control->appl_ptr = appl_ptr;
1887 if (substream->ops->ack)
1888 substream->ops->ack(substream);
1889
1890 offset += frames;
1891 size -= frames;
1892 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 _end_unlock:
1895 snd_pcm_stream_unlock_irq(substream);
1896 _end:
1897 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1898}
1899
Takashi Iwai877211f2005-11-17 13:59:38 +01001900snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwai877211f2005-11-17 13:59:38 +01001902 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001904 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001906 err = pcm_sanity_check(substream);
1907 if (err < 0)
1908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001910 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1912 return -EINVAL;
1913 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1914}
1915
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001916EXPORT_SYMBOL(snd_pcm_lib_read);
1917
Takashi Iwai877211f2005-11-17 13:59:38 +01001918static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 unsigned int hwoff,
1920 unsigned long data, unsigned int off,
1921 snd_pcm_uframes_t frames)
1922{
Takashi Iwai877211f2005-11-17 13:59:38 +01001923 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int err;
1925 void __user **bufs = (void __user **)data;
1926 int channels = runtime->channels;
1927 int c;
1928 if (substream->ops->copy) {
1929 for (c = 0; c < channels; ++c, ++bufs) {
1930 char __user *buf;
1931 if (*bufs == NULL)
1932 continue;
1933 buf = *bufs + samples_to_bytes(runtime, off);
1934 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1935 return err;
1936 }
1937 } else {
1938 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 for (c = 0; c < channels; ++c, ++bufs) {
1940 char *hwbuf;
1941 char __user *buf;
1942 if (*bufs == NULL)
1943 continue;
1944
1945 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1946 buf = *bufs + samples_to_bytes(runtime, off);
1947 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1948 return -EFAULT;
1949 }
1950 }
1951 return 0;
1952}
1953
Takashi Iwai877211f2005-11-17 13:59:38 +01001954snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 void __user **bufs,
1956 snd_pcm_uframes_t frames)
1957{
Takashi Iwai877211f2005-11-17 13:59:38 +01001958 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001960 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001962 err = pcm_sanity_check(substream);
1963 if (err < 0)
1964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1967 return -EBADFD;
1968
Takashi Iwai0df63e42006-04-28 15:13:41 +02001969 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1971 return -EINVAL;
1972 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
1973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975EXPORT_SYMBOL(snd_pcm_lib_readv);