blob: 0f299a5ad6da31139c9f97f87ee5a59930a5e7a3 [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
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200129#define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100130#else
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200131#define xrun_debug(substream, mask) 0
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100132#endif
133
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200134#define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream, 2)) \
136 dump_stack(); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100137 } 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{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200141 struct snd_pcm_runtime *runtime = substream->runtime;
142
143 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
144 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200146 if (xrun_debug(substream, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
148 substream->pcm->card->number,
149 substream->pcm->device,
150 substream->stream ? 'c' : 'p');
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100151 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Takashi Iwai98204642009-03-19 09:59:21 +0100155static snd_pcm_uframes_t
156snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
157 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 snd_pcm_uframes_t pos;
160
161 pos = substream->ops->pointer(substream);
162 if (pos == SNDRV_PCM_POS_XRUN)
163 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100165 if (printk_ratelimit()) {
166 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
167 "buffer size = 0x%lx, period size = 0x%lx\n",
168 substream->stream, pos, runtime->buffer_size,
169 runtime->period_size);
170 }
171 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 pos -= pos % runtime->min_align;
174 return pos;
175}
176
Takashi Iwai98204642009-03-19 09:59:21 +0100177static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
178 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 snd_pcm_uframes_t avail;
181
182 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
183 avail = snd_pcm_playback_avail(runtime);
184 else
185 avail = snd_pcm_capture_avail(runtime);
186 if (avail > runtime->avail_max)
187 runtime->avail_max = avail;
188 if (avail >= runtime->stop_threshold) {
189 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
190 snd_pcm_drain_done(substream);
191 else
192 xrun(substream);
193 return -EPIPE;
194 }
195 if (avail >= runtime->control->avail_min)
196 wake_up(&runtime->sleep);
197 return 0;
198}
199
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100200#define hw_ptr_error(substream, fmt, args...) \
201 do { \
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200202 if (xrun_debug(substream, 1)) { \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100203 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100204 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100205 } \
206 dump_stack_on_xrun(substream); \
207 } \
208 } while (0)
209
Takashi Iwai98204642009-03-19 09:59:21 +0100210static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Takashi Iwai877211f2005-11-17 13:59:38 +0100212 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 snd_pcm_uframes_t pos;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200214 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
215 snd_pcm_sframes_t hdelta, delta;
216 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200218 old_hw_ptr = runtime->status->hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
220 if (pos == SNDRV_PCM_POS_XRUN) {
221 xrun(substream);
222 return -EPIPE;
223 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100224 hw_base = runtime->hw_ptr_base;
225 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100227 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100228 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100229 hw_ptr_interrupt -= runtime->boundary;
230 if (hw_base < runtime->boundary / 2)
231 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100232 delta = new_hw_ptr - hw_ptr_interrupt;
233 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100234 if (delta < 0) {
235 delta += runtime->buffer_size;
236 if (delta < 0) {
237 hw_ptr_error(substream,
238 "Unexpected hw_pointer value "
239 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
240 substream->stream, (long)pos,
241 (long)hw_ptr_interrupt);
242 /* rebase to interrupt position */
243 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100244 /* align hw_base to buffer_size */
245 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100246 delta = 0;
247 } else {
248 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100249 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100250 hw_base = 0;
251 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200254
255 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200256 if (!xrun_debug(substream, 4))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200257 goto no_jiffies_check;
258
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200259 /* Skip the jiffies check for hardwares with BATCH flag.
260 * Such hardware usually just increases the position at each IRQ,
261 * thus it can't give any strange position.
262 */
263 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
264 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200265 hdelta = new_hw_ptr - old_hw_ptr;
266 jdelta = jiffies - runtime->hw_ptr_jiffies;
267 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
268 delta = jdelta /
269 (((runtime->period_size * HZ) / runtime->rate)
270 + HZ/100);
271 hw_ptr_error(substream,
272 "hw_ptr skipping! [Q] "
273 "(pos=%ld, delta=%ld, period=%ld, "
274 "jdelta=%lu/%lu/%lu)\n",
275 (long)pos, (long)hdelta,
276 (long)runtime->period_size, jdelta,
277 ((hdelta * HZ) / runtime->rate), delta);
278 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
279 runtime->period_size * delta;
280 if (hw_ptr_interrupt >= runtime->boundary)
281 hw_ptr_interrupt -= runtime->boundary;
282 /* rebase to interrupt position */
283 hw_base = new_hw_ptr = hw_ptr_interrupt;
284 /* align hw_base to buffer_size */
285 hw_base -= hw_base % runtime->buffer_size;
286 delta = 0;
287 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200288 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200289 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100290 hw_ptr_error(substream,
291 "Lost interrupts? "
292 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
293 substream->stream, (long)delta,
294 (long)hw_ptr_interrupt);
295 /* rebase hw_ptr_interrupt */
296 hw_ptr_interrupt =
297 new_hw_ptr - new_hw_ptr % runtime->period_size;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
300 runtime->silence_size > 0)
301 snd_pcm_playback_silence(substream, new_hw_ptr);
302
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200303 if (runtime->status->hw_ptr == new_hw_ptr)
304 return 0;
305
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100306 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200308 runtime->hw_ptr_jiffies = jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100309 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200310 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
311 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return snd_pcm_update_hw_ptr_post(substream, runtime);
314}
315
316/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100317int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwai877211f2005-11-17 13:59:38 +0100319 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100321 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200323 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 old_hw_ptr = runtime->status->hw_ptr;
326 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
327 if (pos == SNDRV_PCM_POS_XRUN) {
328 xrun(substream);
329 return -EPIPE;
330 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100331 hw_base = runtime->hw_ptr_base;
332 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100334 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200335 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100336 if (delta < 0) {
337 delta += runtime->buffer_size;
338 if (delta < 0) {
339 hw_ptr_error(substream,
340 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200341 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100342 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200343 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100346 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100347 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100348 hw_base = 0;
349 new_hw_ptr = hw_base + pos;
350 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200351 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200352 if (xrun_debug(substream, 4) &&
Takashi Iwaic87d9732009-05-27 10:53:33 +0200353 ((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100354 hw_ptr_error(substream,
355 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200356 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100357 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200358 (long)runtime->period_size, jdelta,
359 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
363 runtime->silence_size > 0)
364 snd_pcm_playback_silence(substream, new_hw_ptr);
365
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200366 if (runtime->status->hw_ptr != new_hw_ptr)
367 return 0;
368
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100369 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200371 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200372 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
373 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return snd_pcm_update_hw_ptr_post(substream, runtime);
376}
377
378/**
379 * snd_pcm_set_ops - set the PCM operators
380 * @pcm: the pcm instance
381 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
382 * @ops: the operator table
383 *
384 * Sets the given PCM operators to the pcm instance.
385 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100386void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Takashi Iwai877211f2005-11-17 13:59:38 +0100388 struct snd_pcm_str *stream = &pcm->streams[direction];
389 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 for (substream = stream->substream; substream != NULL; substream = substream->next)
392 substream->ops = ops;
393}
394
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200395EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/**
398 * snd_pcm_sync - set the PCM sync id
399 * @substream: the pcm substream
400 *
401 * Sets the PCM sync identifier for the card.
402 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100403void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Takashi Iwai877211f2005-11-17 13:59:38 +0100405 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 runtime->sync.id32[0] = substream->pcm->card->number;
408 runtime->sync.id32[1] = -1;
409 runtime->sync.id32[2] = -1;
410 runtime->sync.id32[3] = -1;
411}
412
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200413EXPORT_SYMBOL(snd_pcm_set_sync);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
416 * Standard ioctl routine
417 */
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static inline unsigned int div32(unsigned int a, unsigned int b,
420 unsigned int *r)
421{
422 if (b == 0) {
423 *r = 0;
424 return UINT_MAX;
425 }
426 *r = a % b;
427 return a / b;
428}
429
430static inline unsigned int div_down(unsigned int a, unsigned int b)
431{
432 if (b == 0)
433 return UINT_MAX;
434 return a / b;
435}
436
437static inline unsigned int div_up(unsigned int a, unsigned int b)
438{
439 unsigned int r;
440 unsigned int q;
441 if (b == 0)
442 return UINT_MAX;
443 q = div32(a, b, &r);
444 if (r)
445 ++q;
446 return q;
447}
448
449static inline unsigned int mul(unsigned int a, unsigned int b)
450{
451 if (a == 0)
452 return 0;
453 if (div_down(UINT_MAX, a) < b)
454 return UINT_MAX;
455 return a * b;
456}
457
458static inline unsigned int muldiv32(unsigned int a, unsigned int b,
459 unsigned int c, unsigned int *r)
460{
461 u_int64_t n = (u_int64_t) a * b;
462 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200463 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *r = 0;
465 return UINT_MAX;
466 }
467 div64_32(&n, c, r);
468 if (n >= UINT_MAX) {
469 *r = 0;
470 return UINT_MAX;
471 }
472 return n;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/**
476 * snd_interval_refine - refine the interval value of configurator
477 * @i: the interval value to refine
478 * @v: the interval value to refer to
479 *
480 * Refines the interval value with the reference value.
481 * The interval is changed to the range satisfying both intervals.
482 * The interval status (min, max, integer, etc.) are evaluated.
483 *
484 * Returns non-zero if the value is changed, zero if not changed.
485 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100486int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200489 if (snd_BUG_ON(snd_interval_empty(i)))
490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (i->min < v->min) {
492 i->min = v->min;
493 i->openmin = v->openmin;
494 changed = 1;
495 } else if (i->min == v->min && !i->openmin && v->openmin) {
496 i->openmin = 1;
497 changed = 1;
498 }
499 if (i->max > v->max) {
500 i->max = v->max;
501 i->openmax = v->openmax;
502 changed = 1;
503 } else if (i->max == v->max && !i->openmax && v->openmax) {
504 i->openmax = 1;
505 changed = 1;
506 }
507 if (!i->integer && v->integer) {
508 i->integer = 1;
509 changed = 1;
510 }
511 if (i->integer) {
512 if (i->openmin) {
513 i->min++;
514 i->openmin = 0;
515 }
516 if (i->openmax) {
517 i->max--;
518 i->openmax = 0;
519 }
520 } else if (!i->openmin && !i->openmax && i->min == i->max)
521 i->integer = 1;
522 if (snd_interval_checkempty(i)) {
523 snd_interval_none(i);
524 return -EINVAL;
525 }
526 return changed;
527}
528
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200529EXPORT_SYMBOL(snd_interval_refine);
530
Takashi Iwai877211f2005-11-17 13:59:38 +0100531static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200533 if (snd_BUG_ON(snd_interval_empty(i)))
534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (snd_interval_single(i))
536 return 0;
537 i->max = i->min;
538 i->openmax = i->openmin;
539 if (i->openmax)
540 i->max++;
541 return 1;
542}
543
Takashi Iwai877211f2005-11-17 13:59:38 +0100544static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200546 if (snd_BUG_ON(snd_interval_empty(i)))
547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (snd_interval_single(i))
549 return 0;
550 i->min = i->max;
551 i->openmin = i->openmax;
552 if (i->openmin)
553 i->min--;
554 return 1;
555}
556
Takashi Iwai877211f2005-11-17 13:59:38 +0100557void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 if (a->empty || b->empty) {
560 snd_interval_none(c);
561 return;
562 }
563 c->empty = 0;
564 c->min = mul(a->min, b->min);
565 c->openmin = (a->openmin || b->openmin);
566 c->max = mul(a->max, b->max);
567 c->openmax = (a->openmax || b->openmax);
568 c->integer = (a->integer && b->integer);
569}
570
571/**
572 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200573 * @a: dividend
574 * @b: divisor
575 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * c = a / b
578 *
579 * Returns non-zero if the value is changed, zero if not changed.
580 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100581void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 unsigned int r;
584 if (a->empty || b->empty) {
585 snd_interval_none(c);
586 return;
587 }
588 c->empty = 0;
589 c->min = div32(a->min, b->max, &r);
590 c->openmin = (r || a->openmin || b->openmax);
591 if (b->min > 0) {
592 c->max = div32(a->max, b->min, &r);
593 if (r) {
594 c->max++;
595 c->openmax = 1;
596 } else
597 c->openmax = (a->openmax || b->openmin);
598 } else {
599 c->max = UINT_MAX;
600 c->openmax = 0;
601 }
602 c->integer = 0;
603}
604
605/**
606 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200607 * @a: dividend 1
608 * @b: dividend 2
609 * @k: divisor (as integer)
610 * @c: result
611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * c = a * b / k
613 *
614 * Returns non-zero if the value is changed, zero if not changed.
615 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100616void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
617 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 unsigned int r;
620 if (a->empty || b->empty) {
621 snd_interval_none(c);
622 return;
623 }
624 c->empty = 0;
625 c->min = muldiv32(a->min, b->min, k, &r);
626 c->openmin = (r || a->openmin || b->openmin);
627 c->max = muldiv32(a->max, b->max, k, &r);
628 if (r) {
629 c->max++;
630 c->openmax = 1;
631 } else
632 c->openmax = (a->openmax || b->openmax);
633 c->integer = 0;
634}
635
636/**
637 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200638 * @a: dividend 1
639 * @k: dividend 2 (as integer)
640 * @b: divisor
641 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * c = a * k / b
644 *
645 * Returns non-zero if the value is changed, zero if not changed.
646 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100647void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
648 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 unsigned int r;
651 if (a->empty || b->empty) {
652 snd_interval_none(c);
653 return;
654 }
655 c->empty = 0;
656 c->min = muldiv32(a->min, k, b->max, &r);
657 c->openmin = (r || a->openmin || b->openmax);
658 if (b->min > 0) {
659 c->max = muldiv32(a->max, k, b->min, &r);
660 if (r) {
661 c->max++;
662 c->openmax = 1;
663 } else
664 c->openmax = (a->openmax || b->openmin);
665 } else {
666 c->max = UINT_MAX;
667 c->openmax = 0;
668 }
669 c->integer = 0;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/* ---- */
673
674
675/**
676 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200677 * @i: interval to refine
678 * @rats_count: number of ratnum_t
679 * @rats: ratnum_t array
680 * @nump: pointer to store the resultant numerator
681 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
683 * Returns non-zero if the value is changed, zero if not changed.
684 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100685int snd_interval_ratnum(struct snd_interval *i,
686 unsigned int rats_count, struct snd_ratnum *rats,
687 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 unsigned int best_num, best_diff, best_den;
690 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100691 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int err;
693
694 best_num = best_den = best_diff = 0;
695 for (k = 0; k < rats_count; ++k) {
696 unsigned int num = rats[k].num;
697 unsigned int den;
698 unsigned int q = i->min;
699 int diff;
700 if (q == 0)
701 q = 1;
702 den = div_down(num, q);
703 if (den < rats[k].den_min)
704 continue;
705 if (den > rats[k].den_max)
706 den = rats[k].den_max;
707 else {
708 unsigned int r;
709 r = (den - rats[k].den_min) % rats[k].den_step;
710 if (r != 0)
711 den -= r;
712 }
713 diff = num - q * den;
714 if (best_num == 0 ||
715 diff * best_den < best_diff * den) {
716 best_diff = diff;
717 best_den = den;
718 best_num = num;
719 }
720 }
721 if (best_den == 0) {
722 i->empty = 1;
723 return -EINVAL;
724 }
725 t.min = div_down(best_num, best_den);
726 t.openmin = !!(best_num % best_den);
727
728 best_num = best_den = best_diff = 0;
729 for (k = 0; k < rats_count; ++k) {
730 unsigned int num = rats[k].num;
731 unsigned int den;
732 unsigned int q = i->max;
733 int diff;
734 if (q == 0) {
735 i->empty = 1;
736 return -EINVAL;
737 }
738 den = div_up(num, q);
739 if (den > rats[k].den_max)
740 continue;
741 if (den < rats[k].den_min)
742 den = rats[k].den_min;
743 else {
744 unsigned int r;
745 r = (den - rats[k].den_min) % rats[k].den_step;
746 if (r != 0)
747 den += rats[k].den_step - r;
748 }
749 diff = q * den - num;
750 if (best_num == 0 ||
751 diff * best_den < best_diff * den) {
752 best_diff = diff;
753 best_den = den;
754 best_num = num;
755 }
756 }
757 if (best_den == 0) {
758 i->empty = 1;
759 return -EINVAL;
760 }
761 t.max = div_up(best_num, best_den);
762 t.openmax = !!(best_num % best_den);
763 t.integer = 0;
764 err = snd_interval_refine(i, &t);
765 if (err < 0)
766 return err;
767
768 if (snd_interval_single(i)) {
769 if (nump)
770 *nump = best_num;
771 if (denp)
772 *denp = best_den;
773 }
774 return err;
775}
776
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200777EXPORT_SYMBOL(snd_interval_ratnum);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/**
780 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200781 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100782 * @rats_count: number of struct ratden
783 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200784 * @nump: pointer to store the resultant numerator
785 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
787 * Returns non-zero if the value is changed, zero if not changed.
788 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100789static int snd_interval_ratden(struct snd_interval *i,
790 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned int *nump, unsigned int *denp)
792{
793 unsigned int best_num, best_diff, best_den;
794 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100795 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int err;
797
798 best_num = best_den = best_diff = 0;
799 for (k = 0; k < rats_count; ++k) {
800 unsigned int num;
801 unsigned int den = rats[k].den;
802 unsigned int q = i->min;
803 int diff;
804 num = mul(q, den);
805 if (num > rats[k].num_max)
806 continue;
807 if (num < rats[k].num_min)
808 num = rats[k].num_max;
809 else {
810 unsigned int r;
811 r = (num - rats[k].num_min) % rats[k].num_step;
812 if (r != 0)
813 num += rats[k].num_step - r;
814 }
815 diff = num - q * den;
816 if (best_num == 0 ||
817 diff * best_den < best_diff * den) {
818 best_diff = diff;
819 best_den = den;
820 best_num = num;
821 }
822 }
823 if (best_den == 0) {
824 i->empty = 1;
825 return -EINVAL;
826 }
827 t.min = div_down(best_num, best_den);
828 t.openmin = !!(best_num % best_den);
829
830 best_num = best_den = best_diff = 0;
831 for (k = 0; k < rats_count; ++k) {
832 unsigned int num;
833 unsigned int den = rats[k].den;
834 unsigned int q = i->max;
835 int diff;
836 num = mul(q, den);
837 if (num < rats[k].num_min)
838 continue;
839 if (num > rats[k].num_max)
840 num = rats[k].num_max;
841 else {
842 unsigned int r;
843 r = (num - rats[k].num_min) % rats[k].num_step;
844 if (r != 0)
845 num -= r;
846 }
847 diff = q * den - num;
848 if (best_num == 0 ||
849 diff * best_den < best_diff * den) {
850 best_diff = diff;
851 best_den = den;
852 best_num = num;
853 }
854 }
855 if (best_den == 0) {
856 i->empty = 1;
857 return -EINVAL;
858 }
859 t.max = div_up(best_num, best_den);
860 t.openmax = !!(best_num % best_den);
861 t.integer = 0;
862 err = snd_interval_refine(i, &t);
863 if (err < 0)
864 return err;
865
866 if (snd_interval_single(i)) {
867 if (nump)
868 *nump = best_num;
869 if (denp)
870 *denp = best_den;
871 }
872 return err;
873}
874
875/**
876 * snd_interval_list - refine the interval value from the list
877 * @i: the interval value to refine
878 * @count: the number of elements in the list
879 * @list: the value list
880 * @mask: the bit-mask to evaluate
881 *
882 * Refines the interval value from the list.
883 * When mask is non-zero, only the elements corresponding to bit 1 are
884 * evaluated.
885 *
886 * Returns non-zero if the value is changed, zero if not changed.
887 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100888int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 unsigned int k;
891 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100892
893 if (!count) {
894 i->empty = 1;
895 return -EINVAL;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 for (k = 0; k < count; k++) {
898 if (mask && !(mask & (1 << k)))
899 continue;
900 if (i->min == list[k] && !i->openmin)
901 goto _l1;
902 if (i->min < list[k]) {
903 i->min = list[k];
904 i->openmin = 0;
905 changed = 1;
906 goto _l1;
907 }
908 }
909 i->empty = 1;
910 return -EINVAL;
911 _l1:
912 for (k = count; k-- > 0;) {
913 if (mask && !(mask & (1 << k)))
914 continue;
915 if (i->max == list[k] && !i->openmax)
916 goto _l2;
917 if (i->max > list[k]) {
918 i->max = list[k];
919 i->openmax = 0;
920 changed = 1;
921 goto _l2;
922 }
923 }
924 i->empty = 1;
925 return -EINVAL;
926 _l2:
927 if (snd_interval_checkempty(i)) {
928 i->empty = 1;
929 return -EINVAL;
930 }
931 return changed;
932}
933
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200934EXPORT_SYMBOL(snd_interval_list);
935
Takashi Iwai877211f2005-11-17 13:59:38 +0100936static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 unsigned int n;
939 int changed = 0;
940 n = (i->min - min) % step;
941 if (n != 0 || i->openmin) {
942 i->min += step - n;
943 changed = 1;
944 }
945 n = (i->max - min) % step;
946 if (n != 0 || i->openmax) {
947 i->max -= n;
948 changed = 1;
949 }
950 if (snd_interval_checkempty(i)) {
951 i->empty = 1;
952 return -EINVAL;
953 }
954 return changed;
955}
956
957/* Info constraints helpers */
958
959/**
960 * snd_pcm_hw_rule_add - add the hw-constraint rule
961 * @runtime: the pcm runtime instance
962 * @cond: condition bits
963 * @var: the variable to evaluate
964 * @func: the evaluation function
965 * @private: the private data pointer passed to function
966 * @dep: the dependent variables
967 *
968 * Returns zero if successful, or a negative error code on failure.
969 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100970int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int var,
972 snd_pcm_hw_rule_func_t func, void *private,
973 int dep, ...)
974{
Takashi Iwai877211f2005-11-17 13:59:38 +0100975 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
976 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned int k;
978 va_list args;
979 va_start(args, dep);
980 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100981 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned int new_rules = constrs->rules_all + 16;
983 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
984 if (!new)
985 return -ENOMEM;
986 if (constrs->rules) {
987 memcpy(new, constrs->rules,
988 constrs->rules_num * sizeof(*c));
989 kfree(constrs->rules);
990 }
991 constrs->rules = new;
992 constrs->rules_all = new_rules;
993 }
994 c = &constrs->rules[constrs->rules_num];
995 c->cond = cond;
996 c->func = func;
997 c->var = var;
998 c->private = private;
999 k = 0;
1000 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001001 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 c->deps[k++] = dep;
1004 if (dep < 0)
1005 break;
1006 dep = va_arg(args, int);
1007 }
1008 constrs->rules_num++;
1009 va_end(args);
1010 return 0;
1011}
1012
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001013EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001016 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001017 * @runtime: PCM runtime instance
1018 * @var: hw_params variable to apply the mask
1019 * @mask: the bitmap mask
1020 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001021 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001023int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 u_int32_t mask)
1025{
Takashi Iwai877211f2005-11-17 13:59:38 +01001026 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1027 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *maskp->bits &= mask;
1029 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1030 if (*maskp->bits == 0)
1031 return -EINVAL;
1032 return 0;
1033}
1034
1035/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001036 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001037 * @runtime: PCM runtime instance
1038 * @var: hw_params variable to apply the mask
1039 * @mask: the 64bit bitmap mask
1040 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001041 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001043int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 u_int64_t mask)
1045{
Takashi Iwai877211f2005-11-17 13:59:38 +01001046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1047 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 maskp->bits[0] &= (u_int32_t)mask;
1049 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1050 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1051 if (! maskp->bits[0] && ! maskp->bits[1])
1052 return -EINVAL;
1053 return 0;
1054}
1055
1056/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001057 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001058 * @runtime: PCM runtime instance
1059 * @var: hw_params variable to apply the integer constraint
1060 *
1061 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001063int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Takashi Iwai877211f2005-11-17 13:59:38 +01001065 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return snd_interval_setinteger(constrs_interval(constrs, var));
1067}
1068
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001069EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001072 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001073 * @runtime: PCM runtime instance
1074 * @var: hw_params variable to apply the range
1075 * @min: the minimal value
1076 * @max: the maximal value
1077 *
1078 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001080int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 unsigned int min, unsigned int max)
1082{
Takashi Iwai877211f2005-11-17 13:59:38 +01001083 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1084 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 t.min = min;
1086 t.max = max;
1087 t.openmin = t.openmax = 0;
1088 t.integer = 0;
1089 return snd_interval_refine(constrs_interval(constrs, var), &t);
1090}
1091
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001092EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1093
Takashi Iwai877211f2005-11-17 13:59:38 +01001094static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1095 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Takashi Iwai877211f2005-11-17 13:59:38 +01001097 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1099}
1100
1101
1102/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001103 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001104 * @runtime: PCM runtime instance
1105 * @cond: condition bits
1106 * @var: hw_params variable to apply the list constraint
1107 * @l: list
1108 *
1109 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001111int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned int cond,
1113 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001114 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 return snd_pcm_hw_rule_add(runtime, cond, var,
1117 snd_pcm_hw_rule_list, l,
1118 var, -1);
1119}
1120
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001121EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1122
Takashi Iwai877211f2005-11-17 13:59:38 +01001123static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1124 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Takashi Iwai877211f2005-11-17 13:59:38 +01001126 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 unsigned int num = 0, den = 0;
1128 int err;
1129 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1130 r->nrats, r->rats, &num, &den);
1131 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1132 params->rate_num = num;
1133 params->rate_den = den;
1134 }
1135 return err;
1136}
1137
1138/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001139 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001140 * @runtime: PCM runtime instance
1141 * @cond: condition bits
1142 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001143 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001145int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 unsigned int cond,
1147 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 return snd_pcm_hw_rule_add(runtime, cond, var,
1151 snd_pcm_hw_rule_ratnums, r,
1152 var, -1);
1153}
1154
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001155EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1156
Takashi Iwai877211f2005-11-17 13:59:38 +01001157static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1158 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Takashi Iwai877211f2005-11-17 13:59:38 +01001160 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned int num = 0, den = 0;
1162 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1163 r->nrats, r->rats, &num, &den);
1164 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1165 params->rate_num = num;
1166 params->rate_den = den;
1167 }
1168 return err;
1169}
1170
1171/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001172 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001173 * @runtime: PCM runtime instance
1174 * @cond: condition bits
1175 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001176 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001178int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned int cond,
1180 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001181 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 return snd_pcm_hw_rule_add(runtime, cond, var,
1184 snd_pcm_hw_rule_ratdens, r,
1185 var, -1);
1186}
1187
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001188EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1189
Takashi Iwai877211f2005-11-17 13:59:38 +01001190static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1191 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 unsigned int l = (unsigned long) rule->private;
1194 int width = l & 0xffff;
1195 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001196 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (snd_interval_single(i) && snd_interval_value(i) == width)
1198 params->msbits = msbits;
1199 return 0;
1200}
1201
1202/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001203 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001204 * @runtime: PCM runtime instance
1205 * @cond: condition bits
1206 * @width: sample bits width
1207 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001209int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 unsigned int cond,
1211 unsigned int width,
1212 unsigned int msbits)
1213{
1214 unsigned long l = (msbits << 16) | width;
1215 return snd_pcm_hw_rule_add(runtime, cond, -1,
1216 snd_pcm_hw_rule_msbits,
1217 (void*) l,
1218 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1219}
1220
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001221EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1222
Takashi Iwai877211f2005-11-17 13:59:38 +01001223static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1224 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 unsigned long step = (unsigned long) rule->private;
1227 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1228}
1229
1230/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001231 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001232 * @runtime: PCM runtime instance
1233 * @cond: condition bits
1234 * @var: hw_params variable to apply the step constraint
1235 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001237int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 unsigned int cond,
1239 snd_pcm_hw_param_t var,
1240 unsigned long step)
1241{
1242 return snd_pcm_hw_rule_add(runtime, cond, var,
1243 snd_pcm_hw_rule_step, (void *) step,
1244 var, -1);
1245}
1246
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001247EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1248
Takashi Iwai877211f2005-11-17 13:59:38 +01001249static 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 -07001250{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001251 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1253 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1254 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1255 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1256 };
1257 return snd_interval_list(hw_param_interval(params, rule->var),
1258 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1259}
1260
1261/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001262 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001263 * @runtime: PCM runtime instance
1264 * @cond: condition bits
1265 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001267int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned int cond,
1269 snd_pcm_hw_param_t var)
1270{
1271 return snd_pcm_hw_rule_add(runtime, cond, var,
1272 snd_pcm_hw_rule_pow2, NULL,
1273 var, -1);
1274}
1275
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001276EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1277
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001279 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 if (hw_is_mask(var)) {
1282 snd_mask_any(hw_param_mask(params, var));
1283 params->cmask |= 1 << var;
1284 params->rmask |= 1 << var;
1285 return;
1286 }
1287 if (hw_is_interval(var)) {
1288 snd_interval_any(hw_param_interval(params, var));
1289 params->cmask |= 1 << var;
1290 params->rmask |= 1 << var;
1291 return;
1292 }
1293 snd_BUG();
1294}
1295
Takashi Iwai877211f2005-11-17 13:59:38 +01001296void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 unsigned int k;
1299 memset(params, 0, sizeof(*params));
1300 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1301 _snd_pcm_hw_param_any(params, k);
1302 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1303 _snd_pcm_hw_param_any(params, k);
1304 params->info = ~0U;
1305}
1306
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001307EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001310 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001311 * @params: the hw_params instance
1312 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001313 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001315 * Return the value for field @var if it's fixed in configuration space
1316 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001318int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1319 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001322 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!snd_mask_single(mask))
1324 return -EINVAL;
1325 if (dir)
1326 *dir = 0;
1327 return snd_mask_value(mask);
1328 }
1329 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001330 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (!snd_interval_single(i))
1332 return -EINVAL;
1333 if (dir)
1334 *dir = i->openmin;
1335 return snd_interval_value(i);
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EINVAL;
1338}
1339
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001340EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 snd_pcm_hw_param_t var)
1344{
1345 if (hw_is_mask(var)) {
1346 snd_mask_none(hw_param_mask(params, var));
1347 params->cmask |= 1 << var;
1348 params->rmask |= 1 << var;
1349 } else if (hw_is_interval(var)) {
1350 snd_interval_none(hw_param_interval(params, var));
1351 params->cmask |= 1 << var;
1352 params->rmask |= 1 << var;
1353 } else {
1354 snd_BUG();
1355 }
1356}
1357
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001358EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Takashi Iwai877211f2005-11-17 13:59:38 +01001360static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001361 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int changed;
1364 if (hw_is_mask(var))
1365 changed = snd_mask_refine_first(hw_param_mask(params, var));
1366 else if (hw_is_interval(var))
1367 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001368 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (changed) {
1371 params->cmask |= 1 << var;
1372 params->rmask |= 1 << var;
1373 }
1374 return changed;
1375}
1376
1377
1378/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001379 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001380 * @pcm: PCM instance
1381 * @params: the hw_params instance
1382 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001383 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001385 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * values > minimum. Reduce configuration space accordingly.
1387 * Return the minimum.
1388 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001389int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1390 struct snd_pcm_hw_params *params,
1391 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 int changed = _snd_pcm_hw_param_first(params, var);
1394 if (changed < 0)
1395 return changed;
1396 if (params->rmask) {
1397 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001398 if (snd_BUG_ON(err < 0))
1399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 return snd_pcm_hw_param_value(params, var, dir);
1402}
1403
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001404EXPORT_SYMBOL(snd_pcm_hw_param_first);
1405
Takashi Iwai877211f2005-11-17 13:59:38 +01001406static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001407 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int changed;
1410 if (hw_is_mask(var))
1411 changed = snd_mask_refine_last(hw_param_mask(params, var));
1412 else if (hw_is_interval(var))
1413 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001414 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (changed) {
1417 params->cmask |= 1 << var;
1418 params->rmask |= 1 << var;
1419 }
1420 return changed;
1421}
1422
1423
1424/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001425 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001426 * @pcm: PCM instance
1427 * @params: the hw_params instance
1428 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001429 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001431 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * values < maximum. Reduce configuration space accordingly.
1433 * Return the maximum.
1434 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001435int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1436 struct snd_pcm_hw_params *params,
1437 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int changed = _snd_pcm_hw_param_last(params, var);
1440 if (changed < 0)
1441 return changed;
1442 if (params->rmask) {
1443 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001444 if (snd_BUG_ON(err < 0))
1445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 return snd_pcm_hw_param_value(params, var, dir);
1448}
1449
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001450EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001453 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001454 * @pcm: PCM instance
1455 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001457 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * The configuration chosen is that obtained fixing in this order:
1459 * first access, first format, first subformat, min channels,
1460 * min rate, min period time, max buffer size, min tick time
1461 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001462int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1463 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001465 static int vars[] = {
1466 SNDRV_PCM_HW_PARAM_ACCESS,
1467 SNDRV_PCM_HW_PARAM_FORMAT,
1468 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1469 SNDRV_PCM_HW_PARAM_CHANNELS,
1470 SNDRV_PCM_HW_PARAM_RATE,
1471 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1472 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1473 SNDRV_PCM_HW_PARAM_TICK_TIME,
1474 -1
1475 };
1476 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001478 for (v = vars; *v != -1; v++) {
1479 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1480 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1481 else
1482 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001483 if (snd_BUG_ON(err < 0))
1484 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487}
1488
Takashi Iwai877211f2005-11-17 13:59:38 +01001489static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 void *arg)
1491{
Takashi Iwai877211f2005-11-17 13:59:38 +01001492 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 unsigned long flags;
1494 snd_pcm_stream_lock_irqsave(substream, flags);
1495 if (snd_pcm_running(substream) &&
1496 snd_pcm_update_hw_ptr(substream) >= 0)
1497 runtime->status->hw_ptr %= runtime->buffer_size;
1498 else
1499 runtime->status->hw_ptr = 0;
1500 snd_pcm_stream_unlock_irqrestore(substream, flags);
1501 return 0;
1502}
1503
Takashi Iwai877211f2005-11-17 13:59:38 +01001504static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 void *arg)
1506{
Takashi Iwai877211f2005-11-17 13:59:38 +01001507 struct snd_pcm_channel_info *info = arg;
1508 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int width;
1510 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1511 info->offset = -1;
1512 return 0;
1513 }
1514 width = snd_pcm_format_physical_width(runtime->format);
1515 if (width < 0)
1516 return width;
1517 info->offset = 0;
1518 switch (runtime->access) {
1519 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1520 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1521 info->first = info->channel * width;
1522 info->step = runtime->channels * width;
1523 break;
1524 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1525 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1526 {
1527 size_t size = runtime->dma_bytes / runtime->channels;
1528 info->first = info->channel * size * 8;
1529 info->step = width;
1530 break;
1531 }
1532 default:
1533 snd_BUG();
1534 break;
1535 }
1536 return 0;
1537}
1538
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001539static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1540 void *arg)
1541{
1542 struct snd_pcm_hw_params *params = arg;
1543 snd_pcm_format_t format;
1544 int channels, width;
1545
1546 params->fifo_size = substream->runtime->hw.fifo_size;
1547 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1548 format = params_format(params);
1549 channels = params_channels(params);
1550 width = snd_pcm_format_physical_width(format);
1551 params->fifo_size /= width * channels;
1552 }
1553 return 0;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556/**
1557 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1558 * @substream: the pcm substream instance
1559 * @cmd: ioctl command
1560 * @arg: ioctl argument
1561 *
1562 * Processes the generic ioctl commands for PCM.
1563 * Can be passed as the ioctl callback for PCM ops.
1564 *
1565 * Returns zero if successful, or a negative error code on failure.
1566 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001567int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int cmd, void *arg)
1569{
1570 switch (cmd) {
1571 case SNDRV_PCM_IOCTL1_INFO:
1572 return 0;
1573 case SNDRV_PCM_IOCTL1_RESET:
1574 return snd_pcm_lib_ioctl_reset(substream, arg);
1575 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1576 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001577 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1578 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 return -ENXIO;
1581}
1582
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001583EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585/**
1586 * snd_pcm_period_elapsed - update the pcm status for the next period
1587 * @substream: the pcm substream instance
1588 *
1589 * This function is called from the interrupt handler when the
1590 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001591 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 *
1593 * Even if more than one periods have elapsed since the last call, you
1594 * have to call this only once.
1595 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001596void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai877211f2005-11-17 13:59:38 +01001598 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned long flags;
1600
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001601 if (PCM_RUNTIME_CHECK(substream))
1602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (runtime->transfer_ack_begin)
1606 runtime->transfer_ack_begin(substream);
1607
1608 snd_pcm_stream_lock_irqsave(substream, flags);
1609 if (!snd_pcm_running(substream) ||
1610 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1611 goto _end;
1612
1613 if (substream->timer_running)
1614 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 _end:
1616 snd_pcm_stream_unlock_irqrestore(substream, flags);
1617 if (runtime->transfer_ack_end)
1618 runtime->transfer_ack_end(substream);
1619 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1620}
1621
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001622EXPORT_SYMBOL(snd_pcm_period_elapsed);
1623
Takashi Iwai13075512008-01-08 18:08:14 +01001624/*
1625 * Wait until avail_min data becomes available
1626 * Returns a negative error code if any error occurs during operation.
1627 * The available space is stored on availp. When err = 0 and avail = 0
1628 * on the capture stream, it indicates the stream is in DRAINING state.
1629 */
1630static int wait_for_avail_min(struct snd_pcm_substream *substream,
1631 snd_pcm_uframes_t *availp)
1632{
1633 struct snd_pcm_runtime *runtime = substream->runtime;
1634 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1635 wait_queue_t wait;
1636 int err = 0;
1637 snd_pcm_uframes_t avail = 0;
1638 long tout;
1639
1640 init_waitqueue_entry(&wait, current);
1641 add_wait_queue(&runtime->sleep, &wait);
1642 for (;;) {
1643 if (signal_pending(current)) {
1644 err = -ERESTARTSYS;
1645 break;
1646 }
1647 set_current_state(TASK_INTERRUPTIBLE);
1648 snd_pcm_stream_unlock_irq(substream);
1649 tout = schedule_timeout(msecs_to_jiffies(10000));
1650 snd_pcm_stream_lock_irq(substream);
1651 switch (runtime->status->state) {
1652 case SNDRV_PCM_STATE_SUSPENDED:
1653 err = -ESTRPIPE;
1654 goto _endloop;
1655 case SNDRV_PCM_STATE_XRUN:
1656 err = -EPIPE;
1657 goto _endloop;
1658 case SNDRV_PCM_STATE_DRAINING:
1659 if (is_playback)
1660 err = -EPIPE;
1661 else
1662 avail = 0; /* indicate draining */
1663 goto _endloop;
1664 case SNDRV_PCM_STATE_OPEN:
1665 case SNDRV_PCM_STATE_SETUP:
1666 case SNDRV_PCM_STATE_DISCONNECTED:
1667 err = -EBADFD;
1668 goto _endloop;
1669 }
1670 if (!tout) {
1671 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1672 is_playback ? "playback" : "capture");
1673 err = -EIO;
1674 break;
1675 }
1676 if (is_playback)
1677 avail = snd_pcm_playback_avail(runtime);
1678 else
1679 avail = snd_pcm_capture_avail(runtime);
1680 if (avail >= runtime->control->avail_min)
1681 break;
1682 }
1683 _endloop:
1684 remove_wait_queue(&runtime->sleep, &wait);
1685 *availp = avail;
1686 return err;
1687}
1688
Takashi Iwai877211f2005-11-17 13:59:38 +01001689static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 unsigned int hwoff,
1691 unsigned long data, unsigned int off,
1692 snd_pcm_uframes_t frames)
1693{
Takashi Iwai877211f2005-11-17 13:59:38 +01001694 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int err;
1696 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1697 if (substream->ops->copy) {
1698 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1699 return err;
1700 } else {
1701 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1703 return -EFAULT;
1704 }
1705 return 0;
1706}
1707
Takashi Iwai877211f2005-11-17 13:59:38 +01001708typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 unsigned long data, unsigned int off,
1710 snd_pcm_uframes_t size);
1711
Takashi Iwai877211f2005-11-17 13:59:38 +01001712static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 unsigned long data,
1714 snd_pcm_uframes_t size,
1715 int nonblock,
1716 transfer_f transfer)
1717{
Takashi Iwai877211f2005-11-17 13:59:38 +01001718 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 snd_pcm_uframes_t xfer = 0;
1720 snd_pcm_uframes_t offset = 0;
1721 int err = 0;
1722
1723 if (size == 0)
1724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 snd_pcm_stream_lock_irq(substream);
1727 switch (runtime->status->state) {
1728 case SNDRV_PCM_STATE_PREPARED:
1729 case SNDRV_PCM_STATE_RUNNING:
1730 case SNDRV_PCM_STATE_PAUSED:
1731 break;
1732 case SNDRV_PCM_STATE_XRUN:
1733 err = -EPIPE;
1734 goto _end_unlock;
1735 case SNDRV_PCM_STATE_SUSPENDED:
1736 err = -ESTRPIPE;
1737 goto _end_unlock;
1738 default:
1739 err = -EBADFD;
1740 goto _end_unlock;
1741 }
1742
1743 while (size > 0) {
1744 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1745 snd_pcm_uframes_t avail;
1746 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001747 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 snd_pcm_update_hw_ptr(substream);
1749 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001750 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (nonblock) {
1752 err = -EAGAIN;
1753 goto _end_unlock;
1754 }
Takashi Iwai13075512008-01-08 18:08:14 +01001755 err = wait_for_avail_min(substream, &avail);
1756 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 frames = size > avail ? avail : size;
1760 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1761 if (frames > cont)
1762 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001763 if (snd_BUG_ON(!frames)) {
1764 snd_pcm_stream_unlock_irq(substream);
1765 return -EINVAL;
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 appl_ptr = runtime->control->appl_ptr;
1768 appl_ofs = appl_ptr % runtime->buffer_size;
1769 snd_pcm_stream_unlock_irq(substream);
1770 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1771 goto _end;
1772 snd_pcm_stream_lock_irq(substream);
1773 switch (runtime->status->state) {
1774 case SNDRV_PCM_STATE_XRUN:
1775 err = -EPIPE;
1776 goto _end_unlock;
1777 case SNDRV_PCM_STATE_SUSPENDED:
1778 err = -ESTRPIPE;
1779 goto _end_unlock;
1780 default:
1781 break;
1782 }
1783 appl_ptr += frames;
1784 if (appl_ptr >= runtime->boundary)
1785 appl_ptr -= runtime->boundary;
1786 runtime->control->appl_ptr = appl_ptr;
1787 if (substream->ops->ack)
1788 substream->ops->ack(substream);
1789
1790 offset += frames;
1791 size -= frames;
1792 xfer += frames;
1793 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1794 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1795 err = snd_pcm_start(substream);
1796 if (err < 0)
1797 goto _end_unlock;
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 _end_unlock:
1801 snd_pcm_stream_unlock_irq(substream);
1802 _end:
1803 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1804}
1805
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001806/* sanity-check for read/write methods */
1807static int pcm_sanity_check(struct snd_pcm_substream *substream)
1808{
1809 struct snd_pcm_runtime *runtime;
1810 if (PCM_RUNTIME_CHECK(substream))
1811 return -ENXIO;
1812 runtime = substream->runtime;
1813 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1814 return -EINVAL;
1815 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1816 return -EBADFD;
1817 return 0;
1818}
1819
Takashi Iwai877211f2005-11-17 13:59:38 +01001820snd_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 -07001821{
Takashi Iwai877211f2005-11-17 13:59:38 +01001822 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001824 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001826 err = pcm_sanity_check(substream);
1827 if (err < 0)
1828 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001830 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1833 runtime->channels > 1)
1834 return -EINVAL;
1835 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1836 snd_pcm_lib_write_transfer);
1837}
1838
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001839EXPORT_SYMBOL(snd_pcm_lib_write);
1840
Takashi Iwai877211f2005-11-17 13:59:38 +01001841static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 unsigned int hwoff,
1843 unsigned long data, unsigned int off,
1844 snd_pcm_uframes_t frames)
1845{
Takashi Iwai877211f2005-11-17 13:59:38 +01001846 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 int err;
1848 void __user **bufs = (void __user **)data;
1849 int channels = runtime->channels;
1850 int c;
1851 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001852 if (snd_BUG_ON(!substream->ops->silence))
1853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 for (c = 0; c < channels; ++c, ++bufs) {
1855 if (*bufs == NULL) {
1856 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1857 return err;
1858 } else {
1859 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1860 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1861 return err;
1862 }
1863 }
1864 } else {
1865 /* default transfer behaviour */
1866 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 for (c = 0; c < channels; ++c, ++bufs) {
1868 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1869 if (*bufs == NULL) {
1870 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1871 } else {
1872 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1873 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1874 return -EFAULT;
1875 }
1876 }
1877 }
1878 return 0;
1879}
1880
Takashi Iwai877211f2005-11-17 13:59:38 +01001881snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 void __user **bufs,
1883 snd_pcm_uframes_t frames)
1884{
Takashi Iwai877211f2005-11-17 13:59:38 +01001885 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001887 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001889 err = pcm_sanity_check(substream);
1890 if (err < 0)
1891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001893 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1896 return -EINVAL;
1897 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1898 nonblock, snd_pcm_lib_writev_transfer);
1899}
1900
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001901EXPORT_SYMBOL(snd_pcm_lib_writev);
1902
Takashi Iwai877211f2005-11-17 13:59:38 +01001903static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 unsigned int hwoff,
1905 unsigned long data, unsigned int off,
1906 snd_pcm_uframes_t frames)
1907{
Takashi Iwai877211f2005-11-17 13:59:38 +01001908 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int err;
1910 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1911 if (substream->ops->copy) {
1912 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1913 return err;
1914 } else {
1915 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1917 return -EFAULT;
1918 }
1919 return 0;
1920}
1921
Takashi Iwai877211f2005-11-17 13:59:38 +01001922static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 unsigned long data,
1924 snd_pcm_uframes_t size,
1925 int nonblock,
1926 transfer_f transfer)
1927{
Takashi Iwai877211f2005-11-17 13:59:38 +01001928 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 snd_pcm_uframes_t xfer = 0;
1930 snd_pcm_uframes_t offset = 0;
1931 int err = 0;
1932
1933 if (size == 0)
1934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 snd_pcm_stream_lock_irq(substream);
1937 switch (runtime->status->state) {
1938 case SNDRV_PCM_STATE_PREPARED:
1939 if (size >= runtime->start_threshold) {
1940 err = snd_pcm_start(substream);
1941 if (err < 0)
1942 goto _end_unlock;
1943 }
1944 break;
1945 case SNDRV_PCM_STATE_DRAINING:
1946 case SNDRV_PCM_STATE_RUNNING:
1947 case SNDRV_PCM_STATE_PAUSED:
1948 break;
1949 case SNDRV_PCM_STATE_XRUN:
1950 err = -EPIPE;
1951 goto _end_unlock;
1952 case SNDRV_PCM_STATE_SUSPENDED:
1953 err = -ESTRPIPE;
1954 goto _end_unlock;
1955 default:
1956 err = -EBADFD;
1957 goto _end_unlock;
1958 }
1959
1960 while (size > 0) {
1961 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1962 snd_pcm_uframes_t avail;
1963 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001964 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001967 if (!avail) {
1968 if (runtime->status->state ==
1969 SNDRV_PCM_STATE_DRAINING) {
1970 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto _end_unlock;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (nonblock) {
1974 err = -EAGAIN;
1975 goto _end_unlock;
1976 }
Takashi Iwai13075512008-01-08 18:08:14 +01001977 err = wait_for_avail_min(substream, &avail);
1978 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001980 if (!avail)
1981 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 frames = size > avail ? avail : size;
1984 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1985 if (frames > cont)
1986 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001987 if (snd_BUG_ON(!frames)) {
1988 snd_pcm_stream_unlock_irq(substream);
1989 return -EINVAL;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 appl_ptr = runtime->control->appl_ptr;
1992 appl_ofs = appl_ptr % runtime->buffer_size;
1993 snd_pcm_stream_unlock_irq(substream);
1994 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1995 goto _end;
1996 snd_pcm_stream_lock_irq(substream);
1997 switch (runtime->status->state) {
1998 case SNDRV_PCM_STATE_XRUN:
1999 err = -EPIPE;
2000 goto _end_unlock;
2001 case SNDRV_PCM_STATE_SUSPENDED:
2002 err = -ESTRPIPE;
2003 goto _end_unlock;
2004 default:
2005 break;
2006 }
2007 appl_ptr += frames;
2008 if (appl_ptr >= runtime->boundary)
2009 appl_ptr -= runtime->boundary;
2010 runtime->control->appl_ptr = appl_ptr;
2011 if (substream->ops->ack)
2012 substream->ops->ack(substream);
2013
2014 offset += frames;
2015 size -= frames;
2016 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 _end_unlock:
2019 snd_pcm_stream_unlock_irq(substream);
2020 _end:
2021 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2022}
2023
Takashi Iwai877211f2005-11-17 13:59:38 +01002024snd_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 -07002025{
Takashi Iwai877211f2005-11-17 13:59:38 +01002026 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002030 err = pcm_sanity_check(substream);
2031 if (err < 0)
2032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002034 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2036 return -EINVAL;
2037 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2038}
2039
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002040EXPORT_SYMBOL(snd_pcm_lib_read);
2041
Takashi Iwai877211f2005-11-17 13:59:38 +01002042static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 unsigned int hwoff,
2044 unsigned long data, unsigned int off,
2045 snd_pcm_uframes_t frames)
2046{
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int err;
2049 void __user **bufs = (void __user **)data;
2050 int channels = runtime->channels;
2051 int c;
2052 if (substream->ops->copy) {
2053 for (c = 0; c < channels; ++c, ++bufs) {
2054 char __user *buf;
2055 if (*bufs == NULL)
2056 continue;
2057 buf = *bufs + samples_to_bytes(runtime, off);
2058 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2059 return err;
2060 }
2061 } else {
2062 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 for (c = 0; c < channels; ++c, ++bufs) {
2064 char *hwbuf;
2065 char __user *buf;
2066 if (*bufs == NULL)
2067 continue;
2068
2069 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2070 buf = *bufs + samples_to_bytes(runtime, off);
2071 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2072 return -EFAULT;
2073 }
2074 }
2075 return 0;
2076}
2077
Takashi Iwai877211f2005-11-17 13:59:38 +01002078snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 void __user **bufs,
2080 snd_pcm_uframes_t frames)
2081{
Takashi Iwai877211f2005-11-17 13:59:38 +01002082 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002084 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002086 err = pcm_sanity_check(substream);
2087 if (err < 0)
2088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2091 return -EBADFD;
2092
Takashi Iwai0df63e42006-04-28 15:13:41 +02002093 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2095 return -EINVAL;
2096 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099EXPORT_SYMBOL(snd_pcm_lib_readv);