blob: 1533f0379e9d57abe850c0e636952afa5ccc397c [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 }
88 snd_assert(frames <= runtime->buffer_size, return);
89 if (frames == 0)
90 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020091 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 while (frames > 0) {
93 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
94 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
95 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
96 if (substream->ops->silence) {
97 int err;
98 err = substream->ops->silence(substream, -1, ofs, transfer);
99 snd_assert(err >= 0, );
100 } else {
101 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
102 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
103 }
104 } else {
105 unsigned int c;
106 unsigned int channels = runtime->channels;
107 if (substream->ops->silence) {
108 for (c = 0; c < channels; ++c) {
109 int err;
110 err = substream->ops->silence(substream, c, ofs, transfer);
111 snd_assert(err >= 0, );
112 }
113 } else {
114 size_t dma_csize = runtime->dma_bytes / channels;
115 for (c = 0; c < channels; ++c) {
116 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
117 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
118 }
119 }
120 }
121 runtime->silence_filled += transfer;
122 frames -= transfer;
123 ofs = 0;
124 }
125}
126
Takashi Iwai877211f2005-11-17 13:59:38 +0100127static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200130#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (substream->pstr->xrun_debug) {
132 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
133 substream->pcm->card->number,
134 substream->pcm->device,
135 substream->stream ? 'c' : 'p');
136 if (substream->pstr->xrun_debug > 1)
137 dump_stack();
138 }
139#endif
140}
141
Takashi Iwai877211f2005-11-17 13:59:38 +0100142static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
143 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 snd_pcm_uframes_t pos;
146
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100147 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
148 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 pos = substream->ops->pointer(substream);
150 if (pos == SNDRV_PCM_POS_XRUN)
151 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifdef CONFIG_SND_DEBUG
153 if (pos >= runtime->buffer_size) {
154 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 +0200155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 pos -= pos % runtime->min_align;
158 return pos;
159}
160
Takashi Iwai877211f2005-11-17 13:59:38 +0100161static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
162 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 snd_pcm_uframes_t avail;
165
166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
167 avail = snd_pcm_playback_avail(runtime);
168 else
169 avail = snd_pcm_capture_avail(runtime);
170 if (avail > runtime->avail_max)
171 runtime->avail_max = avail;
172 if (avail >= runtime->stop_threshold) {
173 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
174 snd_pcm_drain_done(substream);
175 else
176 xrun(substream);
177 return -EPIPE;
178 }
179 if (avail >= runtime->control->avail_min)
180 wake_up(&runtime->sleep);
181 return 0;
182}
183
Takashi Iwai877211f2005-11-17 13:59:38 +0100184static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Takashi Iwai877211f2005-11-17 13:59:38 +0100186 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 snd_pcm_uframes_t pos;
188 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
189 snd_pcm_sframes_t delta;
190
191 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
192 if (pos == SNDRV_PCM_POS_XRUN) {
193 xrun(substream);
194 return -EPIPE;
195 }
196 if (runtime->period_size == runtime->buffer_size)
197 goto __next_buf;
198 new_hw_ptr = runtime->hw_ptr_base + pos;
199 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
200
201 delta = hw_ptr_interrupt - new_hw_ptr;
202 if (delta > 0) {
203 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200204#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
206 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);
207 if (substream->pstr->xrun_debug > 1)
208 dump_stack();
209 }
210#endif
211 return 0;
212 }
213 __next_buf:
214 runtime->hw_ptr_base += runtime->buffer_size;
215 if (runtime->hw_ptr_base == runtime->boundary)
216 runtime->hw_ptr_base = 0;
217 new_hw_ptr = runtime->hw_ptr_base + pos;
218 }
219
220 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
221 runtime->silence_size > 0)
222 snd_pcm_playback_silence(substream, new_hw_ptr);
223
224 runtime->status->hw_ptr = new_hw_ptr;
225 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
226
227 return snd_pcm_update_hw_ptr_post(substream, runtime);
228}
229
230/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100231int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Takashi Iwai877211f2005-11-17 13:59:38 +0100233 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 snd_pcm_uframes_t pos;
235 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
236 snd_pcm_sframes_t delta;
237
238 old_hw_ptr = runtime->status->hw_ptr;
239 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
240 if (pos == SNDRV_PCM_POS_XRUN) {
241 xrun(substream);
242 return -EPIPE;
243 }
244 new_hw_ptr = runtime->hw_ptr_base + pos;
245
246 delta = old_hw_ptr - new_hw_ptr;
247 if (delta > 0) {
248 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200249#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
251 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);
252 if (substream->pstr->xrun_debug > 1)
253 dump_stack();
254 }
255#endif
256 return 0;
257 }
258 runtime->hw_ptr_base += runtime->buffer_size;
259 if (runtime->hw_ptr_base == runtime->boundary)
260 runtime->hw_ptr_base = 0;
261 new_hw_ptr = runtime->hw_ptr_base + pos;
262 }
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
264 runtime->silence_size > 0)
265 snd_pcm_playback_silence(substream, new_hw_ptr);
266
267 runtime->status->hw_ptr = new_hw_ptr;
268
269 return snd_pcm_update_hw_ptr_post(substream, runtime);
270}
271
272/**
273 * snd_pcm_set_ops - set the PCM operators
274 * @pcm: the pcm instance
275 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
276 * @ops: the operator table
277 *
278 * Sets the given PCM operators to the pcm instance.
279 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100280void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Takashi Iwai877211f2005-11-17 13:59:38 +0100282 struct snd_pcm_str *stream = &pcm->streams[direction];
283 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 for (substream = stream->substream; substream != NULL; substream = substream->next)
286 substream->ops = ops;
287}
288
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200289EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/**
292 * snd_pcm_sync - set the PCM sync id
293 * @substream: the pcm substream
294 *
295 * Sets the PCM sync identifier for the card.
296 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100297void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwai877211f2005-11-17 13:59:38 +0100299 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 runtime->sync.id32[0] = substream->pcm->card->number;
302 runtime->sync.id32[1] = -1;
303 runtime->sync.id32[2] = -1;
304 runtime->sync.id32[3] = -1;
305}
306
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200307EXPORT_SYMBOL(snd_pcm_set_sync);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * Standard ioctl routine
311 */
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static inline unsigned int div32(unsigned int a, unsigned int b,
314 unsigned int *r)
315{
316 if (b == 0) {
317 *r = 0;
318 return UINT_MAX;
319 }
320 *r = a % b;
321 return a / b;
322}
323
324static inline unsigned int div_down(unsigned int a, unsigned int b)
325{
326 if (b == 0)
327 return UINT_MAX;
328 return a / b;
329}
330
331static inline unsigned int div_up(unsigned int a, unsigned int b)
332{
333 unsigned int r;
334 unsigned int q;
335 if (b == 0)
336 return UINT_MAX;
337 q = div32(a, b, &r);
338 if (r)
339 ++q;
340 return q;
341}
342
343static inline unsigned int mul(unsigned int a, unsigned int b)
344{
345 if (a == 0)
346 return 0;
347 if (div_down(UINT_MAX, a) < b)
348 return UINT_MAX;
349 return a * b;
350}
351
352static inline unsigned int muldiv32(unsigned int a, unsigned int b,
353 unsigned int c, unsigned int *r)
354{
355 u_int64_t n = (u_int64_t) a * b;
356 if (c == 0) {
357 snd_assert(n > 0, );
358 *r = 0;
359 return UINT_MAX;
360 }
361 div64_32(&n, c, r);
362 if (n >= UINT_MAX) {
363 *r = 0;
364 return UINT_MAX;
365 }
366 return n;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
370 * snd_interval_refine - refine the interval value of configurator
371 * @i: the interval value to refine
372 * @v: the interval value to refer to
373 *
374 * Refines the interval value with the reference value.
375 * The interval is changed to the range satisfying both intervals.
376 * The interval status (min, max, integer, etc.) are evaluated.
377 *
378 * Returns non-zero if the value is changed, zero if not changed.
379 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100380int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 int changed = 0;
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200383 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (i->min < v->min) {
385 i->min = v->min;
386 i->openmin = v->openmin;
387 changed = 1;
388 } else if (i->min == v->min && !i->openmin && v->openmin) {
389 i->openmin = 1;
390 changed = 1;
391 }
392 if (i->max > v->max) {
393 i->max = v->max;
394 i->openmax = v->openmax;
395 changed = 1;
396 } else if (i->max == v->max && !i->openmax && v->openmax) {
397 i->openmax = 1;
398 changed = 1;
399 }
400 if (!i->integer && v->integer) {
401 i->integer = 1;
402 changed = 1;
403 }
404 if (i->integer) {
405 if (i->openmin) {
406 i->min++;
407 i->openmin = 0;
408 }
409 if (i->openmax) {
410 i->max--;
411 i->openmax = 0;
412 }
413 } else if (!i->openmin && !i->openmax && i->min == i->max)
414 i->integer = 1;
415 if (snd_interval_checkempty(i)) {
416 snd_interval_none(i);
417 return -EINVAL;
418 }
419 return changed;
420}
421
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200422EXPORT_SYMBOL(snd_interval_refine);
423
Takashi Iwai877211f2005-11-17 13:59:38 +0100424static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200426 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (snd_interval_single(i))
428 return 0;
429 i->max = i->min;
430 i->openmax = i->openmin;
431 if (i->openmax)
432 i->max++;
433 return 1;
434}
435
Takashi Iwai877211f2005-11-17 13:59:38 +0100436static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200438 snd_assert(!snd_interval_empty(i), return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (snd_interval_single(i))
440 return 0;
441 i->min = i->max;
442 i->openmin = i->openmax;
443 if (i->openmin)
444 i->min--;
445 return 1;
446}
447
Takashi Iwai877211f2005-11-17 13:59:38 +0100448void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 if (a->empty || b->empty) {
451 snd_interval_none(c);
452 return;
453 }
454 c->empty = 0;
455 c->min = mul(a->min, b->min);
456 c->openmin = (a->openmin || b->openmin);
457 c->max = mul(a->max, b->max);
458 c->openmax = (a->openmax || b->openmax);
459 c->integer = (a->integer && b->integer);
460}
461
462/**
463 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200464 * @a: dividend
465 * @b: divisor
466 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 * c = a / b
469 *
470 * Returns non-zero if the value is changed, zero if not changed.
471 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100472void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 unsigned int r;
475 if (a->empty || b->empty) {
476 snd_interval_none(c);
477 return;
478 }
479 c->empty = 0;
480 c->min = div32(a->min, b->max, &r);
481 c->openmin = (r || a->openmin || b->openmax);
482 if (b->min > 0) {
483 c->max = div32(a->max, b->min, &r);
484 if (r) {
485 c->max++;
486 c->openmax = 1;
487 } else
488 c->openmax = (a->openmax || b->openmin);
489 } else {
490 c->max = UINT_MAX;
491 c->openmax = 0;
492 }
493 c->integer = 0;
494}
495
496/**
497 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200498 * @a: dividend 1
499 * @b: dividend 2
500 * @k: divisor (as integer)
501 * @c: result
502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * c = a * b / k
504 *
505 * Returns non-zero if the value is changed, zero if not changed.
506 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100507void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
508 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned int r;
511 if (a->empty || b->empty) {
512 snd_interval_none(c);
513 return;
514 }
515 c->empty = 0;
516 c->min = muldiv32(a->min, b->min, k, &r);
517 c->openmin = (r || a->openmin || b->openmin);
518 c->max = muldiv32(a->max, b->max, k, &r);
519 if (r) {
520 c->max++;
521 c->openmax = 1;
522 } else
523 c->openmax = (a->openmax || b->openmax);
524 c->integer = 0;
525}
526
527/**
528 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200529 * @a: dividend 1
530 * @k: dividend 2 (as integer)
531 * @b: divisor
532 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
534 * c = a * k / b
535 *
536 * Returns non-zero if the value is changed, zero if not changed.
537 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100538void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
539 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned int r;
542 if (a->empty || b->empty) {
543 snd_interval_none(c);
544 return;
545 }
546 c->empty = 0;
547 c->min = muldiv32(a->min, k, b->max, &r);
548 c->openmin = (r || a->openmin || b->openmax);
549 if (b->min > 0) {
550 c->max = muldiv32(a->max, k, b->min, &r);
551 if (r) {
552 c->max++;
553 c->openmax = 1;
554 } else
555 c->openmax = (a->openmax || b->openmin);
556 } else {
557 c->max = UINT_MAX;
558 c->openmax = 0;
559 }
560 c->integer = 0;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* ---- */
564
565
566/**
567 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200568 * @i: interval to refine
569 * @rats_count: number of ratnum_t
570 * @rats: ratnum_t array
571 * @nump: pointer to store the resultant numerator
572 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * Returns non-zero if the value is changed, zero if not changed.
575 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100576int snd_interval_ratnum(struct snd_interval *i,
577 unsigned int rats_count, struct snd_ratnum *rats,
578 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 unsigned int best_num, best_diff, best_den;
581 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100582 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int err;
584
585 best_num = best_den = best_diff = 0;
586 for (k = 0; k < rats_count; ++k) {
587 unsigned int num = rats[k].num;
588 unsigned int den;
589 unsigned int q = i->min;
590 int diff;
591 if (q == 0)
592 q = 1;
593 den = div_down(num, q);
594 if (den < rats[k].den_min)
595 continue;
596 if (den > rats[k].den_max)
597 den = rats[k].den_max;
598 else {
599 unsigned int r;
600 r = (den - rats[k].den_min) % rats[k].den_step;
601 if (r != 0)
602 den -= r;
603 }
604 diff = num - q * den;
605 if (best_num == 0 ||
606 diff * best_den < best_diff * den) {
607 best_diff = diff;
608 best_den = den;
609 best_num = num;
610 }
611 }
612 if (best_den == 0) {
613 i->empty = 1;
614 return -EINVAL;
615 }
616 t.min = div_down(best_num, best_den);
617 t.openmin = !!(best_num % best_den);
618
619 best_num = best_den = best_diff = 0;
620 for (k = 0; k < rats_count; ++k) {
621 unsigned int num = rats[k].num;
622 unsigned int den;
623 unsigned int q = i->max;
624 int diff;
625 if (q == 0) {
626 i->empty = 1;
627 return -EINVAL;
628 }
629 den = div_up(num, q);
630 if (den > rats[k].den_max)
631 continue;
632 if (den < rats[k].den_min)
633 den = rats[k].den_min;
634 else {
635 unsigned int r;
636 r = (den - rats[k].den_min) % rats[k].den_step;
637 if (r != 0)
638 den += rats[k].den_step - r;
639 }
640 diff = q * den - num;
641 if (best_num == 0 ||
642 diff * best_den < best_diff * den) {
643 best_diff = diff;
644 best_den = den;
645 best_num = num;
646 }
647 }
648 if (best_den == 0) {
649 i->empty = 1;
650 return -EINVAL;
651 }
652 t.max = div_up(best_num, best_den);
653 t.openmax = !!(best_num % best_den);
654 t.integer = 0;
655 err = snd_interval_refine(i, &t);
656 if (err < 0)
657 return err;
658
659 if (snd_interval_single(i)) {
660 if (nump)
661 *nump = best_num;
662 if (denp)
663 *denp = best_den;
664 }
665 return err;
666}
667
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200668EXPORT_SYMBOL(snd_interval_ratnum);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/**
671 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200672 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100673 * @rats_count: number of struct ratden
674 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200675 * @nump: pointer to store the resultant numerator
676 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
678 * Returns non-zero if the value is changed, zero if not changed.
679 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100680static int snd_interval_ratden(struct snd_interval *i,
681 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned int *nump, unsigned int *denp)
683{
684 unsigned int best_num, best_diff, best_den;
685 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100686 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int err;
688
689 best_num = best_den = best_diff = 0;
690 for (k = 0; k < rats_count; ++k) {
691 unsigned int num;
692 unsigned int den = rats[k].den;
693 unsigned int q = i->min;
694 int diff;
695 num = mul(q, den);
696 if (num > rats[k].num_max)
697 continue;
698 if (num < rats[k].num_min)
699 num = rats[k].num_max;
700 else {
701 unsigned int r;
702 r = (num - rats[k].num_min) % rats[k].num_step;
703 if (r != 0)
704 num += rats[k].num_step - r;
705 }
706 diff = num - q * den;
707 if (best_num == 0 ||
708 diff * best_den < best_diff * den) {
709 best_diff = diff;
710 best_den = den;
711 best_num = num;
712 }
713 }
714 if (best_den == 0) {
715 i->empty = 1;
716 return -EINVAL;
717 }
718 t.min = div_down(best_num, best_den);
719 t.openmin = !!(best_num % best_den);
720
721 best_num = best_den = best_diff = 0;
722 for (k = 0; k < rats_count; ++k) {
723 unsigned int num;
724 unsigned int den = rats[k].den;
725 unsigned int q = i->max;
726 int diff;
727 num = mul(q, den);
728 if (num < rats[k].num_min)
729 continue;
730 if (num > rats[k].num_max)
731 num = rats[k].num_max;
732 else {
733 unsigned int r;
734 r = (num - rats[k].num_min) % rats[k].num_step;
735 if (r != 0)
736 num -= r;
737 }
738 diff = q * den - num;
739 if (best_num == 0 ||
740 diff * best_den < best_diff * den) {
741 best_diff = diff;
742 best_den = den;
743 best_num = num;
744 }
745 }
746 if (best_den == 0) {
747 i->empty = 1;
748 return -EINVAL;
749 }
750 t.max = div_up(best_num, best_den);
751 t.openmax = !!(best_num % best_den);
752 t.integer = 0;
753 err = snd_interval_refine(i, &t);
754 if (err < 0)
755 return err;
756
757 if (snd_interval_single(i)) {
758 if (nump)
759 *nump = best_num;
760 if (denp)
761 *denp = best_den;
762 }
763 return err;
764}
765
766/**
767 * snd_interval_list - refine the interval value from the list
768 * @i: the interval value to refine
769 * @count: the number of elements in the list
770 * @list: the value list
771 * @mask: the bit-mask to evaluate
772 *
773 * Refines the interval value from the list.
774 * When mask is non-zero, only the elements corresponding to bit 1 are
775 * evaluated.
776 *
777 * Returns non-zero if the value is changed, zero if not changed.
778 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100779int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 unsigned int k;
782 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100783
784 if (!count) {
785 i->empty = 1;
786 return -EINVAL;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 for (k = 0; k < count; k++) {
789 if (mask && !(mask & (1 << k)))
790 continue;
791 if (i->min == list[k] && !i->openmin)
792 goto _l1;
793 if (i->min < list[k]) {
794 i->min = list[k];
795 i->openmin = 0;
796 changed = 1;
797 goto _l1;
798 }
799 }
800 i->empty = 1;
801 return -EINVAL;
802 _l1:
803 for (k = count; k-- > 0;) {
804 if (mask && !(mask & (1 << k)))
805 continue;
806 if (i->max == list[k] && !i->openmax)
807 goto _l2;
808 if (i->max > list[k]) {
809 i->max = list[k];
810 i->openmax = 0;
811 changed = 1;
812 goto _l2;
813 }
814 }
815 i->empty = 1;
816 return -EINVAL;
817 _l2:
818 if (snd_interval_checkempty(i)) {
819 i->empty = 1;
820 return -EINVAL;
821 }
822 return changed;
823}
824
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200825EXPORT_SYMBOL(snd_interval_list);
826
Takashi Iwai877211f2005-11-17 13:59:38 +0100827static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 unsigned int n;
830 int changed = 0;
831 n = (i->min - min) % step;
832 if (n != 0 || i->openmin) {
833 i->min += step - n;
834 changed = 1;
835 }
836 n = (i->max - min) % step;
837 if (n != 0 || i->openmax) {
838 i->max -= n;
839 changed = 1;
840 }
841 if (snd_interval_checkempty(i)) {
842 i->empty = 1;
843 return -EINVAL;
844 }
845 return changed;
846}
847
848/* Info constraints helpers */
849
850/**
851 * snd_pcm_hw_rule_add - add the hw-constraint rule
852 * @runtime: the pcm runtime instance
853 * @cond: condition bits
854 * @var: the variable to evaluate
855 * @func: the evaluation function
856 * @private: the private data pointer passed to function
857 * @dep: the dependent variables
858 *
859 * Returns zero if successful, or a negative error code on failure.
860 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100861int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int var,
863 snd_pcm_hw_rule_func_t func, void *private,
864 int dep, ...)
865{
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
867 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int k;
869 va_list args;
870 va_start(args, dep);
871 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned int new_rules = constrs->rules_all + 16;
874 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
875 if (!new)
876 return -ENOMEM;
877 if (constrs->rules) {
878 memcpy(new, constrs->rules,
879 constrs->rules_num * sizeof(*c));
880 kfree(constrs->rules);
881 }
882 constrs->rules = new;
883 constrs->rules_all = new_rules;
884 }
885 c = &constrs->rules[constrs->rules_num];
886 c->cond = cond;
887 c->func = func;
888 c->var = var;
889 c->private = private;
890 k = 0;
891 while (1) {
892 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
893 c->deps[k++] = dep;
894 if (dep < 0)
895 break;
896 dep = va_arg(args, int);
897 }
898 constrs->rules_num++;
899 va_end(args);
900 return 0;
901}
902
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200903EXPORT_SYMBOL(snd_pcm_hw_rule_add);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/**
906 * snd_pcm_hw_constraint_mask
Takashi Iwaidf8db932005-09-07 13:38:19 +0200907 * @runtime: PCM runtime instance
908 * @var: hw_params variable to apply the mask
909 * @mask: the bitmap mask
910 *
911 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100913int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u_int32_t mask)
915{
Takashi Iwai877211f2005-11-17 13:59:38 +0100916 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
917 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *maskp->bits &= mask;
919 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
920 if (*maskp->bits == 0)
921 return -EINVAL;
922 return 0;
923}
924
925/**
926 * snd_pcm_hw_constraint_mask64
Takashi Iwaidf8db932005-09-07 13:38:19 +0200927 * @runtime: PCM runtime instance
928 * @var: hw_params variable to apply the mask
929 * @mask: the 64bit bitmap mask
930 *
931 * Apply the constraint of the given bitmap mask to a mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100933int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 u_int64_t mask)
935{
Takashi Iwai877211f2005-11-17 13:59:38 +0100936 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
937 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 maskp->bits[0] &= (u_int32_t)mask;
939 maskp->bits[1] &= (u_int32_t)(mask >> 32);
940 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
941 if (! maskp->bits[0] && ! maskp->bits[1])
942 return -EINVAL;
943 return 0;
944}
945
946/**
947 * snd_pcm_hw_constraint_integer
Takashi Iwaidf8db932005-09-07 13:38:19 +0200948 * @runtime: PCM runtime instance
949 * @var: hw_params variable to apply the integer constraint
950 *
951 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100953int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Takashi Iwai877211f2005-11-17 13:59:38 +0100955 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return snd_interval_setinteger(constrs_interval(constrs, var));
957}
958
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200959EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/**
962 * snd_pcm_hw_constraint_minmax
Takashi Iwaidf8db932005-09-07 13:38:19 +0200963 * @runtime: PCM runtime instance
964 * @var: hw_params variable to apply the range
965 * @min: the minimal value
966 * @max: the maximal value
967 *
968 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100970int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned int min, unsigned int max)
972{
Takashi Iwai877211f2005-11-17 13:59:38 +0100973 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
974 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 t.min = min;
976 t.max = max;
977 t.openmin = t.openmax = 0;
978 t.integer = 0;
979 return snd_interval_refine(constrs_interval(constrs, var), &t);
980}
981
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200982EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
985 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwai877211f2005-11-17 13:59:38 +0100987 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
989}
990
991
992/**
993 * snd_pcm_hw_constraint_list
Takashi Iwaidf8db932005-09-07 13:38:19 +0200994 * @runtime: PCM runtime instance
995 * @cond: condition bits
996 * @var: hw_params variable to apply the list constraint
997 * @l: list
998 *
999 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001001int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned int cond,
1003 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001004 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 return snd_pcm_hw_rule_add(runtime, cond, var,
1007 snd_pcm_hw_rule_list, l,
1008 var, -1);
1009}
1010
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001011EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1012
Takashi Iwai877211f2005-11-17 13:59:38 +01001013static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1014 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Takashi Iwai877211f2005-11-17 13:59:38 +01001016 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned int num = 0, den = 0;
1018 int err;
1019 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1020 r->nrats, r->rats, &num, &den);
1021 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1022 params->rate_num = num;
1023 params->rate_den = den;
1024 }
1025 return err;
1026}
1027
1028/**
1029 * snd_pcm_hw_constraint_ratnums
Takashi Iwaidf8db932005-09-07 13:38:19 +02001030 * @runtime: PCM runtime instance
1031 * @cond: condition bits
1032 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001033 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001035int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned int cond,
1037 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001038 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return snd_pcm_hw_rule_add(runtime, cond, var,
1041 snd_pcm_hw_rule_ratnums, r,
1042 var, -1);
1043}
1044
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001045EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1046
Takashi Iwai877211f2005-11-17 13:59:38 +01001047static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1048 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned int num = 0, den = 0;
1052 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1053 r->nrats, r->rats, &num, &den);
1054 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1055 params->rate_num = num;
1056 params->rate_den = den;
1057 }
1058 return err;
1059}
1060
1061/**
1062 * snd_pcm_hw_constraint_ratdens
Takashi Iwaidf8db932005-09-07 13:38:19 +02001063 * @runtime: PCM runtime instance
1064 * @cond: condition bits
1065 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001066 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001068int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 unsigned int cond,
1070 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 return snd_pcm_hw_rule_add(runtime, cond, var,
1074 snd_pcm_hw_rule_ratdens, r,
1075 var, -1);
1076}
1077
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001078EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1079
Takashi Iwai877211f2005-11-17 13:59:38 +01001080static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1081 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 unsigned int l = (unsigned long) rule->private;
1084 int width = l & 0xffff;
1085 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001086 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (snd_interval_single(i) && snd_interval_value(i) == width)
1088 params->msbits = msbits;
1089 return 0;
1090}
1091
1092/**
1093 * snd_pcm_hw_constraint_msbits
Takashi Iwaidf8db932005-09-07 13:38:19 +02001094 * @runtime: PCM runtime instance
1095 * @cond: condition bits
1096 * @width: sample bits width
1097 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001099int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 unsigned int cond,
1101 unsigned int width,
1102 unsigned int msbits)
1103{
1104 unsigned long l = (msbits << 16) | width;
1105 return snd_pcm_hw_rule_add(runtime, cond, -1,
1106 snd_pcm_hw_rule_msbits,
1107 (void*) l,
1108 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1109}
1110
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001111EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1112
Takashi Iwai877211f2005-11-17 13:59:38 +01001113static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1114 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 unsigned long step = (unsigned long) rule->private;
1117 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1118}
1119
1120/**
1121 * snd_pcm_hw_constraint_step
Takashi Iwaidf8db932005-09-07 13:38:19 +02001122 * @runtime: PCM runtime instance
1123 * @cond: condition bits
1124 * @var: hw_params variable to apply the step constraint
1125 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001127int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 unsigned int cond,
1129 snd_pcm_hw_param_t var,
1130 unsigned long step)
1131{
1132 return snd_pcm_hw_rule_add(runtime, cond, var,
1133 snd_pcm_hw_rule_step, (void *) step,
1134 var, -1);
1135}
1136
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001137EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1138
Takashi Iwai877211f2005-11-17 13:59:38 +01001139static 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 -07001140{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001141 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1143 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1144 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1145 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1146 };
1147 return snd_interval_list(hw_param_interval(params, rule->var),
1148 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1149}
1150
1151/**
1152 * snd_pcm_hw_constraint_pow2
Takashi Iwaidf8db932005-09-07 13:38:19 +02001153 * @runtime: PCM runtime instance
1154 * @cond: condition bits
1155 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001157int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned int cond,
1159 snd_pcm_hw_param_t var)
1160{
1161 return snd_pcm_hw_rule_add(runtime, cond, var,
1162 snd_pcm_hw_rule_pow2, NULL,
1163 var, -1);
1164}
1165
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001166EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1167
Takashi Iwai877211f2005-11-17 13:59:38 +01001168static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001169 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 if (hw_is_mask(var)) {
1172 snd_mask_any(hw_param_mask(params, var));
1173 params->cmask |= 1 << var;
1174 params->rmask |= 1 << var;
1175 return;
1176 }
1177 if (hw_is_interval(var)) {
1178 snd_interval_any(hw_param_interval(params, var));
1179 params->cmask |= 1 << var;
1180 params->rmask |= 1 << var;
1181 return;
1182 }
1183 snd_BUG();
1184}
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 unsigned int k;
1189 memset(params, 0, sizeof(*params));
1190 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1191 _snd_pcm_hw_param_any(params, k);
1192 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1193 _snd_pcm_hw_param_any(params, k);
1194 params->info = ~0U;
1195}
1196
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001197EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199/**
1200 * snd_pcm_hw_param_value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001201 * @params: the hw_params instance
1202 * @var: parameter to retrieve
1203 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
1205 * Return the value for field PAR if it's fixed in configuration space
1206 * defined by PARAMS. Return -EINVAL otherwise
1207 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001208int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1209 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001212 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!snd_mask_single(mask))
1214 return -EINVAL;
1215 if (dir)
1216 *dir = 0;
1217 return snd_mask_value(mask);
1218 }
1219 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001220 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!snd_interval_single(i))
1222 return -EINVAL;
1223 if (dir)
1224 *dir = i->openmin;
1225 return snd_interval_value(i);
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EINVAL;
1228}
1229
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001230EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Takashi Iwai877211f2005-11-17 13:59:38 +01001232void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 snd_pcm_hw_param_t var)
1234{
1235 if (hw_is_mask(var)) {
1236 snd_mask_none(hw_param_mask(params, var));
1237 params->cmask |= 1 << var;
1238 params->rmask |= 1 << var;
1239 } else if (hw_is_interval(var)) {
1240 snd_interval_none(hw_param_interval(params, var));
1241 params->cmask |= 1 << var;
1242 params->rmask |= 1 << var;
1243 } else {
1244 snd_BUG();
1245 }
1246}
1247
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001248EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001251 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 int changed;
1254 if (hw_is_mask(var))
1255 changed = snd_mask_refine_first(hw_param_mask(params, var));
1256 else if (hw_is_interval(var))
1257 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001258 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (changed) {
1261 params->cmask |= 1 << var;
1262 params->rmask |= 1 << var;
1263 }
1264 return changed;
1265}
1266
1267
1268/**
1269 * snd_pcm_hw_param_first
Takashi Iwaidf8db932005-09-07 13:38:19 +02001270 * @pcm: PCM instance
1271 * @params: the hw_params instance
1272 * @var: parameter to retrieve
1273 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 *
1275 * Inside configuration space defined by PARAMS remove from PAR all
1276 * values > minimum. Reduce configuration space accordingly.
1277 * Return the minimum.
1278 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001279int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1280 struct snd_pcm_hw_params *params,
1281 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int changed = _snd_pcm_hw_param_first(params, var);
1284 if (changed < 0)
1285 return changed;
1286 if (params->rmask) {
1287 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001288 snd_assert(err >= 0, return err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 return snd_pcm_hw_param_value(params, var, dir);
1291}
1292
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001293EXPORT_SYMBOL(snd_pcm_hw_param_first);
1294
Takashi Iwai877211f2005-11-17 13:59:38 +01001295static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001296 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 int changed;
1299 if (hw_is_mask(var))
1300 changed = snd_mask_refine_last(hw_param_mask(params, var));
1301 else if (hw_is_interval(var))
1302 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001303 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (changed) {
1306 params->cmask |= 1 << var;
1307 params->rmask |= 1 << var;
1308 }
1309 return changed;
1310}
1311
1312
1313/**
1314 * snd_pcm_hw_param_last
Takashi Iwaidf8db932005-09-07 13:38:19 +02001315 * @pcm: PCM instance
1316 * @params: the hw_params instance
1317 * @var: parameter to retrieve
1318 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 *
1320 * Inside configuration space defined by PARAMS remove from PAR all
1321 * values < maximum. Reduce configuration space accordingly.
1322 * Return the maximum.
1323 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001324int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1325 struct snd_pcm_hw_params *params,
1326 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 int changed = _snd_pcm_hw_param_last(params, var);
1329 if (changed < 0)
1330 return changed;
1331 if (params->rmask) {
1332 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001333 snd_assert(err >= 0, return err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 return snd_pcm_hw_param_value(params, var, dir);
1336}
1337
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001338EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340/**
1341 * snd_pcm_hw_param_choose
Takashi Iwaidf8db932005-09-07 13:38:19 +02001342 * @pcm: PCM instance
1343 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
1345 * Choose one configuration from configuration space defined by PARAMS
1346 * The configuration chosen is that obtained fixing in this order:
1347 * first access, first format, first subformat, min channels,
1348 * min rate, min period time, max buffer size, min tick time
1349 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001350int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1351 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001353 static int vars[] = {
1354 SNDRV_PCM_HW_PARAM_ACCESS,
1355 SNDRV_PCM_HW_PARAM_FORMAT,
1356 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1357 SNDRV_PCM_HW_PARAM_CHANNELS,
1358 SNDRV_PCM_HW_PARAM_RATE,
1359 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1360 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1361 SNDRV_PCM_HW_PARAM_TICK_TIME,
1362 -1
1363 };
1364 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001366 for (v = vars; *v != -1; v++) {
1367 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1368 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1369 else
1370 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1371 snd_assert(err >= 0, return err);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
Takashi Iwai877211f2005-11-17 13:59:38 +01001376static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 void *arg)
1378{
Takashi Iwai877211f2005-11-17 13:59:38 +01001379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 unsigned long flags;
1381 snd_pcm_stream_lock_irqsave(substream, flags);
1382 if (snd_pcm_running(substream) &&
1383 snd_pcm_update_hw_ptr(substream) >= 0)
1384 runtime->status->hw_ptr %= runtime->buffer_size;
1385 else
1386 runtime->status->hw_ptr = 0;
1387 snd_pcm_stream_unlock_irqrestore(substream, flags);
1388 return 0;
1389}
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 void *arg)
1393{
Takashi Iwai877211f2005-11-17 13:59:38 +01001394 struct snd_pcm_channel_info *info = arg;
1395 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int width;
1397 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1398 info->offset = -1;
1399 return 0;
1400 }
1401 width = snd_pcm_format_physical_width(runtime->format);
1402 if (width < 0)
1403 return width;
1404 info->offset = 0;
1405 switch (runtime->access) {
1406 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1407 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1408 info->first = info->channel * width;
1409 info->step = runtime->channels * width;
1410 break;
1411 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1412 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1413 {
1414 size_t size = runtime->dma_bytes / runtime->channels;
1415 info->first = info->channel * size * 8;
1416 info->step = width;
1417 break;
1418 }
1419 default:
1420 snd_BUG();
1421 break;
1422 }
1423 return 0;
1424}
1425
1426/**
1427 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1428 * @substream: the pcm substream instance
1429 * @cmd: ioctl command
1430 * @arg: ioctl argument
1431 *
1432 * Processes the generic ioctl commands for PCM.
1433 * Can be passed as the ioctl callback for PCM ops.
1434 *
1435 * Returns zero if successful, or a negative error code on failure.
1436 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001437int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 unsigned int cmd, void *arg)
1439{
1440 switch (cmd) {
1441 case SNDRV_PCM_IOCTL1_INFO:
1442 return 0;
1443 case SNDRV_PCM_IOCTL1_RESET:
1444 return snd_pcm_lib_ioctl_reset(substream, arg);
1445 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1446 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1447 }
1448 return -ENXIO;
1449}
1450
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001451EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/**
1454 * snd_pcm_period_elapsed - update the pcm status for the next period
1455 * @substream: the pcm substream instance
1456 *
1457 * This function is called from the interrupt handler when the
1458 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001459 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * Even if more than one periods have elapsed since the last call, you
1462 * have to call this only once.
1463 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001464void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai877211f2005-11-17 13:59:38 +01001466 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 unsigned long flags;
1468
1469 snd_assert(substream != NULL, return);
1470 runtime = substream->runtime;
1471 snd_assert(runtime != NULL, return);
1472
1473 if (runtime->transfer_ack_begin)
1474 runtime->transfer_ack_begin(substream);
1475
1476 snd_pcm_stream_lock_irqsave(substream, flags);
1477 if (!snd_pcm_running(substream) ||
1478 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1479 goto _end;
1480
1481 if (substream->timer_running)
1482 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 _end:
1484 snd_pcm_stream_unlock_irqrestore(substream, flags);
1485 if (runtime->transfer_ack_end)
1486 runtime->transfer_ack_end(substream);
1487 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1488}
1489
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001490EXPORT_SYMBOL(snd_pcm_period_elapsed);
1491
Takashi Iwai13075512008-01-08 18:08:14 +01001492/*
1493 * Wait until avail_min data becomes available
1494 * Returns a negative error code if any error occurs during operation.
1495 * The available space is stored on availp. When err = 0 and avail = 0
1496 * on the capture stream, it indicates the stream is in DRAINING state.
1497 */
1498static int wait_for_avail_min(struct snd_pcm_substream *substream,
1499 snd_pcm_uframes_t *availp)
1500{
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1503 wait_queue_t wait;
1504 int err = 0;
1505 snd_pcm_uframes_t avail = 0;
1506 long tout;
1507
1508 init_waitqueue_entry(&wait, current);
1509 add_wait_queue(&runtime->sleep, &wait);
1510 for (;;) {
1511 if (signal_pending(current)) {
1512 err = -ERESTARTSYS;
1513 break;
1514 }
1515 set_current_state(TASK_INTERRUPTIBLE);
1516 snd_pcm_stream_unlock_irq(substream);
1517 tout = schedule_timeout(msecs_to_jiffies(10000));
1518 snd_pcm_stream_lock_irq(substream);
1519 switch (runtime->status->state) {
1520 case SNDRV_PCM_STATE_SUSPENDED:
1521 err = -ESTRPIPE;
1522 goto _endloop;
1523 case SNDRV_PCM_STATE_XRUN:
1524 err = -EPIPE;
1525 goto _endloop;
1526 case SNDRV_PCM_STATE_DRAINING:
1527 if (is_playback)
1528 err = -EPIPE;
1529 else
1530 avail = 0; /* indicate draining */
1531 goto _endloop;
1532 case SNDRV_PCM_STATE_OPEN:
1533 case SNDRV_PCM_STATE_SETUP:
1534 case SNDRV_PCM_STATE_DISCONNECTED:
1535 err = -EBADFD;
1536 goto _endloop;
1537 }
1538 if (!tout) {
1539 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1540 is_playback ? "playback" : "capture");
1541 err = -EIO;
1542 break;
1543 }
1544 if (is_playback)
1545 avail = snd_pcm_playback_avail(runtime);
1546 else
1547 avail = snd_pcm_capture_avail(runtime);
1548 if (avail >= runtime->control->avail_min)
1549 break;
1550 }
1551 _endloop:
1552 remove_wait_queue(&runtime->sleep, &wait);
1553 *availp = avail;
1554 return err;
1555}
1556
Takashi Iwai877211f2005-11-17 13:59:38 +01001557static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 unsigned int hwoff,
1559 unsigned long data, unsigned int off,
1560 snd_pcm_uframes_t frames)
1561{
Takashi Iwai877211f2005-11-17 13:59:38 +01001562 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int err;
1564 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1565 if (substream->ops->copy) {
1566 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1567 return err;
1568 } else {
1569 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1570 snd_assert(runtime->dma_area, return -EFAULT);
1571 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1572 return -EFAULT;
1573 }
1574 return 0;
1575}
1576
Takashi Iwai877211f2005-11-17 13:59:38 +01001577typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 unsigned long data, unsigned int off,
1579 snd_pcm_uframes_t size);
1580
Takashi Iwai877211f2005-11-17 13:59:38 +01001581static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 unsigned long data,
1583 snd_pcm_uframes_t size,
1584 int nonblock,
1585 transfer_f transfer)
1586{
Takashi Iwai877211f2005-11-17 13:59:38 +01001587 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 snd_pcm_uframes_t xfer = 0;
1589 snd_pcm_uframes_t offset = 0;
1590 int err = 0;
1591
1592 if (size == 0)
1593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 snd_pcm_stream_lock_irq(substream);
1596 switch (runtime->status->state) {
1597 case SNDRV_PCM_STATE_PREPARED:
1598 case SNDRV_PCM_STATE_RUNNING:
1599 case SNDRV_PCM_STATE_PAUSED:
1600 break;
1601 case SNDRV_PCM_STATE_XRUN:
1602 err = -EPIPE;
1603 goto _end_unlock;
1604 case SNDRV_PCM_STATE_SUSPENDED:
1605 err = -ESTRPIPE;
1606 goto _end_unlock;
1607 default:
1608 err = -EBADFD;
1609 goto _end_unlock;
1610 }
1611
1612 while (size > 0) {
1613 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1614 snd_pcm_uframes_t avail;
1615 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001616 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 snd_pcm_update_hw_ptr(substream);
1618 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001619 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (nonblock) {
1621 err = -EAGAIN;
1622 goto _end_unlock;
1623 }
Takashi Iwai13075512008-01-08 18:08:14 +01001624 err = wait_for_avail_min(substream, &avail);
1625 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 frames = size > avail ? avail : size;
1629 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1630 if (frames > cont)
1631 frames = cont;
1632 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1633 appl_ptr = runtime->control->appl_ptr;
1634 appl_ofs = appl_ptr % runtime->buffer_size;
1635 snd_pcm_stream_unlock_irq(substream);
1636 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1637 goto _end;
1638 snd_pcm_stream_lock_irq(substream);
1639 switch (runtime->status->state) {
1640 case SNDRV_PCM_STATE_XRUN:
1641 err = -EPIPE;
1642 goto _end_unlock;
1643 case SNDRV_PCM_STATE_SUSPENDED:
1644 err = -ESTRPIPE;
1645 goto _end_unlock;
1646 default:
1647 break;
1648 }
1649 appl_ptr += frames;
1650 if (appl_ptr >= runtime->boundary)
1651 appl_ptr -= runtime->boundary;
1652 runtime->control->appl_ptr = appl_ptr;
1653 if (substream->ops->ack)
1654 substream->ops->ack(substream);
1655
1656 offset += frames;
1657 size -= frames;
1658 xfer += frames;
1659 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1660 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1661 err = snd_pcm_start(substream);
1662 if (err < 0)
1663 goto _end_unlock;
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666 _end_unlock:
1667 snd_pcm_stream_unlock_irq(substream);
1668 _end:
1669 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1670}
1671
Takashi Iwai877211f2005-11-17 13:59:38 +01001672snd_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 -07001673{
Takashi Iwai877211f2005-11-17 13:59:38 +01001674 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int nonblock;
1676
1677 snd_assert(substream != NULL, return -ENXIO);
1678 runtime = substream->runtime;
1679 snd_assert(runtime != NULL, return -ENXIO);
1680 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1681 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1682 return -EBADFD;
1683
Takashi Iwai0df63e42006-04-28 15:13:41 +02001684 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1687 runtime->channels > 1)
1688 return -EINVAL;
1689 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1690 snd_pcm_lib_write_transfer);
1691}
1692
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001693EXPORT_SYMBOL(snd_pcm_lib_write);
1694
Takashi Iwai877211f2005-11-17 13:59:38 +01001695static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 unsigned int hwoff,
1697 unsigned long data, unsigned int off,
1698 snd_pcm_uframes_t frames)
1699{
Takashi Iwai877211f2005-11-17 13:59:38 +01001700 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 int err;
1702 void __user **bufs = (void __user **)data;
1703 int channels = runtime->channels;
1704 int c;
1705 if (substream->ops->copy) {
1706 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1707 for (c = 0; c < channels; ++c, ++bufs) {
1708 if (*bufs == NULL) {
1709 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1710 return err;
1711 } else {
1712 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1713 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1714 return err;
1715 }
1716 }
1717 } else {
1718 /* default transfer behaviour */
1719 size_t dma_csize = runtime->dma_bytes / channels;
1720 snd_assert(runtime->dma_area, return -EFAULT);
1721 for (c = 0; c < channels; ++c, ++bufs) {
1722 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1723 if (*bufs == NULL) {
1724 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1725 } else {
1726 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1727 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1728 return -EFAULT;
1729 }
1730 }
1731 }
1732 return 0;
1733}
1734
Takashi Iwai877211f2005-11-17 13:59:38 +01001735snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 void __user **bufs,
1737 snd_pcm_uframes_t frames)
1738{
Takashi Iwai877211f2005-11-17 13:59:38 +01001739 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int nonblock;
1741
1742 snd_assert(substream != NULL, return -ENXIO);
1743 runtime = substream->runtime;
1744 snd_assert(runtime != NULL, return -ENXIO);
1745 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1746 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1747 return -EBADFD;
1748
Takashi Iwai0df63e42006-04-28 15:13:41 +02001749 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1752 return -EINVAL;
1753 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1754 nonblock, snd_pcm_lib_writev_transfer);
1755}
1756
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001757EXPORT_SYMBOL(snd_pcm_lib_writev);
1758
Takashi Iwai877211f2005-11-17 13:59:38 +01001759static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned int hwoff,
1761 unsigned long data, unsigned int off,
1762 snd_pcm_uframes_t frames)
1763{
Takashi Iwai877211f2005-11-17 13:59:38 +01001764 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int err;
1766 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1767 if (substream->ops->copy) {
1768 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1769 return err;
1770 } else {
1771 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1772 snd_assert(runtime->dma_area, return -EFAULT);
1773 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1774 return -EFAULT;
1775 }
1776 return 0;
1777}
1778
Takashi Iwai877211f2005-11-17 13:59:38 +01001779static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 unsigned long data,
1781 snd_pcm_uframes_t size,
1782 int nonblock,
1783 transfer_f transfer)
1784{
Takashi Iwai877211f2005-11-17 13:59:38 +01001785 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 snd_pcm_uframes_t xfer = 0;
1787 snd_pcm_uframes_t offset = 0;
1788 int err = 0;
1789
1790 if (size == 0)
1791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 snd_pcm_stream_lock_irq(substream);
1794 switch (runtime->status->state) {
1795 case SNDRV_PCM_STATE_PREPARED:
1796 if (size >= runtime->start_threshold) {
1797 err = snd_pcm_start(substream);
1798 if (err < 0)
1799 goto _end_unlock;
1800 }
1801 break;
1802 case SNDRV_PCM_STATE_DRAINING:
1803 case SNDRV_PCM_STATE_RUNNING:
1804 case SNDRV_PCM_STATE_PAUSED:
1805 break;
1806 case SNDRV_PCM_STATE_XRUN:
1807 err = -EPIPE;
1808 goto _end_unlock;
1809 case SNDRV_PCM_STATE_SUSPENDED:
1810 err = -ESTRPIPE;
1811 goto _end_unlock;
1812 default:
1813 err = -EBADFD;
1814 goto _end_unlock;
1815 }
1816
1817 while (size > 0) {
1818 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1819 snd_pcm_uframes_t avail;
1820 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001821 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001824 if (!avail) {
1825 if (runtime->status->state ==
1826 SNDRV_PCM_STATE_DRAINING) {
1827 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto _end_unlock;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (nonblock) {
1831 err = -EAGAIN;
1832 goto _end_unlock;
1833 }
Takashi Iwai13075512008-01-08 18:08:14 +01001834 err = wait_for_avail_min(substream, &avail);
1835 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001837 if (!avail)
1838 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 frames = size > avail ? avail : size;
1841 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1842 if (frames > cont)
1843 frames = cont;
1844 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1845 appl_ptr = runtime->control->appl_ptr;
1846 appl_ofs = appl_ptr % runtime->buffer_size;
1847 snd_pcm_stream_unlock_irq(substream);
1848 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1849 goto _end;
1850 snd_pcm_stream_lock_irq(substream);
1851 switch (runtime->status->state) {
1852 case SNDRV_PCM_STATE_XRUN:
1853 err = -EPIPE;
1854 goto _end_unlock;
1855 case SNDRV_PCM_STATE_SUSPENDED:
1856 err = -ESTRPIPE;
1857 goto _end_unlock;
1858 default:
1859 break;
1860 }
1861 appl_ptr += frames;
1862 if (appl_ptr >= runtime->boundary)
1863 appl_ptr -= runtime->boundary;
1864 runtime->control->appl_ptr = appl_ptr;
1865 if (substream->ops->ack)
1866 substream->ops->ack(substream);
1867
1868 offset += frames;
1869 size -= frames;
1870 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 _end_unlock:
1873 snd_pcm_stream_unlock_irq(substream);
1874 _end:
1875 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1876}
1877
Takashi Iwai877211f2005-11-17 13:59:38 +01001878snd_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 -07001879{
Takashi Iwai877211f2005-11-17 13:59:38 +01001880 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int nonblock;
1882
1883 snd_assert(substream != NULL, return -ENXIO);
1884 runtime = substream->runtime;
1885 snd_assert(runtime != NULL, return -ENXIO);
1886 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1887 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1888 return -EBADFD;
1889
Takashi Iwai0df63e42006-04-28 15:13:41 +02001890 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1892 return -EINVAL;
1893 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1894}
1895
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001896EXPORT_SYMBOL(snd_pcm_lib_read);
1897
Takashi Iwai877211f2005-11-17 13:59:38 +01001898static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 unsigned int hwoff,
1900 unsigned long data, unsigned int off,
1901 snd_pcm_uframes_t frames)
1902{
Takashi Iwai877211f2005-11-17 13:59:38 +01001903 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 int err;
1905 void __user **bufs = (void __user **)data;
1906 int channels = runtime->channels;
1907 int c;
1908 if (substream->ops->copy) {
1909 for (c = 0; c < channels; ++c, ++bufs) {
1910 char __user *buf;
1911 if (*bufs == NULL)
1912 continue;
1913 buf = *bufs + samples_to_bytes(runtime, off);
1914 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1915 return err;
1916 }
1917 } else {
1918 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1919 snd_assert(runtime->dma_area, return -EFAULT);
1920 for (c = 0; c < channels; ++c, ++bufs) {
1921 char *hwbuf;
1922 char __user *buf;
1923 if (*bufs == NULL)
1924 continue;
1925
1926 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1927 buf = *bufs + samples_to_bytes(runtime, off);
1928 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1929 return -EFAULT;
1930 }
1931 }
1932 return 0;
1933}
1934
Takashi Iwai877211f2005-11-17 13:59:38 +01001935snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 void __user **bufs,
1937 snd_pcm_uframes_t frames)
1938{
Takashi Iwai877211f2005-11-17 13:59:38 +01001939 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int nonblock;
1941
1942 snd_assert(substream != NULL, return -ENXIO);
1943 runtime = substream->runtime;
1944 snd_assert(runtime != NULL, return -ENXIO);
1945 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1946 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1947 return -EBADFD;
1948
Takashi Iwai0df63e42006-04-28 15:13:41 +02001949 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1951 return -EINVAL;
1952 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955EXPORT_SYMBOL(snd_pcm_lib_readv);