blob: a2a792c18c40e18412ac7e9e015d22c6035b2958 [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 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200252 /* Skip the jiffies check for hardwares with BATCH flag.
253 * Such hardware usually just increases the position at each IRQ,
254 * thus it can't give any strange position.
255 */
256 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
257 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200258 hdelta = new_hw_ptr - old_hw_ptr;
259 jdelta = jiffies - runtime->hw_ptr_jiffies;
260 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
261 delta = jdelta /
262 (((runtime->period_size * HZ) / runtime->rate)
263 + HZ/100);
264 hw_ptr_error(substream,
265 "hw_ptr skipping! [Q] "
266 "(pos=%ld, delta=%ld, period=%ld, "
267 "jdelta=%lu/%lu/%lu)\n",
268 (long)pos, (long)hdelta,
269 (long)runtime->period_size, jdelta,
270 ((hdelta * HZ) / runtime->rate), delta);
271 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
272 runtime->period_size * delta;
273 if (hw_ptr_interrupt >= runtime->boundary)
274 hw_ptr_interrupt -= runtime->boundary;
275 /* rebase to interrupt position */
276 hw_base = new_hw_ptr = hw_ptr_interrupt;
277 /* align hw_base to buffer_size */
278 hw_base -= hw_base % runtime->buffer_size;
279 delta = 0;
280 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200281 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200282 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100283 hw_ptr_error(substream,
284 "Lost interrupts? "
285 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
286 substream->stream, (long)delta,
287 (long)hw_ptr_interrupt);
288 /* rebase hw_ptr_interrupt */
289 hw_ptr_interrupt =
290 new_hw_ptr - new_hw_ptr % runtime->period_size;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
293 runtime->silence_size > 0)
294 snd_pcm_playback_silence(substream, new_hw_ptr);
295
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100296 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200298 runtime->hw_ptr_jiffies = jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100299 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return snd_pcm_update_hw_ptr_post(substream, runtime);
302}
303
304/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100305int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Takashi Iwai877211f2005-11-17 13:59:38 +0100307 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100309 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200311 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 old_hw_ptr = runtime->status->hw_ptr;
314 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
315 if (pos == SNDRV_PCM_POS_XRUN) {
316 xrun(substream);
317 return -EPIPE;
318 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100319 hw_base = runtime->hw_ptr_base;
320 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100322 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200323 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100324 if (delta < 0) {
325 delta += runtime->buffer_size;
326 if (delta < 0) {
327 hw_ptr_error(substream,
328 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200329 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100330 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200331 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100334 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100335 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100336 hw_base = 0;
337 new_hw_ptr = hw_base + pos;
338 }
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200339 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100340 hw_ptr_error(substream,
341 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200342 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100343 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200344 (long)runtime->period_size, jdelta,
345 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
349 runtime->silence_size > 0)
350 snd_pcm_playback_silence(substream, new_hw_ptr);
351
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100352 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200354 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 return snd_pcm_update_hw_ptr_post(substream, runtime);
357}
358
359/**
360 * snd_pcm_set_ops - set the PCM operators
361 * @pcm: the pcm instance
362 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
363 * @ops: the operator table
364 *
365 * Sets the given PCM operators to the pcm instance.
366 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100367void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Takashi Iwai877211f2005-11-17 13:59:38 +0100369 struct snd_pcm_str *stream = &pcm->streams[direction];
370 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 for (substream = stream->substream; substream != NULL; substream = substream->next)
373 substream->ops = ops;
374}
375
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200376EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378/**
379 * snd_pcm_sync - set the PCM sync id
380 * @substream: the pcm substream
381 *
382 * Sets the PCM sync identifier for the card.
383 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100384void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Takashi Iwai877211f2005-11-17 13:59:38 +0100386 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 runtime->sync.id32[0] = substream->pcm->card->number;
389 runtime->sync.id32[1] = -1;
390 runtime->sync.id32[2] = -1;
391 runtime->sync.id32[3] = -1;
392}
393
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200394EXPORT_SYMBOL(snd_pcm_set_sync);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
397 * Standard ioctl routine
398 */
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static inline unsigned int div32(unsigned int a, unsigned int b,
401 unsigned int *r)
402{
403 if (b == 0) {
404 *r = 0;
405 return UINT_MAX;
406 }
407 *r = a % b;
408 return a / b;
409}
410
411static inline unsigned int div_down(unsigned int a, unsigned int b)
412{
413 if (b == 0)
414 return UINT_MAX;
415 return a / b;
416}
417
418static inline unsigned int div_up(unsigned int a, unsigned int b)
419{
420 unsigned int r;
421 unsigned int q;
422 if (b == 0)
423 return UINT_MAX;
424 q = div32(a, b, &r);
425 if (r)
426 ++q;
427 return q;
428}
429
430static inline unsigned int mul(unsigned int a, unsigned int b)
431{
432 if (a == 0)
433 return 0;
434 if (div_down(UINT_MAX, a) < b)
435 return UINT_MAX;
436 return a * b;
437}
438
439static inline unsigned int muldiv32(unsigned int a, unsigned int b,
440 unsigned int c, unsigned int *r)
441{
442 u_int64_t n = (u_int64_t) a * b;
443 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200444 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *r = 0;
446 return UINT_MAX;
447 }
448 div64_32(&n, c, r);
449 if (n >= UINT_MAX) {
450 *r = 0;
451 return UINT_MAX;
452 }
453 return n;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/**
457 * snd_interval_refine - refine the interval value of configurator
458 * @i: the interval value to refine
459 * @v: the interval value to refer to
460 *
461 * Refines the interval value with the reference value.
462 * The interval is changed to the range satisfying both intervals.
463 * The interval status (min, max, integer, etc.) are evaluated.
464 *
465 * Returns non-zero if the value is changed, zero if not changed.
466 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100467int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200470 if (snd_BUG_ON(snd_interval_empty(i)))
471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (i->min < v->min) {
473 i->min = v->min;
474 i->openmin = v->openmin;
475 changed = 1;
476 } else if (i->min == v->min && !i->openmin && v->openmin) {
477 i->openmin = 1;
478 changed = 1;
479 }
480 if (i->max > v->max) {
481 i->max = v->max;
482 i->openmax = v->openmax;
483 changed = 1;
484 } else if (i->max == v->max && !i->openmax && v->openmax) {
485 i->openmax = 1;
486 changed = 1;
487 }
488 if (!i->integer && v->integer) {
489 i->integer = 1;
490 changed = 1;
491 }
492 if (i->integer) {
493 if (i->openmin) {
494 i->min++;
495 i->openmin = 0;
496 }
497 if (i->openmax) {
498 i->max--;
499 i->openmax = 0;
500 }
501 } else if (!i->openmin && !i->openmax && i->min == i->max)
502 i->integer = 1;
503 if (snd_interval_checkempty(i)) {
504 snd_interval_none(i);
505 return -EINVAL;
506 }
507 return changed;
508}
509
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200510EXPORT_SYMBOL(snd_interval_refine);
511
Takashi Iwai877211f2005-11-17 13:59:38 +0100512static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200514 if (snd_BUG_ON(snd_interval_empty(i)))
515 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (snd_interval_single(i))
517 return 0;
518 i->max = i->min;
519 i->openmax = i->openmin;
520 if (i->openmax)
521 i->max++;
522 return 1;
523}
524
Takashi Iwai877211f2005-11-17 13:59:38 +0100525static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200527 if (snd_BUG_ON(snd_interval_empty(i)))
528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (snd_interval_single(i))
530 return 0;
531 i->min = i->max;
532 i->openmin = i->openmax;
533 if (i->openmin)
534 i->min--;
535 return 1;
536}
537
Takashi Iwai877211f2005-11-17 13:59:38 +0100538void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 if (a->empty || b->empty) {
541 snd_interval_none(c);
542 return;
543 }
544 c->empty = 0;
545 c->min = mul(a->min, b->min);
546 c->openmin = (a->openmin || b->openmin);
547 c->max = mul(a->max, b->max);
548 c->openmax = (a->openmax || b->openmax);
549 c->integer = (a->integer && b->integer);
550}
551
552/**
553 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200554 * @a: dividend
555 * @b: divisor
556 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
558 * c = a / b
559 *
560 * Returns non-zero if the value is changed, zero if not changed.
561 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100562void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 unsigned int r;
565 if (a->empty || b->empty) {
566 snd_interval_none(c);
567 return;
568 }
569 c->empty = 0;
570 c->min = div32(a->min, b->max, &r);
571 c->openmin = (r || a->openmin || b->openmax);
572 if (b->min > 0) {
573 c->max = div32(a->max, b->min, &r);
574 if (r) {
575 c->max++;
576 c->openmax = 1;
577 } else
578 c->openmax = (a->openmax || b->openmin);
579 } else {
580 c->max = UINT_MAX;
581 c->openmax = 0;
582 }
583 c->integer = 0;
584}
585
586/**
587 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200588 * @a: dividend 1
589 * @b: dividend 2
590 * @k: divisor (as integer)
591 * @c: result
592 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * c = a * b / k
594 *
595 * Returns non-zero if the value is changed, zero if not changed.
596 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100597void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
598 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 unsigned int r;
601 if (a->empty || b->empty) {
602 snd_interval_none(c);
603 return;
604 }
605 c->empty = 0;
606 c->min = muldiv32(a->min, b->min, k, &r);
607 c->openmin = (r || a->openmin || b->openmin);
608 c->max = muldiv32(a->max, b->max, k, &r);
609 if (r) {
610 c->max++;
611 c->openmax = 1;
612 } else
613 c->openmax = (a->openmax || b->openmax);
614 c->integer = 0;
615}
616
617/**
618 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200619 * @a: dividend 1
620 * @k: dividend 2 (as integer)
621 * @b: divisor
622 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
624 * c = a * k / b
625 *
626 * Returns non-zero if the value is changed, zero if not changed.
627 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100628void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
629 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 unsigned int r;
632 if (a->empty || b->empty) {
633 snd_interval_none(c);
634 return;
635 }
636 c->empty = 0;
637 c->min = muldiv32(a->min, k, b->max, &r);
638 c->openmin = (r || a->openmin || b->openmax);
639 if (b->min > 0) {
640 c->max = muldiv32(a->max, k, b->min, &r);
641 if (r) {
642 c->max++;
643 c->openmax = 1;
644 } else
645 c->openmax = (a->openmax || b->openmin);
646 } else {
647 c->max = UINT_MAX;
648 c->openmax = 0;
649 }
650 c->integer = 0;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/* ---- */
654
655
656/**
657 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200658 * @i: interval to refine
659 * @rats_count: number of ratnum_t
660 * @rats: ratnum_t array
661 * @nump: pointer to store the resultant numerator
662 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
664 * Returns non-zero if the value is changed, zero if not changed.
665 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100666int snd_interval_ratnum(struct snd_interval *i,
667 unsigned int rats_count, struct snd_ratnum *rats,
668 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 unsigned int best_num, best_diff, best_den;
671 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100672 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int err;
674
675 best_num = best_den = best_diff = 0;
676 for (k = 0; k < rats_count; ++k) {
677 unsigned int num = rats[k].num;
678 unsigned int den;
679 unsigned int q = i->min;
680 int diff;
681 if (q == 0)
682 q = 1;
683 den = div_down(num, q);
684 if (den < rats[k].den_min)
685 continue;
686 if (den > rats[k].den_max)
687 den = rats[k].den_max;
688 else {
689 unsigned int r;
690 r = (den - rats[k].den_min) % rats[k].den_step;
691 if (r != 0)
692 den -= r;
693 }
694 diff = num - q * den;
695 if (best_num == 0 ||
696 diff * best_den < best_diff * den) {
697 best_diff = diff;
698 best_den = den;
699 best_num = num;
700 }
701 }
702 if (best_den == 0) {
703 i->empty = 1;
704 return -EINVAL;
705 }
706 t.min = div_down(best_num, best_den);
707 t.openmin = !!(best_num % best_den);
708
709 best_num = best_den = best_diff = 0;
710 for (k = 0; k < rats_count; ++k) {
711 unsigned int num = rats[k].num;
712 unsigned int den;
713 unsigned int q = i->max;
714 int diff;
715 if (q == 0) {
716 i->empty = 1;
717 return -EINVAL;
718 }
719 den = div_up(num, q);
720 if (den > rats[k].den_max)
721 continue;
722 if (den < rats[k].den_min)
723 den = rats[k].den_min;
724 else {
725 unsigned int r;
726 r = (den - rats[k].den_min) % rats[k].den_step;
727 if (r != 0)
728 den += rats[k].den_step - r;
729 }
730 diff = q * den - num;
731 if (best_num == 0 ||
732 diff * best_den < best_diff * den) {
733 best_diff = diff;
734 best_den = den;
735 best_num = num;
736 }
737 }
738 if (best_den == 0) {
739 i->empty = 1;
740 return -EINVAL;
741 }
742 t.max = div_up(best_num, best_den);
743 t.openmax = !!(best_num % best_den);
744 t.integer = 0;
745 err = snd_interval_refine(i, &t);
746 if (err < 0)
747 return err;
748
749 if (snd_interval_single(i)) {
750 if (nump)
751 *nump = best_num;
752 if (denp)
753 *denp = best_den;
754 }
755 return err;
756}
757
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200758EXPORT_SYMBOL(snd_interval_ratnum);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/**
761 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200762 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100763 * @rats_count: number of struct ratden
764 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200765 * @nump: pointer to store the resultant numerator
766 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
768 * Returns non-zero if the value is changed, zero if not changed.
769 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100770static int snd_interval_ratden(struct snd_interval *i,
771 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned int *nump, unsigned int *denp)
773{
774 unsigned int best_num, best_diff, best_den;
775 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100776 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int err;
778
779 best_num = best_den = best_diff = 0;
780 for (k = 0; k < rats_count; ++k) {
781 unsigned int num;
782 unsigned int den = rats[k].den;
783 unsigned int q = i->min;
784 int diff;
785 num = mul(q, den);
786 if (num > rats[k].num_max)
787 continue;
788 if (num < rats[k].num_min)
789 num = rats[k].num_max;
790 else {
791 unsigned int r;
792 r = (num - rats[k].num_min) % rats[k].num_step;
793 if (r != 0)
794 num += rats[k].num_step - r;
795 }
796 diff = num - q * den;
797 if (best_num == 0 ||
798 diff * best_den < best_diff * den) {
799 best_diff = diff;
800 best_den = den;
801 best_num = num;
802 }
803 }
804 if (best_den == 0) {
805 i->empty = 1;
806 return -EINVAL;
807 }
808 t.min = div_down(best_num, best_den);
809 t.openmin = !!(best_num % best_den);
810
811 best_num = best_den = best_diff = 0;
812 for (k = 0; k < rats_count; ++k) {
813 unsigned int num;
814 unsigned int den = rats[k].den;
815 unsigned int q = i->max;
816 int diff;
817 num = mul(q, den);
818 if (num < rats[k].num_min)
819 continue;
820 if (num > rats[k].num_max)
821 num = rats[k].num_max;
822 else {
823 unsigned int r;
824 r = (num - rats[k].num_min) % rats[k].num_step;
825 if (r != 0)
826 num -= r;
827 }
828 diff = q * den - num;
829 if (best_num == 0 ||
830 diff * best_den < best_diff * den) {
831 best_diff = diff;
832 best_den = den;
833 best_num = num;
834 }
835 }
836 if (best_den == 0) {
837 i->empty = 1;
838 return -EINVAL;
839 }
840 t.max = div_up(best_num, best_den);
841 t.openmax = !!(best_num % best_den);
842 t.integer = 0;
843 err = snd_interval_refine(i, &t);
844 if (err < 0)
845 return err;
846
847 if (snd_interval_single(i)) {
848 if (nump)
849 *nump = best_num;
850 if (denp)
851 *denp = best_den;
852 }
853 return err;
854}
855
856/**
857 * snd_interval_list - refine the interval value from the list
858 * @i: the interval value to refine
859 * @count: the number of elements in the list
860 * @list: the value list
861 * @mask: the bit-mask to evaluate
862 *
863 * Refines the interval value from the list.
864 * When mask is non-zero, only the elements corresponding to bit 1 are
865 * evaluated.
866 *
867 * Returns non-zero if the value is changed, zero if not changed.
868 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100869int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 unsigned int k;
872 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100873
874 if (!count) {
875 i->empty = 1;
876 return -EINVAL;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 for (k = 0; k < count; k++) {
879 if (mask && !(mask & (1 << k)))
880 continue;
881 if (i->min == list[k] && !i->openmin)
882 goto _l1;
883 if (i->min < list[k]) {
884 i->min = list[k];
885 i->openmin = 0;
886 changed = 1;
887 goto _l1;
888 }
889 }
890 i->empty = 1;
891 return -EINVAL;
892 _l1:
893 for (k = count; k-- > 0;) {
894 if (mask && !(mask & (1 << k)))
895 continue;
896 if (i->max == list[k] && !i->openmax)
897 goto _l2;
898 if (i->max > list[k]) {
899 i->max = list[k];
900 i->openmax = 0;
901 changed = 1;
902 goto _l2;
903 }
904 }
905 i->empty = 1;
906 return -EINVAL;
907 _l2:
908 if (snd_interval_checkempty(i)) {
909 i->empty = 1;
910 return -EINVAL;
911 }
912 return changed;
913}
914
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200915EXPORT_SYMBOL(snd_interval_list);
916
Takashi Iwai877211f2005-11-17 13:59:38 +0100917static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 unsigned int n;
920 int changed = 0;
921 n = (i->min - min) % step;
922 if (n != 0 || i->openmin) {
923 i->min += step - n;
924 changed = 1;
925 }
926 n = (i->max - min) % step;
927 if (n != 0 || i->openmax) {
928 i->max -= n;
929 changed = 1;
930 }
931 if (snd_interval_checkempty(i)) {
932 i->empty = 1;
933 return -EINVAL;
934 }
935 return changed;
936}
937
938/* Info constraints helpers */
939
940/**
941 * snd_pcm_hw_rule_add - add the hw-constraint rule
942 * @runtime: the pcm runtime instance
943 * @cond: condition bits
944 * @var: the variable to evaluate
945 * @func: the evaluation function
946 * @private: the private data pointer passed to function
947 * @dep: the dependent variables
948 *
949 * Returns zero if successful, or a negative error code on failure.
950 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100951int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int var,
953 snd_pcm_hw_rule_func_t func, void *private,
954 int dep, ...)
955{
Takashi Iwai877211f2005-11-17 13:59:38 +0100956 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
957 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 unsigned int k;
959 va_list args;
960 va_start(args, dep);
961 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100962 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned int new_rules = constrs->rules_all + 16;
964 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
965 if (!new)
966 return -ENOMEM;
967 if (constrs->rules) {
968 memcpy(new, constrs->rules,
969 constrs->rules_num * sizeof(*c));
970 kfree(constrs->rules);
971 }
972 constrs->rules = new;
973 constrs->rules_all = new_rules;
974 }
975 c = &constrs->rules[constrs->rules_num];
976 c->cond = cond;
977 c->func = func;
978 c->var = var;
979 c->private = private;
980 k = 0;
981 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200982 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
983 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 c->deps[k++] = dep;
985 if (dep < 0)
986 break;
987 dep = va_arg(args, int);
988 }
989 constrs->rules_num++;
990 va_end(args);
991 return 0;
992}
993
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200994EXPORT_SYMBOL(snd_pcm_hw_rule_add);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700997 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200998 * @runtime: PCM runtime instance
999 * @var: hw_params variable to apply the mask
1000 * @mask: the bitmap mask
1001 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001002 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001004int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 u_int32_t mask)
1006{
Takashi Iwai877211f2005-11-17 13:59:38 +01001007 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1008 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *maskp->bits &= mask;
1010 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1011 if (*maskp->bits == 0)
1012 return -EINVAL;
1013 return 0;
1014}
1015
1016/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001017 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001018 * @runtime: PCM runtime instance
1019 * @var: hw_params variable to apply the mask
1020 * @mask: the 64bit bitmap mask
1021 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001022 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001024int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 u_int64_t mask)
1026{
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 maskp->bits[0] &= (u_int32_t)mask;
1030 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1031 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1032 if (! maskp->bits[0] && ! maskp->bits[1])
1033 return -EINVAL;
1034 return 0;
1035}
1036
1037/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001038 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001039 * @runtime: PCM runtime instance
1040 * @var: hw_params variable to apply the integer constraint
1041 *
1042 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001044int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Takashi Iwai877211f2005-11-17 13:59:38 +01001046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return snd_interval_setinteger(constrs_interval(constrs, var));
1048}
1049
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001050EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001053 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001054 * @runtime: PCM runtime instance
1055 * @var: hw_params variable to apply the range
1056 * @min: the minimal value
1057 * @max: the maximal value
1058 *
1059 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001061int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 unsigned int min, unsigned int max)
1063{
Takashi Iwai877211f2005-11-17 13:59:38 +01001064 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1065 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 t.min = min;
1067 t.max = max;
1068 t.openmin = t.openmax = 0;
1069 t.integer = 0;
1070 return snd_interval_refine(constrs_interval(constrs, var), &t);
1071}
1072
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001073EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1074
Takashi Iwai877211f2005-11-17 13:59:38 +01001075static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1076 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai877211f2005-11-17 13:59:38 +01001078 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1080}
1081
1082
1083/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001084 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001085 * @runtime: PCM runtime instance
1086 * @cond: condition bits
1087 * @var: hw_params variable to apply the list constraint
1088 * @l: list
1089 *
1090 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001092int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 unsigned int cond,
1094 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001095 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 return snd_pcm_hw_rule_add(runtime, cond, var,
1098 snd_pcm_hw_rule_list, l,
1099 var, -1);
1100}
1101
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001102EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1103
Takashi Iwai877211f2005-11-17 13:59:38 +01001104static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1105 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Takashi Iwai877211f2005-11-17 13:59:38 +01001107 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 unsigned int num = 0, den = 0;
1109 int err;
1110 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1111 r->nrats, r->rats, &num, &den);
1112 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1113 params->rate_num = num;
1114 params->rate_den = den;
1115 }
1116 return err;
1117}
1118
1119/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001120 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001121 * @runtime: PCM runtime instance
1122 * @cond: condition bits
1123 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001126int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 unsigned int cond,
1128 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001129 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 return snd_pcm_hw_rule_add(runtime, cond, var,
1132 snd_pcm_hw_rule_ratnums, r,
1133 var, -1);
1134}
1135
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001136EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1137
Takashi Iwai877211f2005-11-17 13:59:38 +01001138static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1139 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Takashi Iwai877211f2005-11-17 13:59:38 +01001141 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned int num = 0, den = 0;
1143 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1144 r->nrats, r->rats, &num, &den);
1145 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1146 params->rate_num = num;
1147 params->rate_den = den;
1148 }
1149 return err;
1150}
1151
1152/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001153 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001159int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 unsigned int cond,
1161 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001162 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 return snd_pcm_hw_rule_add(runtime, cond, var,
1165 snd_pcm_hw_rule_ratdens, r,
1166 var, -1);
1167}
1168
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001169EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1170
Takashi Iwai877211f2005-11-17 13:59:38 +01001171static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1172 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 unsigned int l = (unsigned long) rule->private;
1175 int width = l & 0xffff;
1176 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001177 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (snd_interval_single(i) && snd_interval_value(i) == width)
1179 params->msbits = msbits;
1180 return 0;
1181}
1182
1183/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001184 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001185 * @runtime: PCM runtime instance
1186 * @cond: condition bits
1187 * @width: sample bits width
1188 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001190int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 unsigned int cond,
1192 unsigned int width,
1193 unsigned int msbits)
1194{
1195 unsigned long l = (msbits << 16) | width;
1196 return snd_pcm_hw_rule_add(runtime, cond, -1,
1197 snd_pcm_hw_rule_msbits,
1198 (void*) l,
1199 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1200}
1201
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001202EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1203
Takashi Iwai877211f2005-11-17 13:59:38 +01001204static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1205 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 unsigned long step = (unsigned long) rule->private;
1208 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1209}
1210
1211/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001212 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the step constraint
1216 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001218int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned int cond,
1220 snd_pcm_hw_param_t var,
1221 unsigned long step)
1222{
1223 return snd_pcm_hw_rule_add(runtime, cond, var,
1224 snd_pcm_hw_rule_step, (void *) step,
1225 var, -1);
1226}
1227
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001228EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1229
Takashi Iwai877211f2005-11-17 13:59:38 +01001230static 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 -07001231{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001232 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1234 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1235 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1236 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1237 };
1238 return snd_interval_list(hw_param_interval(params, rule->var),
1239 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1240}
1241
1242/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001243 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001244 * @runtime: PCM runtime instance
1245 * @cond: condition bits
1246 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001248int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned int cond,
1250 snd_pcm_hw_param_t var)
1251{
1252 return snd_pcm_hw_rule_add(runtime, cond, var,
1253 snd_pcm_hw_rule_pow2, NULL,
1254 var, -1);
1255}
1256
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001257EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1258
Takashi Iwai877211f2005-11-17 13:59:38 +01001259static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001260 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 if (hw_is_mask(var)) {
1263 snd_mask_any(hw_param_mask(params, var));
1264 params->cmask |= 1 << var;
1265 params->rmask |= 1 << var;
1266 return;
1267 }
1268 if (hw_is_interval(var)) {
1269 snd_interval_any(hw_param_interval(params, var));
1270 params->cmask |= 1 << var;
1271 params->rmask |= 1 << var;
1272 return;
1273 }
1274 snd_BUG();
1275}
1276
Takashi Iwai877211f2005-11-17 13:59:38 +01001277void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 unsigned int k;
1280 memset(params, 0, sizeof(*params));
1281 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1282 _snd_pcm_hw_param_any(params, k);
1283 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1284 _snd_pcm_hw_param_any(params, k);
1285 params->info = ~0U;
1286}
1287
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001288EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001291 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001292 * @params: the hw_params instance
1293 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001294 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001296 * Return the value for field @var if it's fixed in configuration space
1297 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001299int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1300 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001303 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!snd_mask_single(mask))
1305 return -EINVAL;
1306 if (dir)
1307 *dir = 0;
1308 return snd_mask_value(mask);
1309 }
1310 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001311 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!snd_interval_single(i))
1313 return -EINVAL;
1314 if (dir)
1315 *dir = i->openmin;
1316 return snd_interval_value(i);
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EINVAL;
1319}
1320
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001321EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Takashi Iwai877211f2005-11-17 13:59:38 +01001323void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 snd_pcm_hw_param_t var)
1325{
1326 if (hw_is_mask(var)) {
1327 snd_mask_none(hw_param_mask(params, var));
1328 params->cmask |= 1 << var;
1329 params->rmask |= 1 << var;
1330 } else if (hw_is_interval(var)) {
1331 snd_interval_none(hw_param_interval(params, var));
1332 params->cmask |= 1 << var;
1333 params->rmask |= 1 << var;
1334 } else {
1335 snd_BUG();
1336 }
1337}
1338
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001339EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Takashi Iwai877211f2005-11-17 13:59:38 +01001341static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001342 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 int changed;
1345 if (hw_is_mask(var))
1346 changed = snd_mask_refine_first(hw_param_mask(params, var));
1347 else if (hw_is_interval(var))
1348 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001349 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (changed) {
1352 params->cmask |= 1 << var;
1353 params->rmask |= 1 << var;
1354 }
1355 return changed;
1356}
1357
1358
1359/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001360 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001361 * @pcm: PCM instance
1362 * @params: the hw_params instance
1363 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001364 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001366 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * values > minimum. Reduce configuration space accordingly.
1368 * Return the minimum.
1369 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001370int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1371 struct snd_pcm_hw_params *params,
1372 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 int changed = _snd_pcm_hw_param_first(params, var);
1375 if (changed < 0)
1376 return changed;
1377 if (params->rmask) {
1378 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001379 if (snd_BUG_ON(err < 0))
1380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 return snd_pcm_hw_param_value(params, var, dir);
1383}
1384
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001385EXPORT_SYMBOL(snd_pcm_hw_param_first);
1386
Takashi Iwai877211f2005-11-17 13:59:38 +01001387static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001388 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int changed;
1391 if (hw_is_mask(var))
1392 changed = snd_mask_refine_last(hw_param_mask(params, var));
1393 else if (hw_is_interval(var))
1394 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (changed) {
1398 params->cmask |= 1 << var;
1399 params->rmask |= 1 << var;
1400 }
1401 return changed;
1402}
1403
1404
1405/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001406 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001407 * @pcm: PCM instance
1408 * @params: the hw_params instance
1409 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001410 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001412 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * values < maximum. Reduce configuration space accordingly.
1414 * Return the maximum.
1415 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001416int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1417 struct snd_pcm_hw_params *params,
1418 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 int changed = _snd_pcm_hw_param_last(params, var);
1421 if (changed < 0)
1422 return changed;
1423 if (params->rmask) {
1424 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001425 if (snd_BUG_ON(err < 0))
1426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 return snd_pcm_hw_param_value(params, var, dir);
1429}
1430
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001431EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001435 * @pcm: PCM instance
1436 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001438 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * The configuration chosen is that obtained fixing in this order:
1440 * first access, first format, first subformat, min channels,
1441 * min rate, min period time, max buffer size, min tick time
1442 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001443int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1444 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001446 static int vars[] = {
1447 SNDRV_PCM_HW_PARAM_ACCESS,
1448 SNDRV_PCM_HW_PARAM_FORMAT,
1449 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1450 SNDRV_PCM_HW_PARAM_CHANNELS,
1451 SNDRV_PCM_HW_PARAM_RATE,
1452 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1453 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1454 SNDRV_PCM_HW_PARAM_TICK_TIME,
1455 -1
1456 };
1457 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001459 for (v = vars; *v != -1; v++) {
1460 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1461 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1462 else
1463 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001464 if (snd_BUG_ON(err < 0))
1465 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
1469
Takashi Iwai877211f2005-11-17 13:59:38 +01001470static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 void *arg)
1472{
Takashi Iwai877211f2005-11-17 13:59:38 +01001473 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned long flags;
1475 snd_pcm_stream_lock_irqsave(substream, flags);
1476 if (snd_pcm_running(substream) &&
1477 snd_pcm_update_hw_ptr(substream) >= 0)
1478 runtime->status->hw_ptr %= runtime->buffer_size;
1479 else
1480 runtime->status->hw_ptr = 0;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +02001481 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 snd_pcm_stream_unlock_irqrestore(substream, flags);
1483 return 0;
1484}
1485
Takashi Iwai877211f2005-11-17 13:59:38 +01001486static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 void *arg)
1488{
Takashi Iwai877211f2005-11-17 13:59:38 +01001489 struct snd_pcm_channel_info *info = arg;
1490 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int width;
1492 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1493 info->offset = -1;
1494 return 0;
1495 }
1496 width = snd_pcm_format_physical_width(runtime->format);
1497 if (width < 0)
1498 return width;
1499 info->offset = 0;
1500 switch (runtime->access) {
1501 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1502 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1503 info->first = info->channel * width;
1504 info->step = runtime->channels * width;
1505 break;
1506 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1507 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1508 {
1509 size_t size = runtime->dma_bytes / runtime->channels;
1510 info->first = info->channel * size * 8;
1511 info->step = width;
1512 break;
1513 }
1514 default:
1515 snd_BUG();
1516 break;
1517 }
1518 return 0;
1519}
1520
1521/**
1522 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1523 * @substream: the pcm substream instance
1524 * @cmd: ioctl command
1525 * @arg: ioctl argument
1526 *
1527 * Processes the generic ioctl commands for PCM.
1528 * Can be passed as the ioctl callback for PCM ops.
1529 *
1530 * Returns zero if successful, or a negative error code on failure.
1531 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001532int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 unsigned int cmd, void *arg)
1534{
1535 switch (cmd) {
1536 case SNDRV_PCM_IOCTL1_INFO:
1537 return 0;
1538 case SNDRV_PCM_IOCTL1_RESET:
1539 return snd_pcm_lib_ioctl_reset(substream, arg);
1540 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1541 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1542 }
1543 return -ENXIO;
1544}
1545
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001546EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/**
1549 * snd_pcm_period_elapsed - update the pcm status for the next period
1550 * @substream: the pcm substream instance
1551 *
1552 * This function is called from the interrupt handler when the
1553 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001554 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 *
1556 * Even if more than one periods have elapsed since the last call, you
1557 * have to call this only once.
1558 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001559void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Takashi Iwai877211f2005-11-17 13:59:38 +01001561 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 unsigned long flags;
1563
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001564 if (PCM_RUNTIME_CHECK(substream))
1565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (runtime->transfer_ack_begin)
1569 runtime->transfer_ack_begin(substream);
1570
1571 snd_pcm_stream_lock_irqsave(substream, flags);
1572 if (!snd_pcm_running(substream) ||
1573 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1574 goto _end;
1575
1576 if (substream->timer_running)
1577 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 _end:
1579 snd_pcm_stream_unlock_irqrestore(substream, flags);
1580 if (runtime->transfer_ack_end)
1581 runtime->transfer_ack_end(substream);
1582 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1583}
1584
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001585EXPORT_SYMBOL(snd_pcm_period_elapsed);
1586
Takashi Iwai13075512008-01-08 18:08:14 +01001587/*
1588 * Wait until avail_min data becomes available
1589 * Returns a negative error code if any error occurs during operation.
1590 * The available space is stored on availp. When err = 0 and avail = 0
1591 * on the capture stream, it indicates the stream is in DRAINING state.
1592 */
1593static int wait_for_avail_min(struct snd_pcm_substream *substream,
1594 snd_pcm_uframes_t *availp)
1595{
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1598 wait_queue_t wait;
1599 int err = 0;
1600 snd_pcm_uframes_t avail = 0;
1601 long tout;
1602
1603 init_waitqueue_entry(&wait, current);
1604 add_wait_queue(&runtime->sleep, &wait);
1605 for (;;) {
1606 if (signal_pending(current)) {
1607 err = -ERESTARTSYS;
1608 break;
1609 }
1610 set_current_state(TASK_INTERRUPTIBLE);
1611 snd_pcm_stream_unlock_irq(substream);
1612 tout = schedule_timeout(msecs_to_jiffies(10000));
1613 snd_pcm_stream_lock_irq(substream);
1614 switch (runtime->status->state) {
1615 case SNDRV_PCM_STATE_SUSPENDED:
1616 err = -ESTRPIPE;
1617 goto _endloop;
1618 case SNDRV_PCM_STATE_XRUN:
1619 err = -EPIPE;
1620 goto _endloop;
1621 case SNDRV_PCM_STATE_DRAINING:
1622 if (is_playback)
1623 err = -EPIPE;
1624 else
1625 avail = 0; /* indicate draining */
1626 goto _endloop;
1627 case SNDRV_PCM_STATE_OPEN:
1628 case SNDRV_PCM_STATE_SETUP:
1629 case SNDRV_PCM_STATE_DISCONNECTED:
1630 err = -EBADFD;
1631 goto _endloop;
1632 }
1633 if (!tout) {
1634 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1635 is_playback ? "playback" : "capture");
1636 err = -EIO;
1637 break;
1638 }
1639 if (is_playback)
1640 avail = snd_pcm_playback_avail(runtime);
1641 else
1642 avail = snd_pcm_capture_avail(runtime);
1643 if (avail >= runtime->control->avail_min)
1644 break;
1645 }
1646 _endloop:
1647 remove_wait_queue(&runtime->sleep, &wait);
1648 *availp = avail;
1649 return err;
1650}
1651
Takashi Iwai877211f2005-11-17 13:59:38 +01001652static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 unsigned int hwoff,
1654 unsigned long data, unsigned int off,
1655 snd_pcm_uframes_t frames)
1656{
Takashi Iwai877211f2005-11-17 13:59:38 +01001657 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err;
1659 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1660 if (substream->ops->copy) {
1661 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1662 return err;
1663 } else {
1664 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1666 return -EFAULT;
1667 }
1668 return 0;
1669}
1670
Takashi Iwai877211f2005-11-17 13:59:38 +01001671typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 unsigned long data, unsigned int off,
1673 snd_pcm_uframes_t size);
1674
Takashi Iwai877211f2005-11-17 13:59:38 +01001675static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 unsigned long data,
1677 snd_pcm_uframes_t size,
1678 int nonblock,
1679 transfer_f transfer)
1680{
Takashi Iwai877211f2005-11-17 13:59:38 +01001681 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 snd_pcm_uframes_t xfer = 0;
1683 snd_pcm_uframes_t offset = 0;
1684 int err = 0;
1685
1686 if (size == 0)
1687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 snd_pcm_stream_lock_irq(substream);
1690 switch (runtime->status->state) {
1691 case SNDRV_PCM_STATE_PREPARED:
1692 case SNDRV_PCM_STATE_RUNNING:
1693 case SNDRV_PCM_STATE_PAUSED:
1694 break;
1695 case SNDRV_PCM_STATE_XRUN:
1696 err = -EPIPE;
1697 goto _end_unlock;
1698 case SNDRV_PCM_STATE_SUSPENDED:
1699 err = -ESTRPIPE;
1700 goto _end_unlock;
1701 default:
1702 err = -EBADFD;
1703 goto _end_unlock;
1704 }
1705
1706 while (size > 0) {
1707 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1708 snd_pcm_uframes_t avail;
1709 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001710 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_pcm_update_hw_ptr(substream);
1712 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001713 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (nonblock) {
1715 err = -EAGAIN;
1716 goto _end_unlock;
1717 }
Takashi Iwai13075512008-01-08 18:08:14 +01001718 err = wait_for_avail_min(substream, &avail);
1719 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 frames = size > avail ? avail : size;
1723 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1724 if (frames > cont)
1725 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001726 if (snd_BUG_ON(!frames)) {
1727 snd_pcm_stream_unlock_irq(substream);
1728 return -EINVAL;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 appl_ptr = runtime->control->appl_ptr;
1731 appl_ofs = appl_ptr % runtime->buffer_size;
1732 snd_pcm_stream_unlock_irq(substream);
1733 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1734 goto _end;
1735 snd_pcm_stream_lock_irq(substream);
1736 switch (runtime->status->state) {
1737 case SNDRV_PCM_STATE_XRUN:
1738 err = -EPIPE;
1739 goto _end_unlock;
1740 case SNDRV_PCM_STATE_SUSPENDED:
1741 err = -ESTRPIPE;
1742 goto _end_unlock;
1743 default:
1744 break;
1745 }
1746 appl_ptr += frames;
1747 if (appl_ptr >= runtime->boundary)
1748 appl_ptr -= runtime->boundary;
1749 runtime->control->appl_ptr = appl_ptr;
1750 if (substream->ops->ack)
1751 substream->ops->ack(substream);
1752
1753 offset += frames;
1754 size -= frames;
1755 xfer += frames;
1756 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1757 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1758 err = snd_pcm_start(substream);
1759 if (err < 0)
1760 goto _end_unlock;
1761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 _end_unlock:
1764 snd_pcm_stream_unlock_irq(substream);
1765 _end:
1766 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1767}
1768
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001769/* sanity-check for read/write methods */
1770static int pcm_sanity_check(struct snd_pcm_substream *substream)
1771{
1772 struct snd_pcm_runtime *runtime;
1773 if (PCM_RUNTIME_CHECK(substream))
1774 return -ENXIO;
1775 runtime = substream->runtime;
1776 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1777 return -EINVAL;
1778 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1779 return -EBADFD;
1780 return 0;
1781}
1782
Takashi Iwai877211f2005-11-17 13:59:38 +01001783snd_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 -07001784{
Takashi Iwai877211f2005-11-17 13:59:38 +01001785 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001787 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001789 err = pcm_sanity_check(substream);
1790 if (err < 0)
1791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001793 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1796 runtime->channels > 1)
1797 return -EINVAL;
1798 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1799 snd_pcm_lib_write_transfer);
1800}
1801
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001802EXPORT_SYMBOL(snd_pcm_lib_write);
1803
Takashi Iwai877211f2005-11-17 13:59:38 +01001804static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 unsigned int hwoff,
1806 unsigned long data, unsigned int off,
1807 snd_pcm_uframes_t frames)
1808{
Takashi Iwai877211f2005-11-17 13:59:38 +01001809 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int err;
1811 void __user **bufs = (void __user **)data;
1812 int channels = runtime->channels;
1813 int c;
1814 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001815 if (snd_BUG_ON(!substream->ops->silence))
1816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 for (c = 0; c < channels; ++c, ++bufs) {
1818 if (*bufs == NULL) {
1819 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1820 return err;
1821 } else {
1822 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1823 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1824 return err;
1825 }
1826 }
1827 } else {
1828 /* default transfer behaviour */
1829 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 for (c = 0; c < channels; ++c, ++bufs) {
1831 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1832 if (*bufs == NULL) {
1833 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1834 } else {
1835 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1836 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1837 return -EFAULT;
1838 }
1839 }
1840 }
1841 return 0;
1842}
1843
Takashi Iwai877211f2005-11-17 13:59:38 +01001844snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 void __user **bufs,
1846 snd_pcm_uframes_t frames)
1847{
Takashi Iwai877211f2005-11-17 13:59:38 +01001848 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001850 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001852 err = pcm_sanity_check(substream);
1853 if (err < 0)
1854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001856 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1859 return -EINVAL;
1860 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1861 nonblock, snd_pcm_lib_writev_transfer);
1862}
1863
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001864EXPORT_SYMBOL(snd_pcm_lib_writev);
1865
Takashi Iwai877211f2005-11-17 13:59:38 +01001866static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 unsigned int hwoff,
1868 unsigned long data, unsigned int off,
1869 snd_pcm_uframes_t frames)
1870{
Takashi Iwai877211f2005-11-17 13:59:38 +01001871 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int err;
1873 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1874 if (substream->ops->copy) {
1875 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1876 return err;
1877 } else {
1878 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1880 return -EFAULT;
1881 }
1882 return 0;
1883}
1884
Takashi Iwai877211f2005-11-17 13:59:38 +01001885static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 unsigned long data,
1887 snd_pcm_uframes_t size,
1888 int nonblock,
1889 transfer_f transfer)
1890{
Takashi Iwai877211f2005-11-17 13:59:38 +01001891 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 snd_pcm_uframes_t xfer = 0;
1893 snd_pcm_uframes_t offset = 0;
1894 int err = 0;
1895
1896 if (size == 0)
1897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 snd_pcm_stream_lock_irq(substream);
1900 switch (runtime->status->state) {
1901 case SNDRV_PCM_STATE_PREPARED:
1902 if (size >= runtime->start_threshold) {
1903 err = snd_pcm_start(substream);
1904 if (err < 0)
1905 goto _end_unlock;
1906 }
1907 break;
1908 case SNDRV_PCM_STATE_DRAINING:
1909 case SNDRV_PCM_STATE_RUNNING:
1910 case SNDRV_PCM_STATE_PAUSED:
1911 break;
1912 case SNDRV_PCM_STATE_XRUN:
1913 err = -EPIPE;
1914 goto _end_unlock;
1915 case SNDRV_PCM_STATE_SUSPENDED:
1916 err = -ESTRPIPE;
1917 goto _end_unlock;
1918 default:
1919 err = -EBADFD;
1920 goto _end_unlock;
1921 }
1922
1923 while (size > 0) {
1924 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1925 snd_pcm_uframes_t avail;
1926 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001927 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001930 if (!avail) {
1931 if (runtime->status->state ==
1932 SNDRV_PCM_STATE_DRAINING) {
1933 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto _end_unlock;
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (nonblock) {
1937 err = -EAGAIN;
1938 goto _end_unlock;
1939 }
Takashi Iwai13075512008-01-08 18:08:14 +01001940 err = wait_for_avail_min(substream, &avail);
1941 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001943 if (!avail)
1944 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 frames = size > avail ? avail : size;
1947 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1948 if (frames > cont)
1949 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001950 if (snd_BUG_ON(!frames)) {
1951 snd_pcm_stream_unlock_irq(substream);
1952 return -EINVAL;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 appl_ptr = runtime->control->appl_ptr;
1955 appl_ofs = appl_ptr % runtime->buffer_size;
1956 snd_pcm_stream_unlock_irq(substream);
1957 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1958 goto _end;
1959 snd_pcm_stream_lock_irq(substream);
1960 switch (runtime->status->state) {
1961 case SNDRV_PCM_STATE_XRUN:
1962 err = -EPIPE;
1963 goto _end_unlock;
1964 case SNDRV_PCM_STATE_SUSPENDED:
1965 err = -ESTRPIPE;
1966 goto _end_unlock;
1967 default:
1968 break;
1969 }
1970 appl_ptr += frames;
1971 if (appl_ptr >= runtime->boundary)
1972 appl_ptr -= runtime->boundary;
1973 runtime->control->appl_ptr = appl_ptr;
1974 if (substream->ops->ack)
1975 substream->ops->ack(substream);
1976
1977 offset += frames;
1978 size -= frames;
1979 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 _end_unlock:
1982 snd_pcm_stream_unlock_irq(substream);
1983 _end:
1984 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1985}
1986
Takashi Iwai877211f2005-11-17 13:59:38 +01001987snd_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 -07001988{
Takashi Iwai877211f2005-11-17 13:59:38 +01001989 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001993 err = pcm_sanity_check(substream);
1994 if (err < 0)
1995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001997 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1999 return -EINVAL;
2000 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2001}
2002
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002003EXPORT_SYMBOL(snd_pcm_lib_read);
2004
Takashi Iwai877211f2005-11-17 13:59:38 +01002005static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 unsigned int hwoff,
2007 unsigned long data, unsigned int off,
2008 snd_pcm_uframes_t frames)
2009{
Takashi Iwai877211f2005-11-17 13:59:38 +01002010 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int err;
2012 void __user **bufs = (void __user **)data;
2013 int channels = runtime->channels;
2014 int c;
2015 if (substream->ops->copy) {
2016 for (c = 0; c < channels; ++c, ++bufs) {
2017 char __user *buf;
2018 if (*bufs == NULL)
2019 continue;
2020 buf = *bufs + samples_to_bytes(runtime, off);
2021 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2022 return err;
2023 }
2024 } else {
2025 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 for (c = 0; c < channels; ++c, ++bufs) {
2027 char *hwbuf;
2028 char __user *buf;
2029 if (*bufs == NULL)
2030 continue;
2031
2032 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2033 buf = *bufs + samples_to_bytes(runtime, off);
2034 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2035 return -EFAULT;
2036 }
2037 }
2038 return 0;
2039}
2040
Takashi Iwai877211f2005-11-17 13:59:38 +01002041snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 void __user **bufs,
2043 snd_pcm_uframes_t frames)
2044{
Takashi Iwai877211f2005-11-17 13:59:38 +01002045 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002047 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002049 err = pcm_sanity_check(substream);
2050 if (err < 0)
2051 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2054 return -EBADFD;
2055
Takashi Iwai0df63e42006-04-28 15:13:41 +02002056 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2058 return -EINVAL;
2059 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2060}
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062EXPORT_SYMBOL(snd_pcm_lib_readv);