blob: b406630d8fdff0f107aac2aa8c6c1f90703be9da [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
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 */
Takashi Iwai877211f2005-11-17 13:59:38 +010042void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai877211f2005-11-17 13:59:38 +010044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 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 }
Takashi Iwai235475c2005-12-07 15:28:07 +010059 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020082 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020084 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87 frames = runtime->buffer_size - runtime->silence_filled;
88 }
89 snd_assert(frames <= runtime->buffer_size, return);
90 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);
100 snd_assert(err >= 0, );
101 } else {
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
104 }
105 } else {
106 unsigned int c;
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
110 int err;
111 err = substream->ops->silence(substream, c, ofs, transfer);
112 snd_assert(err >= 0, );
113 }
114 } else {
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
119 }
120 }
121 }
122 runtime->silence_filled += transfer;
123 frames -= transfer;
124 ofs = 0;
125 }
126}
127
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
148 pos = substream->ops->pointer(substream);
149 if (pos == SNDRV_PCM_POS_XRUN)
150 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#ifdef CONFIG_SND_DEBUG
152 if (pos >= runtime->buffer_size) {
153 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 +0200154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 pos -= pos % runtime->min_align;
157 return pos;
158}
159
Takashi Iwai877211f2005-11-17 13:59:38 +0100160static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
161 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 snd_pcm_uframes_t avail;
164
165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
166 avail = snd_pcm_playback_avail(runtime);
167 else
168 avail = snd_pcm_capture_avail(runtime);
169 if (avail > runtime->avail_max)
170 runtime->avail_max = avail;
171 if (avail >= runtime->stop_threshold) {
172 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
173 snd_pcm_drain_done(substream);
174 else
175 xrun(substream);
176 return -EPIPE;
177 }
178 if (avail >= runtime->control->avail_min)
179 wake_up(&runtime->sleep);
180 return 0;
181}
182
Takashi Iwai877211f2005-11-17 13:59:38 +0100183static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Takashi Iwai877211f2005-11-17 13:59:38 +0100185 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 snd_pcm_uframes_t pos;
187 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
188 snd_pcm_sframes_t delta;
189
Takashi Iwai7c7fc2d2007-11-23 13:14:23 +0100190 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_MMAP)
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100191 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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) {
358 snd_assert(n > 0, );
359 *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 Iwaie88e8ae62006-04-28 15:13:40 +0200384 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (i->min < v->min) {
386 i->min = v->min;
387 i->openmin = v->openmin;
388 changed = 1;
389 } else if (i->min == v->min && !i->openmin && v->openmin) {
390 i->openmin = 1;
391 changed = 1;
392 }
393 if (i->max > v->max) {
394 i->max = v->max;
395 i->openmax = v->openmax;
396 changed = 1;
397 } else if (i->max == v->max && !i->openmax && v->openmax) {
398 i->openmax = 1;
399 changed = 1;
400 }
401 if (!i->integer && v->integer) {
402 i->integer = 1;
403 changed = 1;
404 }
405 if (i->integer) {
406 if (i->openmin) {
407 i->min++;
408 i->openmin = 0;
409 }
410 if (i->openmax) {
411 i->max--;
412 i->openmax = 0;
413 }
414 } else if (!i->openmin && !i->openmax && i->min == i->max)
415 i->integer = 1;
416 if (snd_interval_checkempty(i)) {
417 snd_interval_none(i);
418 return -EINVAL;
419 }
420 return changed;
421}
422
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200423EXPORT_SYMBOL(snd_interval_refine);
424
Takashi Iwai877211f2005-11-17 13:59:38 +0100425static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200427 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (snd_interval_single(i))
429 return 0;
430 i->max = i->min;
431 i->openmax = i->openmin;
432 if (i->openmax)
433 i->max++;
434 return 1;
435}
436
Takashi Iwai877211f2005-11-17 13:59:38 +0100437static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200439 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (snd_interval_single(i))
441 return 0;
442 i->min = i->max;
443 i->openmin = i->openmax;
444 if (i->openmin)
445 i->min--;
446 return 1;
447}
448
Takashi Iwai877211f2005-11-17 13:59:38 +0100449void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 if (a->empty || b->empty) {
452 snd_interval_none(c);
453 return;
454 }
455 c->empty = 0;
456 c->min = mul(a->min, b->min);
457 c->openmin = (a->openmin || b->openmin);
458 c->max = mul(a->max, b->max);
459 c->openmax = (a->openmax || b->openmax);
460 c->integer = (a->integer && b->integer);
461}
462
463/**
464 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200465 * @a: dividend
466 * @b: divisor
467 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
469 * c = a / b
470 *
471 * Returns non-zero if the value is changed, zero if not changed.
472 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100473void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 unsigned int r;
476 if (a->empty || b->empty) {
477 snd_interval_none(c);
478 return;
479 }
480 c->empty = 0;
481 c->min = div32(a->min, b->max, &r);
482 c->openmin = (r || a->openmin || b->openmax);
483 if (b->min > 0) {
484 c->max = div32(a->max, b->min, &r);
485 if (r) {
486 c->max++;
487 c->openmax = 1;
488 } else
489 c->openmax = (a->openmax || b->openmin);
490 } else {
491 c->max = UINT_MAX;
492 c->openmax = 0;
493 }
494 c->integer = 0;
495}
496
497/**
498 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200499 * @a: dividend 1
500 * @b: dividend 2
501 * @k: divisor (as integer)
502 * @c: result
503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * c = a * b / k
505 *
506 * Returns non-zero if the value is changed, zero if not changed.
507 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100508void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
509 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 unsigned int r;
512 if (a->empty || b->empty) {
513 snd_interval_none(c);
514 return;
515 }
516 c->empty = 0;
517 c->min = muldiv32(a->min, b->min, k, &r);
518 c->openmin = (r || a->openmin || b->openmin);
519 c->max = muldiv32(a->max, b->max, k, &r);
520 if (r) {
521 c->max++;
522 c->openmax = 1;
523 } else
524 c->openmax = (a->openmax || b->openmax);
525 c->integer = 0;
526}
527
528/**
529 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200530 * @a: dividend 1
531 * @k: dividend 2 (as integer)
532 * @b: divisor
533 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
535 * c = a * k / b
536 *
537 * Returns non-zero if the value is changed, zero if not changed.
538 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100539void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
540 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned int r;
543 if (a->empty || b->empty) {
544 snd_interval_none(c);
545 return;
546 }
547 c->empty = 0;
548 c->min = muldiv32(a->min, k, b->max, &r);
549 c->openmin = (r || a->openmin || b->openmax);
550 if (b->min > 0) {
551 c->max = muldiv32(a->max, k, b->min, &r);
552 if (r) {
553 c->max++;
554 c->openmax = 1;
555 } else
556 c->openmax = (a->openmax || b->openmin);
557 } else {
558 c->max = UINT_MAX;
559 c->openmax = 0;
560 }
561 c->integer = 0;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* ---- */
565
566
567/**
568 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200569 * @i: interval to refine
570 * @rats_count: number of ratnum_t
571 * @rats: ratnum_t array
572 * @nump: pointer to store the resultant numerator
573 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
575 * Returns non-zero if the value is changed, zero if not changed.
576 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100577int snd_interval_ratnum(struct snd_interval *i,
578 unsigned int rats_count, struct snd_ratnum *rats,
579 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 unsigned int best_num, best_diff, best_den;
582 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100583 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int err;
585
586 best_num = best_den = best_diff = 0;
587 for (k = 0; k < rats_count; ++k) {
588 unsigned int num = rats[k].num;
589 unsigned int den;
590 unsigned int q = i->min;
591 int diff;
592 if (q == 0)
593 q = 1;
594 den = div_down(num, q);
595 if (den < rats[k].den_min)
596 continue;
597 if (den > rats[k].den_max)
598 den = rats[k].den_max;
599 else {
600 unsigned int r;
601 r = (den - rats[k].den_min) % rats[k].den_step;
602 if (r != 0)
603 den -= r;
604 }
605 diff = num - q * den;
606 if (best_num == 0 ||
607 diff * best_den < best_diff * den) {
608 best_diff = diff;
609 best_den = den;
610 best_num = num;
611 }
612 }
613 if (best_den == 0) {
614 i->empty = 1;
615 return -EINVAL;
616 }
617 t.min = div_down(best_num, best_den);
618 t.openmin = !!(best_num % best_den);
619
620 best_num = best_den = best_diff = 0;
621 for (k = 0; k < rats_count; ++k) {
622 unsigned int num = rats[k].num;
623 unsigned int den;
624 unsigned int q = i->max;
625 int diff;
626 if (q == 0) {
627 i->empty = 1;
628 return -EINVAL;
629 }
630 den = div_up(num, q);
631 if (den > rats[k].den_max)
632 continue;
633 if (den < rats[k].den_min)
634 den = rats[k].den_min;
635 else {
636 unsigned int r;
637 r = (den - rats[k].den_min) % rats[k].den_step;
638 if (r != 0)
639 den += rats[k].den_step - r;
640 }
641 diff = q * den - num;
642 if (best_num == 0 ||
643 diff * best_den < best_diff * den) {
644 best_diff = diff;
645 best_den = den;
646 best_num = num;
647 }
648 }
649 if (best_den == 0) {
650 i->empty = 1;
651 return -EINVAL;
652 }
653 t.max = div_up(best_num, best_den);
654 t.openmax = !!(best_num % best_den);
655 t.integer = 0;
656 err = snd_interval_refine(i, &t);
657 if (err < 0)
658 return err;
659
660 if (snd_interval_single(i)) {
661 if (nump)
662 *nump = best_num;
663 if (denp)
664 *denp = best_den;
665 }
666 return err;
667}
668
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200669EXPORT_SYMBOL(snd_interval_ratnum);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/**
672 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200673 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100674 * @rats_count: number of struct ratden
675 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200676 * @nump: pointer to store the resultant numerator
677 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
679 * Returns non-zero if the value is changed, zero if not changed.
680 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100681static int snd_interval_ratden(struct snd_interval *i,
682 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned int *nump, unsigned int *denp)
684{
685 unsigned int best_num, best_diff, best_den;
686 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100687 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int err;
689
690 best_num = best_den = best_diff = 0;
691 for (k = 0; k < rats_count; ++k) {
692 unsigned int num;
693 unsigned int den = rats[k].den;
694 unsigned int q = i->min;
695 int diff;
696 num = mul(q, den);
697 if (num > rats[k].num_max)
698 continue;
699 if (num < rats[k].num_min)
700 num = rats[k].num_max;
701 else {
702 unsigned int r;
703 r = (num - rats[k].num_min) % rats[k].num_step;
704 if (r != 0)
705 num += rats[k].num_step - r;
706 }
707 diff = num - q * den;
708 if (best_num == 0 ||
709 diff * best_den < best_diff * den) {
710 best_diff = diff;
711 best_den = den;
712 best_num = num;
713 }
714 }
715 if (best_den == 0) {
716 i->empty = 1;
717 return -EINVAL;
718 }
719 t.min = div_down(best_num, best_den);
720 t.openmin = !!(best_num % best_den);
721
722 best_num = best_den = best_diff = 0;
723 for (k = 0; k < rats_count; ++k) {
724 unsigned int num;
725 unsigned int den = rats[k].den;
726 unsigned int q = i->max;
727 int diff;
728 num = mul(q, den);
729 if (num < rats[k].num_min)
730 continue;
731 if (num > rats[k].num_max)
732 num = rats[k].num_max;
733 else {
734 unsigned int r;
735 r = (num - rats[k].num_min) % rats[k].num_step;
736 if (r != 0)
737 num -= r;
738 }
739 diff = q * den - num;
740 if (best_num == 0 ||
741 diff * best_den < best_diff * den) {
742 best_diff = diff;
743 best_den = den;
744 best_num = num;
745 }
746 }
747 if (best_den == 0) {
748 i->empty = 1;
749 return -EINVAL;
750 }
751 t.max = div_up(best_num, best_den);
752 t.openmax = !!(best_num % best_den);
753 t.integer = 0;
754 err = snd_interval_refine(i, &t);
755 if (err < 0)
756 return err;
757
758 if (snd_interval_single(i)) {
759 if (nump)
760 *nump = best_num;
761 if (denp)
762 *denp = best_den;
763 }
764 return err;
765}
766
767/**
768 * snd_interval_list - refine the interval value from the list
769 * @i: the interval value to refine
770 * @count: the number of elements in the list
771 * @list: the value list
772 * @mask: the bit-mask to evaluate
773 *
774 * Refines the interval value from the list.
775 * When mask is non-zero, only the elements corresponding to bit 1 are
776 * evaluated.
777 *
778 * Returns non-zero if the value is changed, zero if not changed.
779 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100780int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 unsigned int k;
783 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100784
785 if (!count) {
786 i->empty = 1;
787 return -EINVAL;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 for (k = 0; k < count; k++) {
790 if (mask && !(mask & (1 << k)))
791 continue;
792 if (i->min == list[k] && !i->openmin)
793 goto _l1;
794 if (i->min < list[k]) {
795 i->min = list[k];
796 i->openmin = 0;
797 changed = 1;
798 goto _l1;
799 }
800 }
801 i->empty = 1;
802 return -EINVAL;
803 _l1:
804 for (k = count; k-- > 0;) {
805 if (mask && !(mask & (1 << k)))
806 continue;
807 if (i->max == list[k] && !i->openmax)
808 goto _l2;
809 if (i->max > list[k]) {
810 i->max = list[k];
811 i->openmax = 0;
812 changed = 1;
813 goto _l2;
814 }
815 }
816 i->empty = 1;
817 return -EINVAL;
818 _l2:
819 if (snd_interval_checkempty(i)) {
820 i->empty = 1;
821 return -EINVAL;
822 }
823 return changed;
824}
825
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200826EXPORT_SYMBOL(snd_interval_list);
827
Takashi Iwai877211f2005-11-17 13:59:38 +0100828static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 unsigned int n;
831 int changed = 0;
832 n = (i->min - min) % step;
833 if (n != 0 || i->openmin) {
834 i->min += step - n;
835 changed = 1;
836 }
837 n = (i->max - min) % step;
838 if (n != 0 || i->openmax) {
839 i->max -= n;
840 changed = 1;
841 }
842 if (snd_interval_checkempty(i)) {
843 i->empty = 1;
844 return -EINVAL;
845 }
846 return changed;
847}
848
849/* Info constraints helpers */
850
851/**
852 * snd_pcm_hw_rule_add - add the hw-constraint rule
853 * @runtime: the pcm runtime instance
854 * @cond: condition bits
855 * @var: the variable to evaluate
856 * @func: the evaluation function
857 * @private: the private data pointer passed to function
858 * @dep: the dependent variables
859 *
860 * Returns zero if successful, or a negative error code on failure.
861 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100862int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int var,
864 snd_pcm_hw_rule_func_t func, void *private,
865 int dep, ...)
866{
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
868 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int k;
870 va_list args;
871 va_start(args, dep);
872 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100873 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned int new_rules = constrs->rules_all + 16;
875 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
876 if (!new)
877 return -ENOMEM;
878 if (constrs->rules) {
879 memcpy(new, constrs->rules,
880 constrs->rules_num * sizeof(*c));
881 kfree(constrs->rules);
882 }
883 constrs->rules = new;
884 constrs->rules_all = new_rules;
885 }
886 c = &constrs->rules[constrs->rules_num];
887 c->cond = cond;
888 c->func = func;
889 c->var = var;
890 c->private = private;
891 k = 0;
892 while (1) {
893 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
894 c->deps[k++] = dep;
895 if (dep < 0)
896 break;
897 dep = va_arg(args, int);
898 }
899 constrs->rules_num++;
900 va_end(args);
901 return 0;
902}
903
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200904EXPORT_SYMBOL(snd_pcm_hw_rule_add);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/**
907 * snd_pcm_hw_constraint_mask
Takashi Iwaidf8db932005-09-07 13:38:19 +0200908 * @runtime: PCM runtime instance
909 * @var: hw_params variable to apply the mask
910 * @mask: the bitmap mask
911 *
912 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100914int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 u_int32_t mask)
916{
Takashi Iwai877211f2005-11-17 13:59:38 +0100917 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
918 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *maskp->bits &= mask;
920 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
921 if (*maskp->bits == 0)
922 return -EINVAL;
923 return 0;
924}
925
926/**
927 * snd_pcm_hw_constraint_mask64
Takashi Iwaidf8db932005-09-07 13:38:19 +0200928 * @runtime: PCM runtime instance
929 * @var: hw_params variable to apply the mask
930 * @mask: the 64bit bitmap mask
931 *
932 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100934int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 u_int64_t mask)
936{
Takashi Iwai877211f2005-11-17 13:59:38 +0100937 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
938 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 maskp->bits[0] &= (u_int32_t)mask;
940 maskp->bits[1] &= (u_int32_t)(mask >> 32);
941 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
942 if (! maskp->bits[0] && ! maskp->bits[1])
943 return -EINVAL;
944 return 0;
945}
946
947/**
948 * snd_pcm_hw_constraint_integer
Takashi Iwaidf8db932005-09-07 13:38:19 +0200949 * @runtime: PCM runtime instance
950 * @var: hw_params variable to apply the integer constraint
951 *
952 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100954int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Takashi Iwai877211f2005-11-17 13:59:38 +0100956 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return snd_interval_setinteger(constrs_interval(constrs, var));
958}
959
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200960EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/**
963 * snd_pcm_hw_constraint_minmax
Takashi Iwaidf8db932005-09-07 13:38:19 +0200964 * @runtime: PCM runtime instance
965 * @var: hw_params variable to apply the range
966 * @min: the minimal value
967 * @max: the maximal value
968 *
969 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100971int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unsigned int min, unsigned int max)
973{
Takashi Iwai877211f2005-11-17 13:59:38 +0100974 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
975 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 t.min = min;
977 t.max = max;
978 t.openmin = t.openmax = 0;
979 t.integer = 0;
980 return snd_interval_refine(constrs_interval(constrs, var), &t);
981}
982
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200983EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
984
Takashi Iwai877211f2005-11-17 13:59:38 +0100985static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
986 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Takashi Iwai877211f2005-11-17 13:59:38 +0100988 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
990}
991
992
993/**
994 * snd_pcm_hw_constraint_list
Takashi Iwaidf8db932005-09-07 13:38:19 +0200995 * @runtime: PCM runtime instance
996 * @cond: condition bits
997 * @var: hw_params variable to apply the list constraint
998 * @l: list
999 *
1000 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001002int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned int cond,
1004 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001005 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 return snd_pcm_hw_rule_add(runtime, cond, var,
1008 snd_pcm_hw_rule_list, l,
1009 var, -1);
1010}
1011
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001012EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1013
Takashi Iwai877211f2005-11-17 13:59:38 +01001014static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1015 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Takashi Iwai877211f2005-11-17 13:59:38 +01001017 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned int num = 0, den = 0;
1019 int err;
1020 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1021 r->nrats, r->rats, &num, &den);
1022 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1023 params->rate_num = num;
1024 params->rate_den = den;
1025 }
1026 return err;
1027}
1028
1029/**
1030 * snd_pcm_hw_constraint_ratnums
Takashi Iwaidf8db932005-09-07 13:38:19 +02001031 * @runtime: PCM runtime instance
1032 * @cond: condition bits
1033 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001034 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001036int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unsigned int cond,
1038 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001039 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 return snd_pcm_hw_rule_add(runtime, cond, var,
1042 snd_pcm_hw_rule_ratnums, r,
1043 var, -1);
1044}
1045
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001046EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1049 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Takashi Iwai877211f2005-11-17 13:59:38 +01001051 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned int num = 0, den = 0;
1053 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1054 r->nrats, r->rats, &num, &den);
1055 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1056 params->rate_num = num;
1057 params->rate_den = den;
1058 }
1059 return err;
1060}
1061
1062/**
1063 * snd_pcm_hw_constraint_ratdens
Takashi Iwaidf8db932005-09-07 13:38:19 +02001064 * @runtime: PCM runtime instance
1065 * @cond: condition bits
1066 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001067 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001069int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 unsigned int cond,
1071 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001072 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 return snd_pcm_hw_rule_add(runtime, cond, var,
1075 snd_pcm_hw_rule_ratdens, r,
1076 var, -1);
1077}
1078
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001079EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1080
Takashi Iwai877211f2005-11-17 13:59:38 +01001081static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1082 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 unsigned int l = (unsigned long) rule->private;
1085 int width = l & 0xffff;
1086 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001087 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (snd_interval_single(i) && snd_interval_value(i) == width)
1089 params->msbits = msbits;
1090 return 0;
1091}
1092
1093/**
1094 * snd_pcm_hw_constraint_msbits
Takashi Iwaidf8db932005-09-07 13:38:19 +02001095 * @runtime: PCM runtime instance
1096 * @cond: condition bits
1097 * @width: sample bits width
1098 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001100int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned int cond,
1102 unsigned int width,
1103 unsigned int msbits)
1104{
1105 unsigned long l = (msbits << 16) | width;
1106 return snd_pcm_hw_rule_add(runtime, cond, -1,
1107 snd_pcm_hw_rule_msbits,
1108 (void*) l,
1109 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1110}
1111
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001112EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1113
Takashi Iwai877211f2005-11-17 13:59:38 +01001114static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1115 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 unsigned long step = (unsigned long) rule->private;
1118 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1119}
1120
1121/**
1122 * snd_pcm_hw_constraint_step
Takashi Iwaidf8db932005-09-07 13:38:19 +02001123 * @runtime: PCM runtime instance
1124 * @cond: condition bits
1125 * @var: hw_params variable to apply the step constraint
1126 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001128int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 unsigned int cond,
1130 snd_pcm_hw_param_t var,
1131 unsigned long step)
1132{
1133 return snd_pcm_hw_rule_add(runtime, cond, var,
1134 snd_pcm_hw_rule_step, (void *) step,
1135 var, -1);
1136}
1137
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001138EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1139
Takashi Iwai877211f2005-11-17 13:59:38 +01001140static 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 -07001141{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001142 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1144 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1145 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1146 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1147 };
1148 return snd_interval_list(hw_param_interval(params, rule->var),
1149 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1150}
1151
1152/**
1153 * snd_pcm_hw_constraint_pow2
Takashi Iwaidf8db932005-09-07 13:38:19 +02001154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001158int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 unsigned int cond,
1160 snd_pcm_hw_param_t var)
1161{
1162 return snd_pcm_hw_rule_add(runtime, cond, var,
1163 snd_pcm_hw_rule_pow2, NULL,
1164 var, -1);
1165}
1166
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001167EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1168
Takashi Iwai877211f2005-11-17 13:59:38 +01001169static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001170 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 if (hw_is_mask(var)) {
1173 snd_mask_any(hw_param_mask(params, var));
1174 params->cmask |= 1 << var;
1175 params->rmask |= 1 << var;
1176 return;
1177 }
1178 if (hw_is_interval(var)) {
1179 snd_interval_any(hw_param_interval(params, var));
1180 params->cmask |= 1 << var;
1181 params->rmask |= 1 << var;
1182 return;
1183 }
1184 snd_BUG();
1185}
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 unsigned int k;
1190 memset(params, 0, sizeof(*params));
1191 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1192 _snd_pcm_hw_param_any(params, k);
1193 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1194 _snd_pcm_hw_param_any(params, k);
1195 params->info = ~0U;
1196}
1197
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001198EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200/**
1201 * snd_pcm_hw_param_value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001202 * @params: the hw_params instance
1203 * @var: parameter to retrieve
1204 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 *
1206 * Return the value for field PAR if it's fixed in configuration space
1207 * defined by PARAMS. Return -EINVAL otherwise
1208 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001209int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1210 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001213 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!snd_mask_single(mask))
1215 return -EINVAL;
1216 if (dir)
1217 *dir = 0;
1218 return snd_mask_value(mask);
1219 }
1220 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001221 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!snd_interval_single(i))
1223 return -EINVAL;
1224 if (dir)
1225 *dir = i->openmin;
1226 return snd_interval_value(i);
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EINVAL;
1229}
1230
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001231EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Takashi Iwai877211f2005-11-17 13:59:38 +01001233void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 snd_pcm_hw_param_t var)
1235{
1236 if (hw_is_mask(var)) {
1237 snd_mask_none(hw_param_mask(params, var));
1238 params->cmask |= 1 << var;
1239 params->rmask |= 1 << var;
1240 } else if (hw_is_interval(var)) {
1241 snd_interval_none(hw_param_interval(params, var));
1242 params->cmask |= 1 << var;
1243 params->rmask |= 1 << var;
1244 } else {
1245 snd_BUG();
1246 }
1247}
1248
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001249EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Takashi Iwai877211f2005-11-17 13:59:38 +01001251static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001252 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 int changed;
1255 if (hw_is_mask(var))
1256 changed = snd_mask_refine_first(hw_param_mask(params, var));
1257 else if (hw_is_interval(var))
1258 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (changed) {
1262 params->cmask |= 1 << var;
1263 params->rmask |= 1 << var;
1264 }
1265 return changed;
1266}
1267
1268
1269/**
1270 * snd_pcm_hw_param_first
Takashi Iwaidf8db932005-09-07 13:38:19 +02001271 * @pcm: PCM instance
1272 * @params: the hw_params instance
1273 * @var: parameter to retrieve
1274 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 *
1276 * Inside configuration space defined by PARAMS remove from PAR all
1277 * values > minimum. Reduce configuration space accordingly.
1278 * Return the minimum.
1279 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001280int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1281 struct snd_pcm_hw_params *params,
1282 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 int changed = _snd_pcm_hw_param_first(params, var);
1285 if (changed < 0)
1286 return changed;
1287 if (params->rmask) {
1288 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001289 snd_assert(err >= 0, return err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 return snd_pcm_hw_param_value(params, var, dir);
1292}
1293
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001294EXPORT_SYMBOL(snd_pcm_hw_param_first);
1295
Takashi Iwai877211f2005-11-17 13:59:38 +01001296static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001297 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 int changed;
1300 if (hw_is_mask(var))
1301 changed = snd_mask_refine_last(hw_param_mask(params, var));
1302 else if (hw_is_interval(var))
1303 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001304 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (changed) {
1307 params->cmask |= 1 << var;
1308 params->rmask |= 1 << var;
1309 }
1310 return changed;
1311}
1312
1313
1314/**
1315 * snd_pcm_hw_param_last
Takashi Iwaidf8db932005-09-07 13:38:19 +02001316 * @pcm: PCM instance
1317 * @params: the hw_params instance
1318 * @var: parameter to retrieve
1319 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 *
1321 * Inside configuration space defined by PARAMS remove from PAR all
1322 * values < maximum. Reduce configuration space accordingly.
1323 * Return the maximum.
1324 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001325int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1326 struct snd_pcm_hw_params *params,
1327 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int changed = _snd_pcm_hw_param_last(params, var);
1330 if (changed < 0)
1331 return changed;
1332 if (params->rmask) {
1333 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001334 snd_assert(err >= 0, return err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 return snd_pcm_hw_param_value(params, var, dir);
1337}
1338
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001339EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341/**
1342 * snd_pcm_hw_param_choose
Takashi Iwaidf8db932005-09-07 13:38:19 +02001343 * @pcm: PCM instance
1344 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *
1346 * Choose one configuration from configuration space defined by PARAMS
1347 * The configuration chosen is that obtained fixing in this order:
1348 * first access, first format, first subformat, min channels,
1349 * min rate, min period time, max buffer size, min tick time
1350 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001351int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1352 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001354 static int vars[] = {
1355 SNDRV_PCM_HW_PARAM_ACCESS,
1356 SNDRV_PCM_HW_PARAM_FORMAT,
1357 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1358 SNDRV_PCM_HW_PARAM_CHANNELS,
1359 SNDRV_PCM_HW_PARAM_RATE,
1360 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1361 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1362 SNDRV_PCM_HW_PARAM_TICK_TIME,
1363 -1
1364 };
1365 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001367 for (v = vars; *v != -1; v++) {
1368 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1369 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1370 else
1371 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1372 snd_assert(err >= 0, return err);
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375}
1376
Takashi Iwai877211f2005-11-17 13:59:38 +01001377static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 void *arg)
1379{
Takashi Iwai877211f2005-11-17 13:59:38 +01001380 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 unsigned long flags;
1382 snd_pcm_stream_lock_irqsave(substream, flags);
1383 if (snd_pcm_running(substream) &&
1384 snd_pcm_update_hw_ptr(substream) >= 0)
1385 runtime->status->hw_ptr %= runtime->buffer_size;
1386 else
1387 runtime->status->hw_ptr = 0;
1388 snd_pcm_stream_unlock_irqrestore(substream, flags);
1389 return 0;
1390}
1391
Takashi Iwai877211f2005-11-17 13:59:38 +01001392static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 void *arg)
1394{
Takashi Iwai877211f2005-11-17 13:59:38 +01001395 struct snd_pcm_channel_info *info = arg;
1396 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int width;
1398 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1399 info->offset = -1;
1400 return 0;
1401 }
1402 width = snd_pcm_format_physical_width(runtime->format);
1403 if (width < 0)
1404 return width;
1405 info->offset = 0;
1406 switch (runtime->access) {
1407 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1408 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1409 info->first = info->channel * width;
1410 info->step = runtime->channels * width;
1411 break;
1412 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1413 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1414 {
1415 size_t size = runtime->dma_bytes / runtime->channels;
1416 info->first = info->channel * size * 8;
1417 info->step = width;
1418 break;
1419 }
1420 default:
1421 snd_BUG();
1422 break;
1423 }
1424 return 0;
1425}
1426
1427/**
1428 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1429 * @substream: the pcm substream instance
1430 * @cmd: ioctl command
1431 * @arg: ioctl argument
1432 *
1433 * Processes the generic ioctl commands for PCM.
1434 * Can be passed as the ioctl callback for PCM ops.
1435 *
1436 * Returns zero if successful, or a negative error code on failure.
1437 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001438int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 unsigned int cmd, void *arg)
1440{
1441 switch (cmd) {
1442 case SNDRV_PCM_IOCTL1_INFO:
1443 return 0;
1444 case SNDRV_PCM_IOCTL1_RESET:
1445 return snd_pcm_lib_ioctl_reset(substream, arg);
1446 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1447 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1448 }
1449 return -ENXIO;
1450}
1451
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001452EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/*
1455 * Conditions
1456 */
1457
Takashi Iwai877211f2005-11-17 13:59:38 +01001458static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 unsigned long ticks)
1460{
Takashi Iwai877211f2005-11-17 13:59:38 +01001461 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (ticks == 0)
1463 del_timer(&runtime->tick_timer);
1464 else {
1465 ticks += (1000000 / HZ) - 1;
1466 ticks /= (1000000 / HZ);
1467 mod_timer(&runtime->tick_timer, jiffies + ticks);
1468 }
1469}
1470
1471/* Temporary alias */
Takashi Iwai877211f2005-11-17 13:59:38 +01001472void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 snd_pcm_system_tick_set(substream, ticks);
1475}
1476
Takashi Iwai877211f2005-11-17 13:59:38 +01001477void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Takashi Iwai877211f2005-11-17 13:59:38 +01001479 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 snd_pcm_uframes_t frames = ULONG_MAX;
1481 snd_pcm_uframes_t avail, dist;
1482 unsigned int ticks;
1483 u_int64_t n;
1484 u_int32_t r;
1485 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1486 if (runtime->silence_size >= runtime->boundary) {
1487 frames = 1;
1488 } else if (runtime->silence_size > 0 &&
1489 runtime->silence_filled < runtime->buffer_size) {
1490 snd_pcm_sframes_t noise_dist;
1491 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
Takashi Iwai235475c2005-12-07 15:28:07 +01001492 if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold)
1493 frames = noise_dist - runtime->silence_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 avail = snd_pcm_playback_avail(runtime);
1496 } else {
1497 avail = snd_pcm_capture_avail(runtime);
1498 }
1499 if (avail < runtime->control->avail_min) {
Marcin Åšlusarzbe3e0112007-12-14 12:52:53 +01001500 snd_pcm_sframes_t to_avail_min =
1501 runtime->control->avail_min - avail;
1502 if (to_avail_min > 0 &&
1503 frames > (snd_pcm_uframes_t)to_avail_min)
1504 frames = to_avail_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 if (avail < runtime->buffer_size) {
Marcin Åšlusarzbe3e0112007-12-14 12:52:53 +01001507 snd_pcm_sframes_t to_buffer_size =
1508 runtime->buffer_size - avail;
1509 if (to_buffer_size > 0 &&
1510 frames > (snd_pcm_uframes_t)to_buffer_size)
1511 frames = to_buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 if (frames == ULONG_MAX) {
1514 snd_pcm_tick_set(substream, 0);
1515 return;
1516 }
1517 dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
1518 /* Distance to next interrupt */
1519 dist = runtime->period_size - dist % runtime->period_size;
1520 if (dist <= frames) {
1521 snd_pcm_tick_set(substream, 0);
1522 return;
1523 }
1524 /* the base time is us */
1525 n = frames;
1526 n *= 1000000;
1527 div64_32(&n, runtime->tick_time * runtime->rate, &r);
1528 ticks = n + (r > 0 ? 1 : 0);
1529 if (ticks < runtime->sleep_min)
1530 ticks = runtime->sleep_min;
1531 snd_pcm_tick_set(substream, (unsigned long) ticks);
1532}
1533
Takashi Iwai877211f2005-11-17 13:59:38 +01001534void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwai877211f2005-11-17 13:59:38 +01001536 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 unsigned long flags;
1538
1539 snd_assert(substream != NULL, return);
1540 runtime = substream->runtime;
1541 snd_assert(runtime != NULL, return);
1542
1543 snd_pcm_stream_lock_irqsave(substream, flags);
1544 if (!snd_pcm_running(substream) ||
1545 snd_pcm_update_hw_ptr(substream) < 0)
1546 goto _end;
1547 if (runtime->sleep_min)
1548 snd_pcm_tick_prepare(substream);
1549 _end:
1550 snd_pcm_stream_unlock_irqrestore(substream, flags);
1551}
1552
1553/**
1554 * snd_pcm_period_elapsed - update the pcm status for the next period
1555 * @substream: the pcm substream instance
1556 *
1557 * This function is called from the interrupt handler when the
1558 * PCM has processed the period size. It will update the current
1559 * pointer, set up the tick, wake up sleepers, etc.
1560 *
1561 * Even if more than one periods have elapsed since the last call, you
1562 * have to call this only once.
1563 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001564void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Takashi Iwai877211f2005-11-17 13:59:38 +01001566 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 unsigned long flags;
1568
1569 snd_assert(substream != NULL, return);
1570 runtime = substream->runtime;
1571 snd_assert(runtime != NULL, return);
1572
1573 if (runtime->transfer_ack_begin)
1574 runtime->transfer_ack_begin(substream);
1575
1576 snd_pcm_stream_lock_irqsave(substream, flags);
1577 if (!snd_pcm_running(substream) ||
1578 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1579 goto _end;
1580
1581 if (substream->timer_running)
1582 snd_timer_interrupt(substream->timer, 1);
1583 if (runtime->sleep_min)
1584 snd_pcm_tick_prepare(substream);
1585 _end:
1586 snd_pcm_stream_unlock_irqrestore(substream, flags);
1587 if (runtime->transfer_ack_end)
1588 runtime->transfer_ack_end(substream);
1589 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1590}
1591
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001592EXPORT_SYMBOL(snd_pcm_period_elapsed);
1593
Takashi Iwai13075512008-01-08 18:08:14 +01001594/*
1595 * Wait until avail_min data becomes available
1596 * Returns a negative error code if any error occurs during operation.
1597 * The available space is stored on availp. When err = 0 and avail = 0
1598 * on the capture stream, it indicates the stream is in DRAINING state.
1599 */
1600static int wait_for_avail_min(struct snd_pcm_substream *substream,
1601 snd_pcm_uframes_t *availp)
1602{
1603 struct snd_pcm_runtime *runtime = substream->runtime;
1604 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1605 wait_queue_t wait;
1606 int err = 0;
1607 snd_pcm_uframes_t avail = 0;
1608 long tout;
1609
1610 init_waitqueue_entry(&wait, current);
1611 add_wait_queue(&runtime->sleep, &wait);
1612 for (;;) {
1613 if (signal_pending(current)) {
1614 err = -ERESTARTSYS;
1615 break;
1616 }
1617 set_current_state(TASK_INTERRUPTIBLE);
1618 snd_pcm_stream_unlock_irq(substream);
1619 tout = schedule_timeout(msecs_to_jiffies(10000));
1620 snd_pcm_stream_lock_irq(substream);
1621 switch (runtime->status->state) {
1622 case SNDRV_PCM_STATE_SUSPENDED:
1623 err = -ESTRPIPE;
1624 goto _endloop;
1625 case SNDRV_PCM_STATE_XRUN:
1626 err = -EPIPE;
1627 goto _endloop;
1628 case SNDRV_PCM_STATE_DRAINING:
1629 if (is_playback)
1630 err = -EPIPE;
1631 else
1632 avail = 0; /* indicate draining */
1633 goto _endloop;
1634 case SNDRV_PCM_STATE_OPEN:
1635 case SNDRV_PCM_STATE_SETUP:
1636 case SNDRV_PCM_STATE_DISCONNECTED:
1637 err = -EBADFD;
1638 goto _endloop;
1639 }
1640 if (!tout) {
1641 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1642 is_playback ? "playback" : "capture");
1643 err = -EIO;
1644 break;
1645 }
1646 if (is_playback)
1647 avail = snd_pcm_playback_avail(runtime);
1648 else
1649 avail = snd_pcm_capture_avail(runtime);
1650 if (avail >= runtime->control->avail_min)
1651 break;
1652 }
1653 _endloop:
1654 remove_wait_queue(&runtime->sleep, &wait);
1655 *availp = avail;
1656 return err;
1657}
1658
Takashi Iwai877211f2005-11-17 13:59:38 +01001659static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 unsigned int hwoff,
1661 unsigned long data, unsigned int off,
1662 snd_pcm_uframes_t frames)
1663{
Takashi Iwai877211f2005-11-17 13:59:38 +01001664 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int err;
1666 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1667 if (substream->ops->copy) {
1668 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1669 return err;
1670 } else {
1671 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1672 snd_assert(runtime->dma_area, return -EFAULT);
1673 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1674 return -EFAULT;
1675 }
1676 return 0;
1677}
1678
Takashi Iwai877211f2005-11-17 13:59:38 +01001679typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 unsigned long data, unsigned int off,
1681 snd_pcm_uframes_t size);
1682
Takashi Iwai877211f2005-11-17 13:59:38 +01001683static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 unsigned long data,
1685 snd_pcm_uframes_t size,
1686 int nonblock,
1687 transfer_f transfer)
1688{
Takashi Iwai877211f2005-11-17 13:59:38 +01001689 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 snd_pcm_uframes_t xfer = 0;
1691 snd_pcm_uframes_t offset = 0;
1692 int err = 0;
1693
1694 if (size == 0)
1695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 snd_pcm_stream_lock_irq(substream);
1698 switch (runtime->status->state) {
1699 case SNDRV_PCM_STATE_PREPARED:
1700 case SNDRV_PCM_STATE_RUNNING:
1701 case SNDRV_PCM_STATE_PAUSED:
1702 break;
1703 case SNDRV_PCM_STATE_XRUN:
1704 err = -EPIPE;
1705 goto _end_unlock;
1706 case SNDRV_PCM_STATE_SUSPENDED:
1707 err = -ESTRPIPE;
1708 goto _end_unlock;
1709 default:
1710 err = -EBADFD;
1711 goto _end_unlock;
1712 }
1713
1714 while (size > 0) {
1715 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1716 snd_pcm_uframes_t avail;
1717 snd_pcm_uframes_t cont;
1718 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1719 snd_pcm_update_hw_ptr(substream);
1720 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001721 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (nonblock) {
1723 err = -EAGAIN;
1724 goto _end_unlock;
1725 }
Takashi Iwai13075512008-01-08 18:08:14 +01001726 err = wait_for_avail_min(substream, &avail);
1727 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 frames = size > avail ? avail : size;
1731 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1732 if (frames > cont)
1733 frames = cont;
1734 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1735 appl_ptr = runtime->control->appl_ptr;
1736 appl_ofs = appl_ptr % runtime->buffer_size;
1737 snd_pcm_stream_unlock_irq(substream);
1738 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1739 goto _end;
1740 snd_pcm_stream_lock_irq(substream);
1741 switch (runtime->status->state) {
1742 case SNDRV_PCM_STATE_XRUN:
1743 err = -EPIPE;
1744 goto _end_unlock;
1745 case SNDRV_PCM_STATE_SUSPENDED:
1746 err = -ESTRPIPE;
1747 goto _end_unlock;
1748 default:
1749 break;
1750 }
1751 appl_ptr += frames;
1752 if (appl_ptr >= runtime->boundary)
1753 appl_ptr -= runtime->boundary;
1754 runtime->control->appl_ptr = appl_ptr;
1755 if (substream->ops->ack)
1756 substream->ops->ack(substream);
1757
1758 offset += frames;
1759 size -= frames;
1760 xfer += frames;
1761 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1762 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1763 err = snd_pcm_start(substream);
1764 if (err < 0)
1765 goto _end_unlock;
1766 }
1767 if (runtime->sleep_min &&
1768 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1769 snd_pcm_tick_prepare(substream);
1770 }
1771 _end_unlock:
1772 snd_pcm_stream_unlock_irq(substream);
1773 _end:
1774 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1775}
1776
Takashi Iwai877211f2005-11-17 13:59:38 +01001777snd_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 -07001778{
Takashi Iwai877211f2005-11-17 13:59:38 +01001779 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 int nonblock;
1781
1782 snd_assert(substream != NULL, return -ENXIO);
1783 runtime = substream->runtime;
1784 snd_assert(runtime != NULL, return -ENXIO);
1785 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1786 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1787 return -EBADFD;
1788
Takashi Iwai0df63e42006-04-28 15:13:41 +02001789 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1792 runtime->channels > 1)
1793 return -EINVAL;
1794 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1795 snd_pcm_lib_write_transfer);
1796}
1797
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001798EXPORT_SYMBOL(snd_pcm_lib_write);
1799
Takashi Iwai877211f2005-11-17 13:59:38 +01001800static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 unsigned int hwoff,
1802 unsigned long data, unsigned int off,
1803 snd_pcm_uframes_t frames)
1804{
Takashi Iwai877211f2005-11-17 13:59:38 +01001805 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 int err;
1807 void __user **bufs = (void __user **)data;
1808 int channels = runtime->channels;
1809 int c;
1810 if (substream->ops->copy) {
1811 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1812 for (c = 0; c < channels; ++c, ++bufs) {
1813 if (*bufs == NULL) {
1814 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1815 return err;
1816 } else {
1817 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1818 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1819 return err;
1820 }
1821 }
1822 } else {
1823 /* default transfer behaviour */
1824 size_t dma_csize = runtime->dma_bytes / channels;
1825 snd_assert(runtime->dma_area, return -EFAULT);
1826 for (c = 0; c < channels; ++c, ++bufs) {
1827 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1828 if (*bufs == NULL) {
1829 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1830 } else {
1831 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1832 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1833 return -EFAULT;
1834 }
1835 }
1836 }
1837 return 0;
1838}
1839
Takashi Iwai877211f2005-11-17 13:59:38 +01001840snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 void __user **bufs,
1842 snd_pcm_uframes_t frames)
1843{
Takashi Iwai877211f2005-11-17 13:59:38 +01001844 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int nonblock;
1846
1847 snd_assert(substream != NULL, return -ENXIO);
1848 runtime = substream->runtime;
1849 snd_assert(runtime != NULL, return -ENXIO);
1850 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1851 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1852 return -EBADFD;
1853
Takashi Iwai0df63e42006-04-28 15:13:41 +02001854 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1857 return -EINVAL;
1858 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1859 nonblock, snd_pcm_lib_writev_transfer);
1860}
1861
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001862EXPORT_SYMBOL(snd_pcm_lib_writev);
1863
Takashi Iwai877211f2005-11-17 13:59:38 +01001864static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 unsigned int hwoff,
1866 unsigned long data, unsigned int off,
1867 snd_pcm_uframes_t frames)
1868{
Takashi Iwai877211f2005-11-17 13:59:38 +01001869 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int err;
1871 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1872 if (substream->ops->copy) {
1873 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1874 return err;
1875 } else {
1876 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1877 snd_assert(runtime->dma_area, return -EFAULT);
1878 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1879 return -EFAULT;
1880 }
1881 return 0;
1882}
1883
Takashi Iwai877211f2005-11-17 13:59:38 +01001884static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned long data,
1886 snd_pcm_uframes_t size,
1887 int nonblock,
1888 transfer_f transfer)
1889{
Takashi Iwai877211f2005-11-17 13:59:38 +01001890 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 snd_pcm_uframes_t xfer = 0;
1892 snd_pcm_uframes_t offset = 0;
1893 int err = 0;
1894
1895 if (size == 0)
1896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 snd_pcm_stream_lock_irq(substream);
1899 switch (runtime->status->state) {
1900 case SNDRV_PCM_STATE_PREPARED:
1901 if (size >= runtime->start_threshold) {
1902 err = snd_pcm_start(substream);
1903 if (err < 0)
1904 goto _end_unlock;
1905 }
1906 break;
1907 case SNDRV_PCM_STATE_DRAINING:
1908 case SNDRV_PCM_STATE_RUNNING:
1909 case SNDRV_PCM_STATE_PAUSED:
1910 break;
1911 case SNDRV_PCM_STATE_XRUN:
1912 err = -EPIPE;
1913 goto _end_unlock;
1914 case SNDRV_PCM_STATE_SUSPENDED:
1915 err = -ESTRPIPE;
1916 goto _end_unlock;
1917 default:
1918 err = -EBADFD;
1919 goto _end_unlock;
1920 }
1921
1922 while (size > 0) {
1923 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1924 snd_pcm_uframes_t avail;
1925 snd_pcm_uframes_t cont;
1926 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1927 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001929 if (!avail) {
1930 if (runtime->status->state ==
1931 SNDRV_PCM_STATE_DRAINING) {
1932 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto _end_unlock;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (nonblock) {
1936 err = -EAGAIN;
1937 goto _end_unlock;
1938 }
Takashi Iwai13075512008-01-08 18:08:14 +01001939 err = wait_for_avail_min(substream, &avail);
1940 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001942 if (!avail)
1943 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 frames = size > avail ? avail : size;
1946 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1947 if (frames > cont)
1948 frames = cont;
1949 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1950 appl_ptr = runtime->control->appl_ptr;
1951 appl_ofs = appl_ptr % runtime->buffer_size;
1952 snd_pcm_stream_unlock_irq(substream);
1953 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1954 goto _end;
1955 snd_pcm_stream_lock_irq(substream);
1956 switch (runtime->status->state) {
1957 case SNDRV_PCM_STATE_XRUN:
1958 err = -EPIPE;
1959 goto _end_unlock;
1960 case SNDRV_PCM_STATE_SUSPENDED:
1961 err = -ESTRPIPE;
1962 goto _end_unlock;
1963 default:
1964 break;
1965 }
1966 appl_ptr += frames;
1967 if (appl_ptr >= runtime->boundary)
1968 appl_ptr -= runtime->boundary;
1969 runtime->control->appl_ptr = appl_ptr;
1970 if (substream->ops->ack)
1971 substream->ops->ack(substream);
1972
1973 offset += frames;
1974 size -= frames;
1975 xfer += frames;
1976 if (runtime->sleep_min &&
1977 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1978 snd_pcm_tick_prepare(substream);
1979 }
1980 _end_unlock:
1981 snd_pcm_stream_unlock_irq(substream);
1982 _end:
1983 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1984}
1985
Takashi Iwai877211f2005-11-17 13:59:38 +01001986snd_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 -07001987{
Takashi Iwai877211f2005-11-17 13:59:38 +01001988 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int nonblock;
1990
1991 snd_assert(substream != NULL, return -ENXIO);
1992 runtime = substream->runtime;
1993 snd_assert(runtime != NULL, return -ENXIO);
1994 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1995 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1996 return -EBADFD;
1997
Takashi Iwai0df63e42006-04-28 15:13:41 +02001998 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2000 return -EINVAL;
2001 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2002}
2003
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002004EXPORT_SYMBOL(snd_pcm_lib_read);
2005
Takashi Iwai877211f2005-11-17 13:59:38 +01002006static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 unsigned int hwoff,
2008 unsigned long data, unsigned int off,
2009 snd_pcm_uframes_t frames)
2010{
Takashi Iwai877211f2005-11-17 13:59:38 +01002011 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 int err;
2013 void __user **bufs = (void __user **)data;
2014 int channels = runtime->channels;
2015 int c;
2016 if (substream->ops->copy) {
2017 for (c = 0; c < channels; ++c, ++bufs) {
2018 char __user *buf;
2019 if (*bufs == NULL)
2020 continue;
2021 buf = *bufs + samples_to_bytes(runtime, off);
2022 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2023 return err;
2024 }
2025 } else {
2026 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2027 snd_assert(runtime->dma_area, return -EFAULT);
2028 for (c = 0; c < channels; ++c, ++bufs) {
2029 char *hwbuf;
2030 char __user *buf;
2031 if (*bufs == NULL)
2032 continue;
2033
2034 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2035 buf = *bufs + samples_to_bytes(runtime, off);
2036 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2037 return -EFAULT;
2038 }
2039 }
2040 return 0;
2041}
2042
Takashi Iwai877211f2005-11-17 13:59:38 +01002043snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 void __user **bufs,
2045 snd_pcm_uframes_t frames)
2046{
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int nonblock;
2049
2050 snd_assert(substream != NULL, return -ENXIO);
2051 runtime = substream->runtime;
2052 snd_assert(runtime != NULL, return -ENXIO);
2053 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2054 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2055 return -EBADFD;
2056
Takashi Iwai0df63e42006-04-28 15:13:41 +02002057 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2059 return -EINVAL;
2060 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063EXPORT_SYMBOL(snd_pcm_lib_readv);