blob: 63d088f2265f5c33ce8a26d707ff220bd61c2a1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/time.h>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/info.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/timer.h>
31
32/*
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
38 *
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
40 */
Takashi Iwai877211f2005-11-17 13:59:38 +010041void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai877211f2005-11-17 13:59:38 +010043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 snd_pcm_uframes_t frames, ofs, transfer;
45
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
50 if (n < 0)
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
54 else
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
57 }
Takashi Iwai235475c2005-12-07 15:28:07 +010058 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
62 return;
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
66 } else {
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
72 runtime->boundary;
73 } else {
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020081 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020083 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 frames = runtime->buffer_size - runtime->silence_filled;
87 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020088 if (snd_BUG_ON(frames > runtime->buffer_size))
89 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (frames == 0)
91 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020092 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 while (frames > 0) {
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
98 int err;
99 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200100 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } else {
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
104 }
105 } else {
106 unsigned int c;
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
110 int err;
111 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200112 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 } else {
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
119 }
120 }
121 }
122 runtime->silence_filled += transfer;
123 frames -= transfer;
124 ofs = 0;
125 }
126}
127
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100128#ifdef CONFIG_SND_PCM_XRUN_DEBUG
129#define xrun_debug(substream) ((substream)->pstr->xrun_debug)
130#else
131#define xrun_debug(substream) 0
132#endif
133
134#define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
136 dump_stack(); \
137 } while (0)
138
Takashi Iwai877211f2005-11-17 13:59:38 +0100139static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100142 if (xrun_debug(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100147 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Takashi Iwai98204642009-03-19 09:59:21 +0100151static snd_pcm_uframes_t
152snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 snd_pcm_uframes_t pos;
156
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
168 }
169 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 pos -= pos % runtime->min_align;
172 return pos;
173}
174
Takashi Iwai98204642009-03-19 09:59:21 +0100175static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 snd_pcm_uframes_t avail;
179
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
182 else
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
189 else
190 xrun(substream);
191 return -EPIPE;
192 }
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
195 return 0;
196}
197
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100198#define hw_ptr_error(substream, fmt, args...) \
199 do { \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100202 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100203 } \
204 dump_stack_on_xrun(substream); \
205 } \
206 } while (0)
207
Takashi Iwai98204642009-03-19 09:59:21 +0100208static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Takashi Iwai877211f2005-11-17 13:59:38 +0100210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 snd_pcm_uframes_t pos;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200212 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
213 snd_pcm_sframes_t hdelta, delta;
214 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200216 old_hw_ptr = runtime->status->hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
218 if (pos == SNDRV_PCM_POS_XRUN) {
219 xrun(substream);
220 return -EPIPE;
221 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100222 hw_base = runtime->hw_ptr_base;
223 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100225 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100226 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100227 hw_ptr_interrupt -= runtime->boundary;
228 if (hw_base < runtime->boundary / 2)
229 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100230 delta = new_hw_ptr - hw_ptr_interrupt;
231 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100232 if (delta < 0) {
233 delta += runtime->buffer_size;
234 if (delta < 0) {
235 hw_ptr_error(substream,
236 "Unexpected hw_pointer value "
237 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
238 substream->stream, (long)pos,
239 (long)hw_ptr_interrupt);
240 /* rebase to interrupt position */
241 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100242 /* align hw_base to buffer_size */
243 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100244 delta = 0;
245 } else {
246 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100247 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100248 hw_base = 0;
249 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200252 hdelta = new_hw_ptr - old_hw_ptr;
253 jdelta = jiffies - runtime->hw_ptr_jiffies;
254 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
255 delta = jdelta /
256 (((runtime->period_size * HZ) / runtime->rate)
257 + HZ/100);
258 hw_ptr_error(substream,
259 "hw_ptr skipping! [Q] "
260 "(pos=%ld, delta=%ld, period=%ld, "
261 "jdelta=%lu/%lu/%lu)\n",
262 (long)pos, (long)hdelta,
263 (long)runtime->period_size, jdelta,
264 ((hdelta * HZ) / runtime->rate), delta);
265 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
266 runtime->period_size * delta;
267 if (hw_ptr_interrupt >= runtime->boundary)
268 hw_ptr_interrupt -= runtime->boundary;
269 /* rebase to interrupt position */
270 hw_base = new_hw_ptr = hw_ptr_interrupt;
271 /* align hw_base to buffer_size */
272 hw_base -= hw_base % runtime->buffer_size;
273 delta = 0;
274 }
275 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100276 hw_ptr_error(substream,
277 "Lost interrupts? "
278 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
279 substream->stream, (long)delta,
280 (long)hw_ptr_interrupt);
281 /* rebase hw_ptr_interrupt */
282 hw_ptr_interrupt =
283 new_hw_ptr - new_hw_ptr % runtime->period_size;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
286 runtime->silence_size > 0)
287 snd_pcm_playback_silence(substream, new_hw_ptr);
288
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100289 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200291 runtime->hw_ptr_jiffies = jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100292 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return snd_pcm_update_hw_ptr_post(substream, runtime);
295}
296
297/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100298int snd_pcm_update_hw_ptr(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 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100302 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200304 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 old_hw_ptr = runtime->status->hw_ptr;
307 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
308 if (pos == SNDRV_PCM_POS_XRUN) {
309 xrun(substream);
310 return -EPIPE;
311 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100312 hw_base = runtime->hw_ptr_base;
313 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100315 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200316 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100317 if (delta < 0) {
318 delta += runtime->buffer_size;
319 if (delta < 0) {
320 hw_ptr_error(substream,
321 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200322 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100323 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200324 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100327 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100328 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100329 hw_base = 0;
330 new_hw_ptr = hw_base + pos;
331 }
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200332 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100333 hw_ptr_error(substream,
334 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200335 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100336 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200337 (long)runtime->period_size, jdelta,
338 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
342 runtime->silence_size > 0)
343 snd_pcm_playback_silence(substream, new_hw_ptr);
344
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100345 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200347 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return snd_pcm_update_hw_ptr_post(substream, runtime);
350}
351
352/**
353 * snd_pcm_set_ops - set the PCM operators
354 * @pcm: the pcm instance
355 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
356 * @ops: the operator table
357 *
358 * Sets the given PCM operators to the pcm instance.
359 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100360void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Takashi Iwai877211f2005-11-17 13:59:38 +0100362 struct snd_pcm_str *stream = &pcm->streams[direction];
363 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 for (substream = stream->substream; substream != NULL; substream = substream->next)
366 substream->ops = ops;
367}
368
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200369EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/**
372 * snd_pcm_sync - set the PCM sync id
373 * @substream: the pcm substream
374 *
375 * Sets the PCM sync identifier for the card.
376 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100377void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Takashi Iwai877211f2005-11-17 13:59:38 +0100379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 runtime->sync.id32[0] = substream->pcm->card->number;
382 runtime->sync.id32[1] = -1;
383 runtime->sync.id32[2] = -1;
384 runtime->sync.id32[3] = -1;
385}
386
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200387EXPORT_SYMBOL(snd_pcm_set_sync);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Standard ioctl routine
391 */
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static inline unsigned int div32(unsigned int a, unsigned int b,
394 unsigned int *r)
395{
396 if (b == 0) {
397 *r = 0;
398 return UINT_MAX;
399 }
400 *r = a % b;
401 return a / b;
402}
403
404static inline unsigned int div_down(unsigned int a, unsigned int b)
405{
406 if (b == 0)
407 return UINT_MAX;
408 return a / b;
409}
410
411static inline unsigned int div_up(unsigned int a, unsigned int b)
412{
413 unsigned int r;
414 unsigned int q;
415 if (b == 0)
416 return UINT_MAX;
417 q = div32(a, b, &r);
418 if (r)
419 ++q;
420 return q;
421}
422
423static inline unsigned int mul(unsigned int a, unsigned int b)
424{
425 if (a == 0)
426 return 0;
427 if (div_down(UINT_MAX, a) < b)
428 return UINT_MAX;
429 return a * b;
430}
431
432static inline unsigned int muldiv32(unsigned int a, unsigned int b,
433 unsigned int c, unsigned int *r)
434{
435 u_int64_t n = (u_int64_t) a * b;
436 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200437 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *r = 0;
439 return UINT_MAX;
440 }
441 div64_32(&n, c, r);
442 if (n >= UINT_MAX) {
443 *r = 0;
444 return UINT_MAX;
445 }
446 return n;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
450 * snd_interval_refine - refine the interval value of configurator
451 * @i: the interval value to refine
452 * @v: the interval value to refer to
453 *
454 * Refines the interval value with the reference value.
455 * The interval is changed to the range satisfying both intervals.
456 * The interval status (min, max, integer, etc.) are evaluated.
457 *
458 * Returns non-zero if the value is changed, zero if not changed.
459 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100460int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200463 if (snd_BUG_ON(snd_interval_empty(i)))
464 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (i->min < v->min) {
466 i->min = v->min;
467 i->openmin = v->openmin;
468 changed = 1;
469 } else if (i->min == v->min && !i->openmin && v->openmin) {
470 i->openmin = 1;
471 changed = 1;
472 }
473 if (i->max > v->max) {
474 i->max = v->max;
475 i->openmax = v->openmax;
476 changed = 1;
477 } else if (i->max == v->max && !i->openmax && v->openmax) {
478 i->openmax = 1;
479 changed = 1;
480 }
481 if (!i->integer && v->integer) {
482 i->integer = 1;
483 changed = 1;
484 }
485 if (i->integer) {
486 if (i->openmin) {
487 i->min++;
488 i->openmin = 0;
489 }
490 if (i->openmax) {
491 i->max--;
492 i->openmax = 0;
493 }
494 } else if (!i->openmin && !i->openmax && i->min == i->max)
495 i->integer = 1;
496 if (snd_interval_checkempty(i)) {
497 snd_interval_none(i);
498 return -EINVAL;
499 }
500 return changed;
501}
502
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200503EXPORT_SYMBOL(snd_interval_refine);
504
Takashi Iwai877211f2005-11-17 13:59:38 +0100505static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200507 if (snd_BUG_ON(snd_interval_empty(i)))
508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (snd_interval_single(i))
510 return 0;
511 i->max = i->min;
512 i->openmax = i->openmin;
513 if (i->openmax)
514 i->max++;
515 return 1;
516}
517
Takashi Iwai877211f2005-11-17 13:59:38 +0100518static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200520 if (snd_BUG_ON(snd_interval_empty(i)))
521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (snd_interval_single(i))
523 return 0;
524 i->min = i->max;
525 i->openmin = i->openmax;
526 if (i->openmin)
527 i->min--;
528 return 1;
529}
530
Takashi Iwai877211f2005-11-17 13:59:38 +0100531void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 if (a->empty || b->empty) {
534 snd_interval_none(c);
535 return;
536 }
537 c->empty = 0;
538 c->min = mul(a->min, b->min);
539 c->openmin = (a->openmin || b->openmin);
540 c->max = mul(a->max, b->max);
541 c->openmax = (a->openmax || b->openmax);
542 c->integer = (a->integer && b->integer);
543}
544
545/**
546 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200547 * @a: dividend
548 * @b: divisor
549 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
551 * c = a / b
552 *
553 * Returns non-zero if the value is changed, zero if not changed.
554 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100555void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 unsigned int r;
558 if (a->empty || b->empty) {
559 snd_interval_none(c);
560 return;
561 }
562 c->empty = 0;
563 c->min = div32(a->min, b->max, &r);
564 c->openmin = (r || a->openmin || b->openmax);
565 if (b->min > 0) {
566 c->max = div32(a->max, b->min, &r);
567 if (r) {
568 c->max++;
569 c->openmax = 1;
570 } else
571 c->openmax = (a->openmax || b->openmin);
572 } else {
573 c->max = UINT_MAX;
574 c->openmax = 0;
575 }
576 c->integer = 0;
577}
578
579/**
580 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200581 * @a: dividend 1
582 * @b: dividend 2
583 * @k: divisor (as integer)
584 * @c: result
585 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * c = a * b / k
587 *
588 * Returns non-zero if the value is changed, zero if not changed.
589 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100590void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
591 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 unsigned int r;
594 if (a->empty || b->empty) {
595 snd_interval_none(c);
596 return;
597 }
598 c->empty = 0;
599 c->min = muldiv32(a->min, b->min, k, &r);
600 c->openmin = (r || a->openmin || b->openmin);
601 c->max = muldiv32(a->max, b->max, k, &r);
602 if (r) {
603 c->max++;
604 c->openmax = 1;
605 } else
606 c->openmax = (a->openmax || b->openmax);
607 c->integer = 0;
608}
609
610/**
611 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200612 * @a: dividend 1
613 * @k: dividend 2 (as integer)
614 * @b: divisor
615 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
617 * c = a * k / b
618 *
619 * Returns non-zero if the value is changed, zero if not changed.
620 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100621void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
622 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 unsigned int r;
625 if (a->empty || b->empty) {
626 snd_interval_none(c);
627 return;
628 }
629 c->empty = 0;
630 c->min = muldiv32(a->min, k, b->max, &r);
631 c->openmin = (r || a->openmin || b->openmax);
632 if (b->min > 0) {
633 c->max = muldiv32(a->max, k, b->min, &r);
634 if (r) {
635 c->max++;
636 c->openmax = 1;
637 } else
638 c->openmax = (a->openmax || b->openmin);
639 } else {
640 c->max = UINT_MAX;
641 c->openmax = 0;
642 }
643 c->integer = 0;
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/* ---- */
647
648
649/**
650 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200651 * @i: interval to refine
652 * @rats_count: number of ratnum_t
653 * @rats: ratnum_t array
654 * @nump: pointer to store the resultant numerator
655 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
657 * Returns non-zero if the value is changed, zero if not changed.
658 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100659int snd_interval_ratnum(struct snd_interval *i,
660 unsigned int rats_count, struct snd_ratnum *rats,
661 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 unsigned int best_num, best_diff, best_den;
664 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100665 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int err;
667
668 best_num = best_den = best_diff = 0;
669 for (k = 0; k < rats_count; ++k) {
670 unsigned int num = rats[k].num;
671 unsigned int den;
672 unsigned int q = i->min;
673 int diff;
674 if (q == 0)
675 q = 1;
676 den = div_down(num, q);
677 if (den < rats[k].den_min)
678 continue;
679 if (den > rats[k].den_max)
680 den = rats[k].den_max;
681 else {
682 unsigned int r;
683 r = (den - rats[k].den_min) % rats[k].den_step;
684 if (r != 0)
685 den -= r;
686 }
687 diff = num - q * den;
688 if (best_num == 0 ||
689 diff * best_den < best_diff * den) {
690 best_diff = diff;
691 best_den = den;
692 best_num = num;
693 }
694 }
695 if (best_den == 0) {
696 i->empty = 1;
697 return -EINVAL;
698 }
699 t.min = div_down(best_num, best_den);
700 t.openmin = !!(best_num % best_den);
701
702 best_num = best_den = best_diff = 0;
703 for (k = 0; k < rats_count; ++k) {
704 unsigned int num = rats[k].num;
705 unsigned int den;
706 unsigned int q = i->max;
707 int diff;
708 if (q == 0) {
709 i->empty = 1;
710 return -EINVAL;
711 }
712 den = div_up(num, q);
713 if (den > rats[k].den_max)
714 continue;
715 if (den < rats[k].den_min)
716 den = rats[k].den_min;
717 else {
718 unsigned int r;
719 r = (den - rats[k].den_min) % rats[k].den_step;
720 if (r != 0)
721 den += rats[k].den_step - r;
722 }
723 diff = q * den - num;
724 if (best_num == 0 ||
725 diff * best_den < best_diff * den) {
726 best_diff = diff;
727 best_den = den;
728 best_num = num;
729 }
730 }
731 if (best_den == 0) {
732 i->empty = 1;
733 return -EINVAL;
734 }
735 t.max = div_up(best_num, best_den);
736 t.openmax = !!(best_num % best_den);
737 t.integer = 0;
738 err = snd_interval_refine(i, &t);
739 if (err < 0)
740 return err;
741
742 if (snd_interval_single(i)) {
743 if (nump)
744 *nump = best_num;
745 if (denp)
746 *denp = best_den;
747 }
748 return err;
749}
750
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200751EXPORT_SYMBOL(snd_interval_ratnum);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/**
754 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200755 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100756 * @rats_count: number of struct ratden
757 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200758 * @nump: pointer to store the resultant numerator
759 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
761 * Returns non-zero if the value is changed, zero if not changed.
762 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100763static int snd_interval_ratden(struct snd_interval *i,
764 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int *nump, unsigned int *denp)
766{
767 unsigned int best_num, best_diff, best_den;
768 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100769 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int err;
771
772 best_num = best_den = best_diff = 0;
773 for (k = 0; k < rats_count; ++k) {
774 unsigned int num;
775 unsigned int den = rats[k].den;
776 unsigned int q = i->min;
777 int diff;
778 num = mul(q, den);
779 if (num > rats[k].num_max)
780 continue;
781 if (num < rats[k].num_min)
782 num = rats[k].num_max;
783 else {
784 unsigned int r;
785 r = (num - rats[k].num_min) % rats[k].num_step;
786 if (r != 0)
787 num += rats[k].num_step - r;
788 }
789 diff = num - q * den;
790 if (best_num == 0 ||
791 diff * best_den < best_diff * den) {
792 best_diff = diff;
793 best_den = den;
794 best_num = num;
795 }
796 }
797 if (best_den == 0) {
798 i->empty = 1;
799 return -EINVAL;
800 }
801 t.min = div_down(best_num, best_den);
802 t.openmin = !!(best_num % best_den);
803
804 best_num = best_den = best_diff = 0;
805 for (k = 0; k < rats_count; ++k) {
806 unsigned int num;
807 unsigned int den = rats[k].den;
808 unsigned int q = i->max;
809 int diff;
810 num = mul(q, den);
811 if (num < rats[k].num_min)
812 continue;
813 if (num > rats[k].num_max)
814 num = rats[k].num_max;
815 else {
816 unsigned int r;
817 r = (num - rats[k].num_min) % rats[k].num_step;
818 if (r != 0)
819 num -= r;
820 }
821 diff = q * den - num;
822 if (best_num == 0 ||
823 diff * best_den < best_diff * den) {
824 best_diff = diff;
825 best_den = den;
826 best_num = num;
827 }
828 }
829 if (best_den == 0) {
830 i->empty = 1;
831 return -EINVAL;
832 }
833 t.max = div_up(best_num, best_den);
834 t.openmax = !!(best_num % best_den);
835 t.integer = 0;
836 err = snd_interval_refine(i, &t);
837 if (err < 0)
838 return err;
839
840 if (snd_interval_single(i)) {
841 if (nump)
842 *nump = best_num;
843 if (denp)
844 *denp = best_den;
845 }
846 return err;
847}
848
849/**
850 * snd_interval_list - refine the interval value from the list
851 * @i: the interval value to refine
852 * @count: the number of elements in the list
853 * @list: the value list
854 * @mask: the bit-mask to evaluate
855 *
856 * Refines the interval value from the list.
857 * When mask is non-zero, only the elements corresponding to bit 1 are
858 * evaluated.
859 *
860 * Returns non-zero if the value is changed, zero if not changed.
861 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100862int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 unsigned int k;
865 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100866
867 if (!count) {
868 i->empty = 1;
869 return -EINVAL;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 for (k = 0; k < count; k++) {
872 if (mask && !(mask & (1 << k)))
873 continue;
874 if (i->min == list[k] && !i->openmin)
875 goto _l1;
876 if (i->min < list[k]) {
877 i->min = list[k];
878 i->openmin = 0;
879 changed = 1;
880 goto _l1;
881 }
882 }
883 i->empty = 1;
884 return -EINVAL;
885 _l1:
886 for (k = count; k-- > 0;) {
887 if (mask && !(mask & (1 << k)))
888 continue;
889 if (i->max == list[k] && !i->openmax)
890 goto _l2;
891 if (i->max > list[k]) {
892 i->max = list[k];
893 i->openmax = 0;
894 changed = 1;
895 goto _l2;
896 }
897 }
898 i->empty = 1;
899 return -EINVAL;
900 _l2:
901 if (snd_interval_checkempty(i)) {
902 i->empty = 1;
903 return -EINVAL;
904 }
905 return changed;
906}
907
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200908EXPORT_SYMBOL(snd_interval_list);
909
Takashi Iwai877211f2005-11-17 13:59:38 +0100910static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 unsigned int n;
913 int changed = 0;
914 n = (i->min - min) % step;
915 if (n != 0 || i->openmin) {
916 i->min += step - n;
917 changed = 1;
918 }
919 n = (i->max - min) % step;
920 if (n != 0 || i->openmax) {
921 i->max -= n;
922 changed = 1;
923 }
924 if (snd_interval_checkempty(i)) {
925 i->empty = 1;
926 return -EINVAL;
927 }
928 return changed;
929}
930
931/* Info constraints helpers */
932
933/**
934 * snd_pcm_hw_rule_add - add the hw-constraint rule
935 * @runtime: the pcm runtime instance
936 * @cond: condition bits
937 * @var: the variable to evaluate
938 * @func: the evaluation function
939 * @private: the private data pointer passed to function
940 * @dep: the dependent variables
941 *
942 * Returns zero if successful, or a negative error code on failure.
943 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100944int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int var,
946 snd_pcm_hw_rule_func_t func, void *private,
947 int dep, ...)
948{
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
950 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned int k;
952 va_list args;
953 va_start(args, dep);
954 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100955 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned int new_rules = constrs->rules_all + 16;
957 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
958 if (!new)
959 return -ENOMEM;
960 if (constrs->rules) {
961 memcpy(new, constrs->rules,
962 constrs->rules_num * sizeof(*c));
963 kfree(constrs->rules);
964 }
965 constrs->rules = new;
966 constrs->rules_all = new_rules;
967 }
968 c = &constrs->rules[constrs->rules_num];
969 c->cond = cond;
970 c->func = func;
971 c->var = var;
972 c->private = private;
973 k = 0;
974 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200975 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 c->deps[k++] = dep;
978 if (dep < 0)
979 break;
980 dep = va_arg(args, int);
981 }
982 constrs->rules_num++;
983 va_end(args);
984 return 0;
985}
986
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200987EXPORT_SYMBOL(snd_pcm_hw_rule_add);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700990 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200991 * @runtime: PCM runtime instance
992 * @var: hw_params variable to apply the mask
993 * @mask: the bitmap mask
994 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700995 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100997int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 u_int32_t mask)
999{
Takashi Iwai877211f2005-11-17 13:59:38 +01001000 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1001 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 *maskp->bits &= mask;
1003 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1004 if (*maskp->bits == 0)
1005 return -EINVAL;
1006 return 0;
1007}
1008
1009/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001010 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001011 * @runtime: PCM runtime instance
1012 * @var: hw_params variable to apply the mask
1013 * @mask: the 64bit bitmap mask
1014 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001015 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001017int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 u_int64_t mask)
1019{
Takashi Iwai877211f2005-11-17 13:59:38 +01001020 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1021 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 maskp->bits[0] &= (u_int32_t)mask;
1023 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1024 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1025 if (! maskp->bits[0] && ! maskp->bits[1])
1026 return -EINVAL;
1027 return 0;
1028}
1029
1030/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001031 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001032 * @runtime: PCM runtime instance
1033 * @var: hw_params variable to apply the integer constraint
1034 *
1035 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001037int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Takashi Iwai877211f2005-11-17 13:59:38 +01001039 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return snd_interval_setinteger(constrs_interval(constrs, var));
1041}
1042
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001043EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001046 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001047 * @runtime: PCM runtime instance
1048 * @var: hw_params variable to apply the range
1049 * @min: the minimal value
1050 * @max: the maximal value
1051 *
1052 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001054int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unsigned int min, unsigned int max)
1056{
Takashi Iwai877211f2005-11-17 13:59:38 +01001057 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1058 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 t.min = min;
1060 t.max = max;
1061 t.openmin = t.openmax = 0;
1062 t.integer = 0;
1063 return snd_interval_refine(constrs_interval(constrs, var), &t);
1064}
1065
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001066EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1067
Takashi Iwai877211f2005-11-17 13:59:38 +01001068static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1069 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1073}
1074
1075
1076/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001077 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001078 * @runtime: PCM runtime instance
1079 * @cond: condition bits
1080 * @var: hw_params variable to apply the list constraint
1081 * @l: list
1082 *
1083 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001085int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned int cond,
1087 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001088 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 return snd_pcm_hw_rule_add(runtime, cond, var,
1091 snd_pcm_hw_rule_list, l,
1092 var, -1);
1093}
1094
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001095EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1096
Takashi Iwai877211f2005-11-17 13:59:38 +01001097static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1098 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Takashi Iwai877211f2005-11-17 13:59:38 +01001100 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned int num = 0, den = 0;
1102 int err;
1103 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1104 r->nrats, r->rats, &num, &den);
1105 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1106 params->rate_num = num;
1107 params->rate_den = den;
1108 }
1109 return err;
1110}
1111
1112/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001113 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001114 * @runtime: PCM runtime instance
1115 * @cond: condition bits
1116 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001117 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001119int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 unsigned int cond,
1121 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001122 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 return snd_pcm_hw_rule_add(runtime, cond, var,
1125 snd_pcm_hw_rule_ratnums, r,
1126 var, -1);
1127}
1128
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001129EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1130
Takashi Iwai877211f2005-11-17 13:59:38 +01001131static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1132 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai877211f2005-11-17 13:59:38 +01001134 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned int num = 0, den = 0;
1136 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1137 r->nrats, r->rats, &num, &den);
1138 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1139 params->rate_num = num;
1140 params->rate_den = den;
1141 }
1142 return err;
1143}
1144
1145/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001146 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001150 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001152int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 unsigned int cond,
1154 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001155 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 return snd_pcm_hw_rule_add(runtime, cond, var,
1158 snd_pcm_hw_rule_ratdens, r,
1159 var, -1);
1160}
1161
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001162EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1163
Takashi Iwai877211f2005-11-17 13:59:38 +01001164static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1165 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 unsigned int l = (unsigned long) rule->private;
1168 int width = l & 0xffff;
1169 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001170 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (snd_interval_single(i) && snd_interval_value(i) == width)
1172 params->msbits = msbits;
1173 return 0;
1174}
1175
1176/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001177 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001178 * @runtime: PCM runtime instance
1179 * @cond: condition bits
1180 * @width: sample bits width
1181 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001183int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unsigned int cond,
1185 unsigned int width,
1186 unsigned int msbits)
1187{
1188 unsigned long l = (msbits << 16) | width;
1189 return snd_pcm_hw_rule_add(runtime, cond, -1,
1190 snd_pcm_hw_rule_msbits,
1191 (void*) l,
1192 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1193}
1194
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001195EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1196
Takashi Iwai877211f2005-11-17 13:59:38 +01001197static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1198 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 unsigned long step = (unsigned long) rule->private;
1201 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1202}
1203
1204/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001205 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001206 * @runtime: PCM runtime instance
1207 * @cond: condition bits
1208 * @var: hw_params variable to apply the step constraint
1209 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001211int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unsigned int cond,
1213 snd_pcm_hw_param_t var,
1214 unsigned long step)
1215{
1216 return snd_pcm_hw_rule_add(runtime, cond, var,
1217 snd_pcm_hw_rule_step, (void *) step,
1218 var, -1);
1219}
1220
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001221EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1222
Takashi Iwai877211f2005-11-17 13:59:38 +01001223static 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 -07001224{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001225 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1227 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1228 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1229 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1230 };
1231 return snd_interval_list(hw_param_interval(params, rule->var),
1232 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1233}
1234
1235/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001236 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001237 * @runtime: PCM runtime instance
1238 * @cond: condition bits
1239 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001241int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned int cond,
1243 snd_pcm_hw_param_t var)
1244{
1245 return snd_pcm_hw_rule_add(runtime, cond, var,
1246 snd_pcm_hw_rule_pow2, NULL,
1247 var, -1);
1248}
1249
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001250EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1251
Takashi Iwai877211f2005-11-17 13:59:38 +01001252static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001253 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 if (hw_is_mask(var)) {
1256 snd_mask_any(hw_param_mask(params, var));
1257 params->cmask |= 1 << var;
1258 params->rmask |= 1 << var;
1259 return;
1260 }
1261 if (hw_is_interval(var)) {
1262 snd_interval_any(hw_param_interval(params, var));
1263 params->cmask |= 1 << var;
1264 params->rmask |= 1 << var;
1265 return;
1266 }
1267 snd_BUG();
1268}
1269
Takashi Iwai877211f2005-11-17 13:59:38 +01001270void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 unsigned int k;
1273 memset(params, 0, sizeof(*params));
1274 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1275 _snd_pcm_hw_param_any(params, k);
1276 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1277 _snd_pcm_hw_param_any(params, k);
1278 params->info = ~0U;
1279}
1280
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001281EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001284 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001285 * @params: the hw_params instance
1286 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001287 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001289 * Return the value for field @var if it's fixed in configuration space
1290 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001292int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1293 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001296 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (!snd_mask_single(mask))
1298 return -EINVAL;
1299 if (dir)
1300 *dir = 0;
1301 return snd_mask_value(mask);
1302 }
1303 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001304 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!snd_interval_single(i))
1306 return -EINVAL;
1307 if (dir)
1308 *dir = i->openmin;
1309 return snd_interval_value(i);
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EINVAL;
1312}
1313
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001314EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Takashi Iwai877211f2005-11-17 13:59:38 +01001316void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 snd_pcm_hw_param_t var)
1318{
1319 if (hw_is_mask(var)) {
1320 snd_mask_none(hw_param_mask(params, var));
1321 params->cmask |= 1 << var;
1322 params->rmask |= 1 << var;
1323 } else if (hw_is_interval(var)) {
1324 snd_interval_none(hw_param_interval(params, var));
1325 params->cmask |= 1 << var;
1326 params->rmask |= 1 << var;
1327 } else {
1328 snd_BUG();
1329 }
1330}
1331
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001332EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Takashi Iwai877211f2005-11-17 13:59:38 +01001334static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001335 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int changed;
1338 if (hw_is_mask(var))
1339 changed = snd_mask_refine_first(hw_param_mask(params, var));
1340 else if (hw_is_interval(var))
1341 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001342 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (changed) {
1345 params->cmask |= 1 << var;
1346 params->rmask |= 1 << var;
1347 }
1348 return changed;
1349}
1350
1351
1352/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001353 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001354 * @pcm: PCM instance
1355 * @params: the hw_params instance
1356 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001357 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001359 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * values > minimum. Reduce configuration space accordingly.
1361 * Return the minimum.
1362 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001363int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1364 struct snd_pcm_hw_params *params,
1365 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 int changed = _snd_pcm_hw_param_first(params, var);
1368 if (changed < 0)
1369 return changed;
1370 if (params->rmask) {
1371 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001372 if (snd_BUG_ON(err < 0))
1373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 return snd_pcm_hw_param_value(params, var, dir);
1376}
1377
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001378EXPORT_SYMBOL(snd_pcm_hw_param_first);
1379
Takashi Iwai877211f2005-11-17 13:59:38 +01001380static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001381 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int changed;
1384 if (hw_is_mask(var))
1385 changed = snd_mask_refine_last(hw_param_mask(params, var));
1386 else if (hw_is_interval(var))
1387 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001388 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (changed) {
1391 params->cmask |= 1 << var;
1392 params->rmask |= 1 << var;
1393 }
1394 return changed;
1395}
1396
1397
1398/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001399 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001400 * @pcm: PCM instance
1401 * @params: the hw_params instance
1402 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001403 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001405 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * values < maximum. Reduce configuration space accordingly.
1407 * Return the maximum.
1408 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001409int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1410 struct snd_pcm_hw_params *params,
1411 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 int changed = _snd_pcm_hw_param_last(params, var);
1414 if (changed < 0)
1415 return changed;
1416 if (params->rmask) {
1417 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001418 if (snd_BUG_ON(err < 0))
1419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 return snd_pcm_hw_param_value(params, var, dir);
1422}
1423
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001424EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001427 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001428 * @pcm: PCM instance
1429 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001431 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * The configuration chosen is that obtained fixing in this order:
1433 * first access, first format, first subformat, min channels,
1434 * min rate, min period time, max buffer size, min tick time
1435 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001436int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1437 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001439 static int vars[] = {
1440 SNDRV_PCM_HW_PARAM_ACCESS,
1441 SNDRV_PCM_HW_PARAM_FORMAT,
1442 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1443 SNDRV_PCM_HW_PARAM_CHANNELS,
1444 SNDRV_PCM_HW_PARAM_RATE,
1445 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1446 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1447 SNDRV_PCM_HW_PARAM_TICK_TIME,
1448 -1
1449 };
1450 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001452 for (v = vars; *v != -1; v++) {
1453 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1454 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1455 else
1456 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001457 if (snd_BUG_ON(err < 0))
1458 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461}
1462
Takashi Iwai877211f2005-11-17 13:59:38 +01001463static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 void *arg)
1465{
Takashi Iwai877211f2005-11-17 13:59:38 +01001466 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 unsigned long flags;
1468 snd_pcm_stream_lock_irqsave(substream, flags);
1469 if (snd_pcm_running(substream) &&
1470 snd_pcm_update_hw_ptr(substream) >= 0)
1471 runtime->status->hw_ptr %= runtime->buffer_size;
1472 else
1473 runtime->status->hw_ptr = 0;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +02001474 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 snd_pcm_stream_unlock_irqrestore(substream, flags);
1476 return 0;
1477}
1478
Takashi Iwai877211f2005-11-17 13:59:38 +01001479static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 void *arg)
1481{
Takashi Iwai877211f2005-11-17 13:59:38 +01001482 struct snd_pcm_channel_info *info = arg;
1483 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int width;
1485 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1486 info->offset = -1;
1487 return 0;
1488 }
1489 width = snd_pcm_format_physical_width(runtime->format);
1490 if (width < 0)
1491 return width;
1492 info->offset = 0;
1493 switch (runtime->access) {
1494 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1495 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1496 info->first = info->channel * width;
1497 info->step = runtime->channels * width;
1498 break;
1499 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1500 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1501 {
1502 size_t size = runtime->dma_bytes / runtime->channels;
1503 info->first = info->channel * size * 8;
1504 info->step = width;
1505 break;
1506 }
1507 default:
1508 snd_BUG();
1509 break;
1510 }
1511 return 0;
1512}
1513
1514/**
1515 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1516 * @substream: the pcm substream instance
1517 * @cmd: ioctl command
1518 * @arg: ioctl argument
1519 *
1520 * Processes the generic ioctl commands for PCM.
1521 * Can be passed as the ioctl callback for PCM ops.
1522 *
1523 * Returns zero if successful, or a negative error code on failure.
1524 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001525int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 unsigned int cmd, void *arg)
1527{
1528 switch (cmd) {
1529 case SNDRV_PCM_IOCTL1_INFO:
1530 return 0;
1531 case SNDRV_PCM_IOCTL1_RESET:
1532 return snd_pcm_lib_ioctl_reset(substream, arg);
1533 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1534 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1535 }
1536 return -ENXIO;
1537}
1538
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001539EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/**
1542 * snd_pcm_period_elapsed - update the pcm status for the next period
1543 * @substream: the pcm substream instance
1544 *
1545 * This function is called from the interrupt handler when the
1546 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001547 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 *
1549 * Even if more than one periods have elapsed since the last call, you
1550 * have to call this only once.
1551 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001552void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai877211f2005-11-17 13:59:38 +01001554 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 unsigned long flags;
1556
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001557 if (PCM_RUNTIME_CHECK(substream))
1558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (runtime->transfer_ack_begin)
1562 runtime->transfer_ack_begin(substream);
1563
1564 snd_pcm_stream_lock_irqsave(substream, flags);
1565 if (!snd_pcm_running(substream) ||
1566 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1567 goto _end;
1568
1569 if (substream->timer_running)
1570 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 _end:
1572 snd_pcm_stream_unlock_irqrestore(substream, flags);
1573 if (runtime->transfer_ack_end)
1574 runtime->transfer_ack_end(substream);
1575 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1576}
1577
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001578EXPORT_SYMBOL(snd_pcm_period_elapsed);
1579
Takashi Iwai13075512008-01-08 18:08:14 +01001580/*
1581 * Wait until avail_min data becomes available
1582 * Returns a negative error code if any error occurs during operation.
1583 * The available space is stored on availp. When err = 0 and avail = 0
1584 * on the capture stream, it indicates the stream is in DRAINING state.
1585 */
1586static int wait_for_avail_min(struct snd_pcm_substream *substream,
1587 snd_pcm_uframes_t *availp)
1588{
1589 struct snd_pcm_runtime *runtime = substream->runtime;
1590 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1591 wait_queue_t wait;
1592 int err = 0;
1593 snd_pcm_uframes_t avail = 0;
1594 long tout;
1595
1596 init_waitqueue_entry(&wait, current);
1597 add_wait_queue(&runtime->sleep, &wait);
1598 for (;;) {
1599 if (signal_pending(current)) {
1600 err = -ERESTARTSYS;
1601 break;
1602 }
1603 set_current_state(TASK_INTERRUPTIBLE);
1604 snd_pcm_stream_unlock_irq(substream);
1605 tout = schedule_timeout(msecs_to_jiffies(10000));
1606 snd_pcm_stream_lock_irq(substream);
1607 switch (runtime->status->state) {
1608 case SNDRV_PCM_STATE_SUSPENDED:
1609 err = -ESTRPIPE;
1610 goto _endloop;
1611 case SNDRV_PCM_STATE_XRUN:
1612 err = -EPIPE;
1613 goto _endloop;
1614 case SNDRV_PCM_STATE_DRAINING:
1615 if (is_playback)
1616 err = -EPIPE;
1617 else
1618 avail = 0; /* indicate draining */
1619 goto _endloop;
1620 case SNDRV_PCM_STATE_OPEN:
1621 case SNDRV_PCM_STATE_SETUP:
1622 case SNDRV_PCM_STATE_DISCONNECTED:
1623 err = -EBADFD;
1624 goto _endloop;
1625 }
1626 if (!tout) {
1627 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1628 is_playback ? "playback" : "capture");
1629 err = -EIO;
1630 break;
1631 }
1632 if (is_playback)
1633 avail = snd_pcm_playback_avail(runtime);
1634 else
1635 avail = snd_pcm_capture_avail(runtime);
1636 if (avail >= runtime->control->avail_min)
1637 break;
1638 }
1639 _endloop:
1640 remove_wait_queue(&runtime->sleep, &wait);
1641 *availp = avail;
1642 return err;
1643}
1644
Takashi Iwai877211f2005-11-17 13:59:38 +01001645static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 unsigned int hwoff,
1647 unsigned long data, unsigned int off,
1648 snd_pcm_uframes_t frames)
1649{
Takashi Iwai877211f2005-11-17 13:59:38 +01001650 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int err;
1652 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1653 if (substream->ops->copy) {
1654 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1655 return err;
1656 } else {
1657 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1659 return -EFAULT;
1660 }
1661 return 0;
1662}
1663
Takashi Iwai877211f2005-11-17 13:59:38 +01001664typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 unsigned long data, unsigned int off,
1666 snd_pcm_uframes_t size);
1667
Takashi Iwai877211f2005-11-17 13:59:38 +01001668static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 unsigned long data,
1670 snd_pcm_uframes_t size,
1671 int nonblock,
1672 transfer_f transfer)
1673{
Takashi Iwai877211f2005-11-17 13:59:38 +01001674 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 snd_pcm_uframes_t xfer = 0;
1676 snd_pcm_uframes_t offset = 0;
1677 int err = 0;
1678
1679 if (size == 0)
1680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 snd_pcm_stream_lock_irq(substream);
1683 switch (runtime->status->state) {
1684 case SNDRV_PCM_STATE_PREPARED:
1685 case SNDRV_PCM_STATE_RUNNING:
1686 case SNDRV_PCM_STATE_PAUSED:
1687 break;
1688 case SNDRV_PCM_STATE_XRUN:
1689 err = -EPIPE;
1690 goto _end_unlock;
1691 case SNDRV_PCM_STATE_SUSPENDED:
1692 err = -ESTRPIPE;
1693 goto _end_unlock;
1694 default:
1695 err = -EBADFD;
1696 goto _end_unlock;
1697 }
1698
1699 while (size > 0) {
1700 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1701 snd_pcm_uframes_t avail;
1702 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001703 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 snd_pcm_update_hw_ptr(substream);
1705 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001706 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (nonblock) {
1708 err = -EAGAIN;
1709 goto _end_unlock;
1710 }
Takashi Iwai13075512008-01-08 18:08:14 +01001711 err = wait_for_avail_min(substream, &avail);
1712 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 frames = size > avail ? avail : size;
1716 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1717 if (frames > cont)
1718 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001719 if (snd_BUG_ON(!frames)) {
1720 snd_pcm_stream_unlock_irq(substream);
1721 return -EINVAL;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 appl_ptr = runtime->control->appl_ptr;
1724 appl_ofs = appl_ptr % runtime->buffer_size;
1725 snd_pcm_stream_unlock_irq(substream);
1726 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1727 goto _end;
1728 snd_pcm_stream_lock_irq(substream);
1729 switch (runtime->status->state) {
1730 case SNDRV_PCM_STATE_XRUN:
1731 err = -EPIPE;
1732 goto _end_unlock;
1733 case SNDRV_PCM_STATE_SUSPENDED:
1734 err = -ESTRPIPE;
1735 goto _end_unlock;
1736 default:
1737 break;
1738 }
1739 appl_ptr += frames;
1740 if (appl_ptr >= runtime->boundary)
1741 appl_ptr -= runtime->boundary;
1742 runtime->control->appl_ptr = appl_ptr;
1743 if (substream->ops->ack)
1744 substream->ops->ack(substream);
1745
1746 offset += frames;
1747 size -= frames;
1748 xfer += frames;
1749 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1750 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1751 err = snd_pcm_start(substream);
1752 if (err < 0)
1753 goto _end_unlock;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 _end_unlock:
1757 snd_pcm_stream_unlock_irq(substream);
1758 _end:
1759 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1760}
1761
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001762/* sanity-check for read/write methods */
1763static int pcm_sanity_check(struct snd_pcm_substream *substream)
1764{
1765 struct snd_pcm_runtime *runtime;
1766 if (PCM_RUNTIME_CHECK(substream))
1767 return -ENXIO;
1768 runtime = substream->runtime;
1769 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1770 return -EINVAL;
1771 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1772 return -EBADFD;
1773 return 0;
1774}
1775
Takashi Iwai877211f2005-11-17 13:59:38 +01001776snd_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 -07001777{
Takashi Iwai877211f2005-11-17 13:59:38 +01001778 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001780 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001782 err = pcm_sanity_check(substream);
1783 if (err < 0)
1784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001786 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1789 runtime->channels > 1)
1790 return -EINVAL;
1791 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1792 snd_pcm_lib_write_transfer);
1793}
1794
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001795EXPORT_SYMBOL(snd_pcm_lib_write);
1796
Takashi Iwai877211f2005-11-17 13:59:38 +01001797static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 unsigned int hwoff,
1799 unsigned long data, unsigned int off,
1800 snd_pcm_uframes_t frames)
1801{
Takashi Iwai877211f2005-11-17 13:59:38 +01001802 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 int err;
1804 void __user **bufs = (void __user **)data;
1805 int channels = runtime->channels;
1806 int c;
1807 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001808 if (snd_BUG_ON(!substream->ops->silence))
1809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 for (c = 0; c < channels; ++c, ++bufs) {
1811 if (*bufs == NULL) {
1812 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1813 return err;
1814 } else {
1815 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1816 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1817 return err;
1818 }
1819 }
1820 } else {
1821 /* default transfer behaviour */
1822 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 for (c = 0; c < channels; ++c, ++bufs) {
1824 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1825 if (*bufs == NULL) {
1826 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1827 } else {
1828 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1829 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1830 return -EFAULT;
1831 }
1832 }
1833 }
1834 return 0;
1835}
1836
Takashi Iwai877211f2005-11-17 13:59:38 +01001837snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 void __user **bufs,
1839 snd_pcm_uframes_t frames)
1840{
Takashi Iwai877211f2005-11-17 13:59:38 +01001841 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001843 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001845 err = pcm_sanity_check(substream);
1846 if (err < 0)
1847 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001849 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1852 return -EINVAL;
1853 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1854 nonblock, snd_pcm_lib_writev_transfer);
1855}
1856
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001857EXPORT_SYMBOL(snd_pcm_lib_writev);
1858
Takashi Iwai877211f2005-11-17 13:59:38 +01001859static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 unsigned int hwoff,
1861 unsigned long data, unsigned int off,
1862 snd_pcm_uframes_t frames)
1863{
Takashi Iwai877211f2005-11-17 13:59:38 +01001864 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 int err;
1866 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1867 if (substream->ops->copy) {
1868 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1869 return err;
1870 } else {
1871 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1873 return -EFAULT;
1874 }
1875 return 0;
1876}
1877
Takashi Iwai877211f2005-11-17 13:59:38 +01001878static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 unsigned long data,
1880 snd_pcm_uframes_t size,
1881 int nonblock,
1882 transfer_f transfer)
1883{
Takashi Iwai877211f2005-11-17 13:59:38 +01001884 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 snd_pcm_uframes_t xfer = 0;
1886 snd_pcm_uframes_t offset = 0;
1887 int err = 0;
1888
1889 if (size == 0)
1890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 snd_pcm_stream_lock_irq(substream);
1893 switch (runtime->status->state) {
1894 case SNDRV_PCM_STATE_PREPARED:
1895 if (size >= runtime->start_threshold) {
1896 err = snd_pcm_start(substream);
1897 if (err < 0)
1898 goto _end_unlock;
1899 }
1900 break;
1901 case SNDRV_PCM_STATE_DRAINING:
1902 case SNDRV_PCM_STATE_RUNNING:
1903 case SNDRV_PCM_STATE_PAUSED:
1904 break;
1905 case SNDRV_PCM_STATE_XRUN:
1906 err = -EPIPE;
1907 goto _end_unlock;
1908 case SNDRV_PCM_STATE_SUSPENDED:
1909 err = -ESTRPIPE;
1910 goto _end_unlock;
1911 default:
1912 err = -EBADFD;
1913 goto _end_unlock;
1914 }
1915
1916 while (size > 0) {
1917 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1918 snd_pcm_uframes_t avail;
1919 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001920 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001923 if (!avail) {
1924 if (runtime->status->state ==
1925 SNDRV_PCM_STATE_DRAINING) {
1926 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto _end_unlock;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (nonblock) {
1930 err = -EAGAIN;
1931 goto _end_unlock;
1932 }
Takashi Iwai13075512008-01-08 18:08:14 +01001933 err = wait_for_avail_min(substream, &avail);
1934 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001936 if (!avail)
1937 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 frames = size > avail ? avail : size;
1940 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1941 if (frames > cont)
1942 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001943 if (snd_BUG_ON(!frames)) {
1944 snd_pcm_stream_unlock_irq(substream);
1945 return -EINVAL;
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 appl_ptr = runtime->control->appl_ptr;
1948 appl_ofs = appl_ptr % runtime->buffer_size;
1949 snd_pcm_stream_unlock_irq(substream);
1950 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1951 goto _end;
1952 snd_pcm_stream_lock_irq(substream);
1953 switch (runtime->status->state) {
1954 case SNDRV_PCM_STATE_XRUN:
1955 err = -EPIPE;
1956 goto _end_unlock;
1957 case SNDRV_PCM_STATE_SUSPENDED:
1958 err = -ESTRPIPE;
1959 goto _end_unlock;
1960 default:
1961 break;
1962 }
1963 appl_ptr += frames;
1964 if (appl_ptr >= runtime->boundary)
1965 appl_ptr -= runtime->boundary;
1966 runtime->control->appl_ptr = appl_ptr;
1967 if (substream->ops->ack)
1968 substream->ops->ack(substream);
1969
1970 offset += frames;
1971 size -= frames;
1972 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 _end_unlock:
1975 snd_pcm_stream_unlock_irq(substream);
1976 _end:
1977 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1978}
1979
Takashi Iwai877211f2005-11-17 13:59:38 +01001980snd_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 -07001981{
Takashi Iwai877211f2005-11-17 13:59:38 +01001982 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001984 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001986 err = pcm_sanity_check(substream);
1987 if (err < 0)
1988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001990 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1992 return -EINVAL;
1993 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1994}
1995
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001996EXPORT_SYMBOL(snd_pcm_lib_read);
1997
Takashi Iwai877211f2005-11-17 13:59:38 +01001998static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 unsigned int hwoff,
2000 unsigned long data, unsigned int off,
2001 snd_pcm_uframes_t frames)
2002{
Takashi Iwai877211f2005-11-17 13:59:38 +01002003 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 int err;
2005 void __user **bufs = (void __user **)data;
2006 int channels = runtime->channels;
2007 int c;
2008 if (substream->ops->copy) {
2009 for (c = 0; c < channels; ++c, ++bufs) {
2010 char __user *buf;
2011 if (*bufs == NULL)
2012 continue;
2013 buf = *bufs + samples_to_bytes(runtime, off);
2014 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2015 return err;
2016 }
2017 } else {
2018 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 for (c = 0; c < channels; ++c, ++bufs) {
2020 char *hwbuf;
2021 char __user *buf;
2022 if (*bufs == NULL)
2023 continue;
2024
2025 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2026 buf = *bufs + samples_to_bytes(runtime, off);
2027 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2028 return -EFAULT;
2029 }
2030 }
2031 return 0;
2032}
2033
Takashi Iwai877211f2005-11-17 13:59:38 +01002034snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 void __user **bufs,
2036 snd_pcm_uframes_t frames)
2037{
Takashi Iwai877211f2005-11-17 13:59:38 +01002038 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002040 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002042 err = pcm_sanity_check(substream);
2043 if (err < 0)
2044 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2047 return -EBADFD;
2048
Takashi Iwai0df63e42006-04-28 15:13:41 +02002049 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2051 return -EINVAL;
2052 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055EXPORT_SYMBOL(snd_pcm_lib_readv);