blob: adb306fd552535dddec197700c4bb7ce00501823 [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;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200266 if (hdelta < runtime->delay)
267 goto no_jiffies_check;
268 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200269 jdelta = jiffies - runtime->hw_ptr_jiffies;
270 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
271 delta = jdelta /
272 (((runtime->period_size * HZ) / runtime->rate)
273 + HZ/100);
274 hw_ptr_error(substream,
275 "hw_ptr skipping! [Q] "
276 "(pos=%ld, delta=%ld, period=%ld, "
277 "jdelta=%lu/%lu/%lu)\n",
278 (long)pos, (long)hdelta,
279 (long)runtime->period_size, jdelta,
280 ((hdelta * HZ) / runtime->rate), delta);
281 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
282 runtime->period_size * delta;
283 if (hw_ptr_interrupt >= runtime->boundary)
284 hw_ptr_interrupt -= runtime->boundary;
285 /* rebase to interrupt position */
286 hw_base = new_hw_ptr = hw_ptr_interrupt;
287 /* align hw_base to buffer_size */
288 hw_base -= hw_base % runtime->buffer_size;
289 delta = 0;
290 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200291 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200292 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100293 hw_ptr_error(substream,
294 "Lost interrupts? "
295 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
296 substream->stream, (long)delta,
297 (long)hw_ptr_interrupt);
298 /* rebase hw_ptr_interrupt */
299 hw_ptr_interrupt =
300 new_hw_ptr - new_hw_ptr % runtime->period_size;
301 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200302 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
305 runtime->silence_size > 0)
306 snd_pcm_playback_silence(substream, new_hw_ptr);
307
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200308 if (runtime->status->hw_ptr == new_hw_ptr)
309 return 0;
310
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100311 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200313 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200314 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
315 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return snd_pcm_update_hw_ptr_post(substream, runtime);
318}
319
320/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100321int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Takashi Iwai877211f2005-11-17 13:59:38 +0100323 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100325 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200327 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 old_hw_ptr = runtime->status->hw_ptr;
330 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
331 if (pos == SNDRV_PCM_POS_XRUN) {
332 xrun(substream);
333 return -EPIPE;
334 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100335 hw_base = runtime->hw_ptr_base;
336 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100338 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200339 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100340 if (delta < 0) {
341 delta += runtime->buffer_size;
342 if (delta < 0) {
343 hw_ptr_error(substream,
344 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200345 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100346 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200347 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100350 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100351 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100352 hw_base = 0;
353 new_hw_ptr = hw_base + pos;
354 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200355 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200356 if (!xrun_debug(substream, 4))
357 goto no_jiffies_check;
358 if (delta < runtime->delay)
359 goto no_jiffies_check;
360 delta -= runtime->delay;
361 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100362 hw_ptr_error(substream,
363 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200364 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100365 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200366 (long)runtime->period_size, jdelta,
367 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200370 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
372 runtime->silence_size > 0)
373 snd_pcm_playback_silence(substream, new_hw_ptr);
374
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200375 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200376 return 0;
377
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100378 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200380 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200381 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
382 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return snd_pcm_update_hw_ptr_post(substream, runtime);
385}
386
387/**
388 * snd_pcm_set_ops - set the PCM operators
389 * @pcm: the pcm instance
390 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
391 * @ops: the operator table
392 *
393 * Sets the given PCM operators to the pcm instance.
394 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100395void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwai877211f2005-11-17 13:59:38 +0100397 struct snd_pcm_str *stream = &pcm->streams[direction];
398 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 for (substream = stream->substream; substream != NULL; substream = substream->next)
401 substream->ops = ops;
402}
403
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200404EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/**
407 * snd_pcm_sync - set the PCM sync id
408 * @substream: the pcm substream
409 *
410 * Sets the PCM sync identifier for the card.
411 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100412void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Takashi Iwai877211f2005-11-17 13:59:38 +0100414 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 runtime->sync.id32[0] = substream->pcm->card->number;
417 runtime->sync.id32[1] = -1;
418 runtime->sync.id32[2] = -1;
419 runtime->sync.id32[3] = -1;
420}
421
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200422EXPORT_SYMBOL(snd_pcm_set_sync);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*
425 * Standard ioctl routine
426 */
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static inline unsigned int div32(unsigned int a, unsigned int b,
429 unsigned int *r)
430{
431 if (b == 0) {
432 *r = 0;
433 return UINT_MAX;
434 }
435 *r = a % b;
436 return a / b;
437}
438
439static inline unsigned int div_down(unsigned int a, unsigned int b)
440{
441 if (b == 0)
442 return UINT_MAX;
443 return a / b;
444}
445
446static inline unsigned int div_up(unsigned int a, unsigned int b)
447{
448 unsigned int r;
449 unsigned int q;
450 if (b == 0)
451 return UINT_MAX;
452 q = div32(a, b, &r);
453 if (r)
454 ++q;
455 return q;
456}
457
458static inline unsigned int mul(unsigned int a, unsigned int b)
459{
460 if (a == 0)
461 return 0;
462 if (div_down(UINT_MAX, a) < b)
463 return UINT_MAX;
464 return a * b;
465}
466
467static inline unsigned int muldiv32(unsigned int a, unsigned int b,
468 unsigned int c, unsigned int *r)
469{
470 u_int64_t n = (u_int64_t) a * b;
471 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200472 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *r = 0;
474 return UINT_MAX;
475 }
476 div64_32(&n, c, r);
477 if (n >= UINT_MAX) {
478 *r = 0;
479 return UINT_MAX;
480 }
481 return n;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/**
485 * snd_interval_refine - refine the interval value of configurator
486 * @i: the interval value to refine
487 * @v: the interval value to refer to
488 *
489 * Refines the interval value with the reference value.
490 * The interval is changed to the range satisfying both intervals.
491 * The interval status (min, max, integer, etc.) are evaluated.
492 *
493 * Returns non-zero if the value is changed, zero if not changed.
494 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100495int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200498 if (snd_BUG_ON(snd_interval_empty(i)))
499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (i->min < v->min) {
501 i->min = v->min;
502 i->openmin = v->openmin;
503 changed = 1;
504 } else if (i->min == v->min && !i->openmin && v->openmin) {
505 i->openmin = 1;
506 changed = 1;
507 }
508 if (i->max > v->max) {
509 i->max = v->max;
510 i->openmax = v->openmax;
511 changed = 1;
512 } else if (i->max == v->max && !i->openmax && v->openmax) {
513 i->openmax = 1;
514 changed = 1;
515 }
516 if (!i->integer && v->integer) {
517 i->integer = 1;
518 changed = 1;
519 }
520 if (i->integer) {
521 if (i->openmin) {
522 i->min++;
523 i->openmin = 0;
524 }
525 if (i->openmax) {
526 i->max--;
527 i->openmax = 0;
528 }
529 } else if (!i->openmin && !i->openmax && i->min == i->max)
530 i->integer = 1;
531 if (snd_interval_checkempty(i)) {
532 snd_interval_none(i);
533 return -EINVAL;
534 }
535 return changed;
536}
537
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200538EXPORT_SYMBOL(snd_interval_refine);
539
Takashi Iwai877211f2005-11-17 13:59:38 +0100540static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200542 if (snd_BUG_ON(snd_interval_empty(i)))
543 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (snd_interval_single(i))
545 return 0;
546 i->max = i->min;
547 i->openmax = i->openmin;
548 if (i->openmax)
549 i->max++;
550 return 1;
551}
552
Takashi Iwai877211f2005-11-17 13:59:38 +0100553static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200555 if (snd_BUG_ON(snd_interval_empty(i)))
556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (snd_interval_single(i))
558 return 0;
559 i->min = i->max;
560 i->openmin = i->openmax;
561 if (i->openmin)
562 i->min--;
563 return 1;
564}
565
Takashi Iwai877211f2005-11-17 13:59:38 +0100566void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 if (a->empty || b->empty) {
569 snd_interval_none(c);
570 return;
571 }
572 c->empty = 0;
573 c->min = mul(a->min, b->min);
574 c->openmin = (a->openmin || b->openmin);
575 c->max = mul(a->max, b->max);
576 c->openmax = (a->openmax || b->openmax);
577 c->integer = (a->integer && b->integer);
578}
579
580/**
581 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200582 * @a: dividend
583 * @b: divisor
584 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * c = a / b
587 *
588 * Returns non-zero if the value is changed, zero if not changed.
589 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100590void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 unsigned int r;
593 if (a->empty || b->empty) {
594 snd_interval_none(c);
595 return;
596 }
597 c->empty = 0;
598 c->min = div32(a->min, b->max, &r);
599 c->openmin = (r || a->openmin || b->openmax);
600 if (b->min > 0) {
601 c->max = div32(a->max, b->min, &r);
602 if (r) {
603 c->max++;
604 c->openmax = 1;
605 } else
606 c->openmax = (a->openmax || b->openmin);
607 } else {
608 c->max = UINT_MAX;
609 c->openmax = 0;
610 }
611 c->integer = 0;
612}
613
614/**
615 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200616 * @a: dividend 1
617 * @b: dividend 2
618 * @k: divisor (as integer)
619 * @c: result
620 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * c = a * b / k
622 *
623 * Returns non-zero if the value is changed, zero if not changed.
624 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100625void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
626 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 unsigned int r;
629 if (a->empty || b->empty) {
630 snd_interval_none(c);
631 return;
632 }
633 c->empty = 0;
634 c->min = muldiv32(a->min, b->min, k, &r);
635 c->openmin = (r || a->openmin || b->openmin);
636 c->max = muldiv32(a->max, b->max, k, &r);
637 if (r) {
638 c->max++;
639 c->openmax = 1;
640 } else
641 c->openmax = (a->openmax || b->openmax);
642 c->integer = 0;
643}
644
645/**
646 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200647 * @a: dividend 1
648 * @k: dividend 2 (as integer)
649 * @b: divisor
650 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
652 * c = a * k / b
653 *
654 * Returns non-zero if the value is changed, zero if not changed.
655 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100656void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
657 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 unsigned int r;
660 if (a->empty || b->empty) {
661 snd_interval_none(c);
662 return;
663 }
664 c->empty = 0;
665 c->min = muldiv32(a->min, k, b->max, &r);
666 c->openmin = (r || a->openmin || b->openmax);
667 if (b->min > 0) {
668 c->max = muldiv32(a->max, k, b->min, &r);
669 if (r) {
670 c->max++;
671 c->openmax = 1;
672 } else
673 c->openmax = (a->openmax || b->openmin);
674 } else {
675 c->max = UINT_MAX;
676 c->openmax = 0;
677 }
678 c->integer = 0;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/* ---- */
682
683
684/**
685 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200686 * @i: interval to refine
687 * @rats_count: number of ratnum_t
688 * @rats: ratnum_t array
689 * @nump: pointer to store the resultant numerator
690 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 *
692 * Returns non-zero if the value is changed, zero if not changed.
693 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100694int snd_interval_ratnum(struct snd_interval *i,
695 unsigned int rats_count, struct snd_ratnum *rats,
696 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 unsigned int best_num, best_diff, best_den;
699 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100700 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int err;
702
703 best_num = best_den = best_diff = 0;
704 for (k = 0; k < rats_count; ++k) {
705 unsigned int num = rats[k].num;
706 unsigned int den;
707 unsigned int q = i->min;
708 int diff;
709 if (q == 0)
710 q = 1;
711 den = div_down(num, q);
712 if (den < rats[k].den_min)
713 continue;
714 if (den > rats[k].den_max)
715 den = rats[k].den_max;
716 else {
717 unsigned int r;
718 r = (den - rats[k].den_min) % rats[k].den_step;
719 if (r != 0)
720 den -= r;
721 }
722 diff = num - q * den;
723 if (best_num == 0 ||
724 diff * best_den < best_diff * den) {
725 best_diff = diff;
726 best_den = den;
727 best_num = num;
728 }
729 }
730 if (best_den == 0) {
731 i->empty = 1;
732 return -EINVAL;
733 }
734 t.min = div_down(best_num, best_den);
735 t.openmin = !!(best_num % best_den);
736
737 best_num = best_den = best_diff = 0;
738 for (k = 0; k < rats_count; ++k) {
739 unsigned int num = rats[k].num;
740 unsigned int den;
741 unsigned int q = i->max;
742 int diff;
743 if (q == 0) {
744 i->empty = 1;
745 return -EINVAL;
746 }
747 den = div_up(num, q);
748 if (den > rats[k].den_max)
749 continue;
750 if (den < rats[k].den_min)
751 den = rats[k].den_min;
752 else {
753 unsigned int r;
754 r = (den - rats[k].den_min) % rats[k].den_step;
755 if (r != 0)
756 den += rats[k].den_step - r;
757 }
758 diff = q * den - num;
759 if (best_num == 0 ||
760 diff * best_den < best_diff * den) {
761 best_diff = diff;
762 best_den = den;
763 best_num = num;
764 }
765 }
766 if (best_den == 0) {
767 i->empty = 1;
768 return -EINVAL;
769 }
770 t.max = div_up(best_num, best_den);
771 t.openmax = !!(best_num % best_den);
772 t.integer = 0;
773 err = snd_interval_refine(i, &t);
774 if (err < 0)
775 return err;
776
777 if (snd_interval_single(i)) {
778 if (nump)
779 *nump = best_num;
780 if (denp)
781 *denp = best_den;
782 }
783 return err;
784}
785
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200786EXPORT_SYMBOL(snd_interval_ratnum);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/**
789 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200790 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100791 * @rats_count: number of struct ratden
792 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200793 * @nump: pointer to store the resultant numerator
794 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
796 * Returns non-zero if the value is changed, zero if not changed.
797 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100798static int snd_interval_ratden(struct snd_interval *i,
799 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 unsigned int *nump, unsigned int *denp)
801{
802 unsigned int best_num, best_diff, best_den;
803 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100804 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int err;
806
807 best_num = best_den = best_diff = 0;
808 for (k = 0; k < rats_count; ++k) {
809 unsigned int num;
810 unsigned int den = rats[k].den;
811 unsigned int q = i->min;
812 int diff;
813 num = mul(q, den);
814 if (num > rats[k].num_max)
815 continue;
816 if (num < rats[k].num_min)
817 num = rats[k].num_max;
818 else {
819 unsigned int r;
820 r = (num - rats[k].num_min) % rats[k].num_step;
821 if (r != 0)
822 num += rats[k].num_step - r;
823 }
824 diff = num - q * den;
825 if (best_num == 0 ||
826 diff * best_den < best_diff * den) {
827 best_diff = diff;
828 best_den = den;
829 best_num = num;
830 }
831 }
832 if (best_den == 0) {
833 i->empty = 1;
834 return -EINVAL;
835 }
836 t.min = div_down(best_num, best_den);
837 t.openmin = !!(best_num % best_den);
838
839 best_num = best_den = best_diff = 0;
840 for (k = 0; k < rats_count; ++k) {
841 unsigned int num;
842 unsigned int den = rats[k].den;
843 unsigned int q = i->max;
844 int diff;
845 num = mul(q, den);
846 if (num < rats[k].num_min)
847 continue;
848 if (num > rats[k].num_max)
849 num = rats[k].num_max;
850 else {
851 unsigned int r;
852 r = (num - rats[k].num_min) % rats[k].num_step;
853 if (r != 0)
854 num -= r;
855 }
856 diff = q * den - num;
857 if (best_num == 0 ||
858 diff * best_den < best_diff * den) {
859 best_diff = diff;
860 best_den = den;
861 best_num = num;
862 }
863 }
864 if (best_den == 0) {
865 i->empty = 1;
866 return -EINVAL;
867 }
868 t.max = div_up(best_num, best_den);
869 t.openmax = !!(best_num % best_den);
870 t.integer = 0;
871 err = snd_interval_refine(i, &t);
872 if (err < 0)
873 return err;
874
875 if (snd_interval_single(i)) {
876 if (nump)
877 *nump = best_num;
878 if (denp)
879 *denp = best_den;
880 }
881 return err;
882}
883
884/**
885 * snd_interval_list - refine the interval value from the list
886 * @i: the interval value to refine
887 * @count: the number of elements in the list
888 * @list: the value list
889 * @mask: the bit-mask to evaluate
890 *
891 * Refines the interval value from the list.
892 * When mask is non-zero, only the elements corresponding to bit 1 are
893 * evaluated.
894 *
895 * Returns non-zero if the value is changed, zero if not changed.
896 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100897int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 unsigned int k;
900 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100901
902 if (!count) {
903 i->empty = 1;
904 return -EINVAL;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 for (k = 0; k < count; k++) {
907 if (mask && !(mask & (1 << k)))
908 continue;
909 if (i->min == list[k] && !i->openmin)
910 goto _l1;
911 if (i->min < list[k]) {
912 i->min = list[k];
913 i->openmin = 0;
914 changed = 1;
915 goto _l1;
916 }
917 }
918 i->empty = 1;
919 return -EINVAL;
920 _l1:
921 for (k = count; k-- > 0;) {
922 if (mask && !(mask & (1 << k)))
923 continue;
924 if (i->max == list[k] && !i->openmax)
925 goto _l2;
926 if (i->max > list[k]) {
927 i->max = list[k];
928 i->openmax = 0;
929 changed = 1;
930 goto _l2;
931 }
932 }
933 i->empty = 1;
934 return -EINVAL;
935 _l2:
936 if (snd_interval_checkempty(i)) {
937 i->empty = 1;
938 return -EINVAL;
939 }
940 return changed;
941}
942
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200943EXPORT_SYMBOL(snd_interval_list);
944
Takashi Iwai877211f2005-11-17 13:59:38 +0100945static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 unsigned int n;
948 int changed = 0;
949 n = (i->min - min) % step;
950 if (n != 0 || i->openmin) {
951 i->min += step - n;
952 changed = 1;
953 }
954 n = (i->max - min) % step;
955 if (n != 0 || i->openmax) {
956 i->max -= n;
957 changed = 1;
958 }
959 if (snd_interval_checkempty(i)) {
960 i->empty = 1;
961 return -EINVAL;
962 }
963 return changed;
964}
965
966/* Info constraints helpers */
967
968/**
969 * snd_pcm_hw_rule_add - add the hw-constraint rule
970 * @runtime: the pcm runtime instance
971 * @cond: condition bits
972 * @var: the variable to evaluate
973 * @func: the evaluation function
974 * @private: the private data pointer passed to function
975 * @dep: the dependent variables
976 *
977 * Returns zero if successful, or a negative error code on failure.
978 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100979int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int var,
981 snd_pcm_hw_rule_func_t func, void *private,
982 int dep, ...)
983{
Takashi Iwai877211f2005-11-17 13:59:38 +0100984 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
985 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unsigned int k;
987 va_list args;
988 va_start(args, dep);
989 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100990 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned int new_rules = constrs->rules_all + 16;
992 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
993 if (!new)
994 return -ENOMEM;
995 if (constrs->rules) {
996 memcpy(new, constrs->rules,
997 constrs->rules_num * sizeof(*c));
998 kfree(constrs->rules);
999 }
1000 constrs->rules = new;
1001 constrs->rules_all = new_rules;
1002 }
1003 c = &constrs->rules[constrs->rules_num];
1004 c->cond = cond;
1005 c->func = func;
1006 c->var = var;
1007 c->private = private;
1008 k = 0;
1009 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001010 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 c->deps[k++] = dep;
1013 if (dep < 0)
1014 break;
1015 dep = va_arg(args, int);
1016 }
1017 constrs->rules_num++;
1018 va_end(args);
1019 return 0;
1020}
1021
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001022EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001025 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001026 * @runtime: PCM runtime instance
1027 * @var: hw_params variable to apply the mask
1028 * @mask: the bitmap mask
1029 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001030 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001032int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 u_int32_t mask)
1034{
Takashi Iwai877211f2005-11-17 13:59:38 +01001035 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1036 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 *maskp->bits &= mask;
1038 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1039 if (*maskp->bits == 0)
1040 return -EINVAL;
1041 return 0;
1042}
1043
1044/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001045 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001046 * @runtime: PCM runtime instance
1047 * @var: hw_params variable to apply the mask
1048 * @mask: the 64bit bitmap mask
1049 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001050 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001052int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 u_int64_t mask)
1054{
Takashi Iwai877211f2005-11-17 13:59:38 +01001055 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1056 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 maskp->bits[0] &= (u_int32_t)mask;
1058 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1059 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1060 if (! maskp->bits[0] && ! maskp->bits[1])
1061 return -EINVAL;
1062 return 0;
1063}
1064
1065/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001066 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001067 * @runtime: PCM runtime instance
1068 * @var: hw_params variable to apply the integer constraint
1069 *
1070 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001072int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return snd_interval_setinteger(constrs_interval(constrs, var));
1076}
1077
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001078EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001081 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001082 * @runtime: PCM runtime instance
1083 * @var: hw_params variable to apply the range
1084 * @min: the minimal value
1085 * @max: the maximal value
1086 *
1087 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001089int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unsigned int min, unsigned int max)
1091{
Takashi Iwai877211f2005-11-17 13:59:38 +01001092 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1093 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 t.min = min;
1095 t.max = max;
1096 t.openmin = t.openmax = 0;
1097 t.integer = 0;
1098 return snd_interval_refine(constrs_interval(constrs, var), &t);
1099}
1100
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001101EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1102
Takashi Iwai877211f2005-11-17 13:59:38 +01001103static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1104 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Takashi Iwai877211f2005-11-17 13:59:38 +01001106 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1108}
1109
1110
1111/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001112 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001113 * @runtime: PCM runtime instance
1114 * @cond: condition bits
1115 * @var: hw_params variable to apply the list constraint
1116 * @l: list
1117 *
1118 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001120int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned int cond,
1122 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001123 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 return snd_pcm_hw_rule_add(runtime, cond, var,
1126 snd_pcm_hw_rule_list, l,
1127 var, -1);
1128}
1129
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001130EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1131
Takashi Iwai877211f2005-11-17 13:59:38 +01001132static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1133 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 unsigned int num = 0, den = 0;
1137 int err;
1138 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1139 r->nrats, r->rats, &num, &den);
1140 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1141 params->rate_num = num;
1142 params->rate_den = den;
1143 }
1144 return err;
1145}
1146
1147/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001148 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001149 * @runtime: PCM runtime instance
1150 * @cond: condition bits
1151 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001152 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001154int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned int cond,
1156 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 return snd_pcm_hw_rule_add(runtime, cond, var,
1160 snd_pcm_hw_rule_ratnums, r,
1161 var, -1);
1162}
1163
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001164EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1165
Takashi Iwai877211f2005-11-17 13:59:38 +01001166static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 unsigned int num = 0, den = 0;
1171 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1172 r->nrats, r->rats, &num, &den);
1173 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1174 params->rate_num = num;
1175 params->rate_den = den;
1176 }
1177 return err;
1178}
1179
1180/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001181 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001182 * @runtime: PCM runtime instance
1183 * @cond: condition bits
1184 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001185 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001187int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 unsigned int cond,
1189 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001190 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 return snd_pcm_hw_rule_add(runtime, cond, var,
1193 snd_pcm_hw_rule_ratdens, r,
1194 var, -1);
1195}
1196
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001197EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1198
Takashi Iwai877211f2005-11-17 13:59:38 +01001199static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1200 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 unsigned int l = (unsigned long) rule->private;
1203 int width = l & 0xffff;
1204 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001205 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (snd_interval_single(i) && snd_interval_value(i) == width)
1207 params->msbits = msbits;
1208 return 0;
1209}
1210
1211/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001212 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @width: sample bits width
1216 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001218int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned int cond,
1220 unsigned int width,
1221 unsigned int msbits)
1222{
1223 unsigned long l = (msbits << 16) | width;
1224 return snd_pcm_hw_rule_add(runtime, cond, -1,
1225 snd_pcm_hw_rule_msbits,
1226 (void*) l,
1227 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1228}
1229
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001230EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1231
Takashi Iwai877211f2005-11-17 13:59:38 +01001232static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1233 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 unsigned long step = (unsigned long) rule->private;
1236 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1237}
1238
1239/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001240 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001241 * @runtime: PCM runtime instance
1242 * @cond: condition bits
1243 * @var: hw_params variable to apply the step constraint
1244 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001246int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned int cond,
1248 snd_pcm_hw_param_t var,
1249 unsigned long step)
1250{
1251 return snd_pcm_hw_rule_add(runtime, cond, var,
1252 snd_pcm_hw_rule_step, (void *) step,
1253 var, -1);
1254}
1255
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001256EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1257
Takashi Iwai877211f2005-11-17 13:59:38 +01001258static 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 -07001259{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001260 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1262 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1263 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1264 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1265 };
1266 return snd_interval_list(hw_param_interval(params, rule->var),
1267 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1268}
1269
1270/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001271 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001272 * @runtime: PCM runtime instance
1273 * @cond: condition bits
1274 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001276int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 unsigned int cond,
1278 snd_pcm_hw_param_t var)
1279{
1280 return snd_pcm_hw_rule_add(runtime, cond, var,
1281 snd_pcm_hw_rule_pow2, NULL,
1282 var, -1);
1283}
1284
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001285EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1286
Takashi Iwai877211f2005-11-17 13:59:38 +01001287static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001288 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 if (hw_is_mask(var)) {
1291 snd_mask_any(hw_param_mask(params, var));
1292 params->cmask |= 1 << var;
1293 params->rmask |= 1 << var;
1294 return;
1295 }
1296 if (hw_is_interval(var)) {
1297 snd_interval_any(hw_param_interval(params, var));
1298 params->cmask |= 1 << var;
1299 params->rmask |= 1 << var;
1300 return;
1301 }
1302 snd_BUG();
1303}
1304
Takashi Iwai877211f2005-11-17 13:59:38 +01001305void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 unsigned int k;
1308 memset(params, 0, sizeof(*params));
1309 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1310 _snd_pcm_hw_param_any(params, k);
1311 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1312 _snd_pcm_hw_param_any(params, k);
1313 params->info = ~0U;
1314}
1315
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001316EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001319 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001320 * @params: the hw_params instance
1321 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001322 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001324 * Return the value for field @var if it's fixed in configuration space
1325 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001327int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1328 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001331 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!snd_mask_single(mask))
1333 return -EINVAL;
1334 if (dir)
1335 *dir = 0;
1336 return snd_mask_value(mask);
1337 }
1338 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001339 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!snd_interval_single(i))
1341 return -EINVAL;
1342 if (dir)
1343 *dir = i->openmin;
1344 return snd_interval_value(i);
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EINVAL;
1347}
1348
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001349EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 snd_pcm_hw_param_t var)
1353{
1354 if (hw_is_mask(var)) {
1355 snd_mask_none(hw_param_mask(params, var));
1356 params->cmask |= 1 << var;
1357 params->rmask |= 1 << var;
1358 } else if (hw_is_interval(var)) {
1359 snd_interval_none(hw_param_interval(params, var));
1360 params->cmask |= 1 << var;
1361 params->rmask |= 1 << var;
1362 } else {
1363 snd_BUG();
1364 }
1365}
1366
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001367EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Takashi Iwai877211f2005-11-17 13:59:38 +01001369static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001370 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int changed;
1373 if (hw_is_mask(var))
1374 changed = snd_mask_refine_first(hw_param_mask(params, var));
1375 else if (hw_is_interval(var))
1376 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001377 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (changed) {
1380 params->cmask |= 1 << var;
1381 params->rmask |= 1 << var;
1382 }
1383 return changed;
1384}
1385
1386
1387/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001388 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001389 * @pcm: PCM instance
1390 * @params: the hw_params instance
1391 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001392 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001394 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * values > minimum. Reduce configuration space accordingly.
1396 * Return the minimum.
1397 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001398int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1399 struct snd_pcm_hw_params *params,
1400 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 int changed = _snd_pcm_hw_param_first(params, var);
1403 if (changed < 0)
1404 return changed;
1405 if (params->rmask) {
1406 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001407 if (snd_BUG_ON(err < 0))
1408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 return snd_pcm_hw_param_value(params, var, dir);
1411}
1412
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001413EXPORT_SYMBOL(snd_pcm_hw_param_first);
1414
Takashi Iwai877211f2005-11-17 13:59:38 +01001415static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001416 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 int changed;
1419 if (hw_is_mask(var))
1420 changed = snd_mask_refine_last(hw_param_mask(params, var));
1421 else if (hw_is_interval(var))
1422 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001423 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (changed) {
1426 params->cmask |= 1 << var;
1427 params->rmask |= 1 << var;
1428 }
1429 return changed;
1430}
1431
1432
1433/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001435 * @pcm: PCM instance
1436 * @params: the hw_params instance
1437 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001438 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001440 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * values < maximum. Reduce configuration space accordingly.
1442 * Return the maximum.
1443 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001444int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1445 struct snd_pcm_hw_params *params,
1446 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 int changed = _snd_pcm_hw_param_last(params, var);
1449 if (changed < 0)
1450 return changed;
1451 if (params->rmask) {
1452 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001453 if (snd_BUG_ON(err < 0))
1454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 return snd_pcm_hw_param_value(params, var, dir);
1457}
1458
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001459EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001462 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001463 * @pcm: PCM instance
1464 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001466 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * The configuration chosen is that obtained fixing in this order:
1468 * first access, first format, first subformat, min channels,
1469 * min rate, min period time, max buffer size, min tick time
1470 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001471int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1472 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001474 static int vars[] = {
1475 SNDRV_PCM_HW_PARAM_ACCESS,
1476 SNDRV_PCM_HW_PARAM_FORMAT,
1477 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1478 SNDRV_PCM_HW_PARAM_CHANNELS,
1479 SNDRV_PCM_HW_PARAM_RATE,
1480 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1481 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1482 SNDRV_PCM_HW_PARAM_TICK_TIME,
1483 -1
1484 };
1485 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001487 for (v = vars; *v != -1; v++) {
1488 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1489 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1490 else
1491 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001492 if (snd_BUG_ON(err < 0))
1493 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
1497
Takashi Iwai877211f2005-11-17 13:59:38 +01001498static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 void *arg)
1500{
Takashi Iwai877211f2005-11-17 13:59:38 +01001501 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 unsigned long flags;
1503 snd_pcm_stream_lock_irqsave(substream, flags);
1504 if (snd_pcm_running(substream) &&
1505 snd_pcm_update_hw_ptr(substream) >= 0)
1506 runtime->status->hw_ptr %= runtime->buffer_size;
1507 else
1508 runtime->status->hw_ptr = 0;
1509 snd_pcm_stream_unlock_irqrestore(substream, flags);
1510 return 0;
1511}
1512
Takashi Iwai877211f2005-11-17 13:59:38 +01001513static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 void *arg)
1515{
Takashi Iwai877211f2005-11-17 13:59:38 +01001516 struct snd_pcm_channel_info *info = arg;
1517 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int width;
1519 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1520 info->offset = -1;
1521 return 0;
1522 }
1523 width = snd_pcm_format_physical_width(runtime->format);
1524 if (width < 0)
1525 return width;
1526 info->offset = 0;
1527 switch (runtime->access) {
1528 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1529 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1530 info->first = info->channel * width;
1531 info->step = runtime->channels * width;
1532 break;
1533 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1534 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1535 {
1536 size_t size = runtime->dma_bytes / runtime->channels;
1537 info->first = info->channel * size * 8;
1538 info->step = width;
1539 break;
1540 }
1541 default:
1542 snd_BUG();
1543 break;
1544 }
1545 return 0;
1546}
1547
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001548static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1549 void *arg)
1550{
1551 struct snd_pcm_hw_params *params = arg;
1552 snd_pcm_format_t format;
1553 int channels, width;
1554
1555 params->fifo_size = substream->runtime->hw.fifo_size;
1556 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1557 format = params_format(params);
1558 channels = params_channels(params);
1559 width = snd_pcm_format_physical_width(format);
1560 params->fifo_size /= width * channels;
1561 }
1562 return 0;
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/**
1566 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1567 * @substream: the pcm substream instance
1568 * @cmd: ioctl command
1569 * @arg: ioctl argument
1570 *
1571 * Processes the generic ioctl commands for PCM.
1572 * Can be passed as the ioctl callback for PCM ops.
1573 *
1574 * Returns zero if successful, or a negative error code on failure.
1575 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001576int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 unsigned int cmd, void *arg)
1578{
1579 switch (cmd) {
1580 case SNDRV_PCM_IOCTL1_INFO:
1581 return 0;
1582 case SNDRV_PCM_IOCTL1_RESET:
1583 return snd_pcm_lib_ioctl_reset(substream, arg);
1584 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1585 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001586 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1587 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 return -ENXIO;
1590}
1591
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001592EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/**
1595 * snd_pcm_period_elapsed - update the pcm status for the next period
1596 * @substream: the pcm substream instance
1597 *
1598 * This function is called from the interrupt handler when the
1599 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001600 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 *
1602 * Even if more than one periods have elapsed since the last call, you
1603 * have to call this only once.
1604 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001605void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Takashi Iwai877211f2005-11-17 13:59:38 +01001607 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 unsigned long flags;
1609
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001610 if (PCM_RUNTIME_CHECK(substream))
1611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (runtime->transfer_ack_begin)
1615 runtime->transfer_ack_begin(substream);
1616
1617 snd_pcm_stream_lock_irqsave(substream, flags);
1618 if (!snd_pcm_running(substream) ||
1619 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1620 goto _end;
1621
1622 if (substream->timer_running)
1623 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 _end:
1625 snd_pcm_stream_unlock_irqrestore(substream, flags);
1626 if (runtime->transfer_ack_end)
1627 runtime->transfer_ack_end(substream);
1628 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1629}
1630
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001631EXPORT_SYMBOL(snd_pcm_period_elapsed);
1632
Takashi Iwai13075512008-01-08 18:08:14 +01001633/*
1634 * Wait until avail_min data becomes available
1635 * Returns a negative error code if any error occurs during operation.
1636 * The available space is stored on availp. When err = 0 and avail = 0
1637 * on the capture stream, it indicates the stream is in DRAINING state.
1638 */
1639static int wait_for_avail_min(struct snd_pcm_substream *substream,
1640 snd_pcm_uframes_t *availp)
1641{
1642 struct snd_pcm_runtime *runtime = substream->runtime;
1643 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1644 wait_queue_t wait;
1645 int err = 0;
1646 snd_pcm_uframes_t avail = 0;
1647 long tout;
1648
1649 init_waitqueue_entry(&wait, current);
1650 add_wait_queue(&runtime->sleep, &wait);
1651 for (;;) {
1652 if (signal_pending(current)) {
1653 err = -ERESTARTSYS;
1654 break;
1655 }
1656 set_current_state(TASK_INTERRUPTIBLE);
1657 snd_pcm_stream_unlock_irq(substream);
1658 tout = schedule_timeout(msecs_to_jiffies(10000));
1659 snd_pcm_stream_lock_irq(substream);
1660 switch (runtime->status->state) {
1661 case SNDRV_PCM_STATE_SUSPENDED:
1662 err = -ESTRPIPE;
1663 goto _endloop;
1664 case SNDRV_PCM_STATE_XRUN:
1665 err = -EPIPE;
1666 goto _endloop;
1667 case SNDRV_PCM_STATE_DRAINING:
1668 if (is_playback)
1669 err = -EPIPE;
1670 else
1671 avail = 0; /* indicate draining */
1672 goto _endloop;
1673 case SNDRV_PCM_STATE_OPEN:
1674 case SNDRV_PCM_STATE_SETUP:
1675 case SNDRV_PCM_STATE_DISCONNECTED:
1676 err = -EBADFD;
1677 goto _endloop;
1678 }
1679 if (!tout) {
1680 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1681 is_playback ? "playback" : "capture");
1682 err = -EIO;
1683 break;
1684 }
1685 if (is_playback)
1686 avail = snd_pcm_playback_avail(runtime);
1687 else
1688 avail = snd_pcm_capture_avail(runtime);
1689 if (avail >= runtime->control->avail_min)
1690 break;
1691 }
1692 _endloop:
1693 remove_wait_queue(&runtime->sleep, &wait);
1694 *availp = avail;
1695 return err;
1696}
1697
Takashi Iwai877211f2005-11-17 13:59:38 +01001698static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 unsigned int hwoff,
1700 unsigned long data, unsigned int off,
1701 snd_pcm_uframes_t frames)
1702{
Takashi Iwai877211f2005-11-17 13:59:38 +01001703 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int err;
1705 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1706 if (substream->ops->copy) {
1707 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1708 return err;
1709 } else {
1710 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1712 return -EFAULT;
1713 }
1714 return 0;
1715}
1716
Takashi Iwai877211f2005-11-17 13:59:38 +01001717typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 unsigned long data, unsigned int off,
1719 snd_pcm_uframes_t size);
1720
Takashi Iwai877211f2005-11-17 13:59:38 +01001721static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 unsigned long data,
1723 snd_pcm_uframes_t size,
1724 int nonblock,
1725 transfer_f transfer)
1726{
Takashi Iwai877211f2005-11-17 13:59:38 +01001727 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 snd_pcm_uframes_t xfer = 0;
1729 snd_pcm_uframes_t offset = 0;
1730 int err = 0;
1731
1732 if (size == 0)
1733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 snd_pcm_stream_lock_irq(substream);
1736 switch (runtime->status->state) {
1737 case SNDRV_PCM_STATE_PREPARED:
1738 case SNDRV_PCM_STATE_RUNNING:
1739 case SNDRV_PCM_STATE_PAUSED:
1740 break;
1741 case SNDRV_PCM_STATE_XRUN:
1742 err = -EPIPE;
1743 goto _end_unlock;
1744 case SNDRV_PCM_STATE_SUSPENDED:
1745 err = -ESTRPIPE;
1746 goto _end_unlock;
1747 default:
1748 err = -EBADFD;
1749 goto _end_unlock;
1750 }
1751
1752 while (size > 0) {
1753 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1754 snd_pcm_uframes_t avail;
1755 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001756 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 snd_pcm_update_hw_ptr(substream);
1758 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001759 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (nonblock) {
1761 err = -EAGAIN;
1762 goto _end_unlock;
1763 }
Takashi Iwai13075512008-01-08 18:08:14 +01001764 err = wait_for_avail_min(substream, &avail);
1765 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 frames = size > avail ? avail : size;
1769 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1770 if (frames > cont)
1771 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001772 if (snd_BUG_ON(!frames)) {
1773 snd_pcm_stream_unlock_irq(substream);
1774 return -EINVAL;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 appl_ptr = runtime->control->appl_ptr;
1777 appl_ofs = appl_ptr % runtime->buffer_size;
1778 snd_pcm_stream_unlock_irq(substream);
1779 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1780 goto _end;
1781 snd_pcm_stream_lock_irq(substream);
1782 switch (runtime->status->state) {
1783 case SNDRV_PCM_STATE_XRUN:
1784 err = -EPIPE;
1785 goto _end_unlock;
1786 case SNDRV_PCM_STATE_SUSPENDED:
1787 err = -ESTRPIPE;
1788 goto _end_unlock;
1789 default:
1790 break;
1791 }
1792 appl_ptr += frames;
1793 if (appl_ptr >= runtime->boundary)
1794 appl_ptr -= runtime->boundary;
1795 runtime->control->appl_ptr = appl_ptr;
1796 if (substream->ops->ack)
1797 substream->ops->ack(substream);
1798
1799 offset += frames;
1800 size -= frames;
1801 xfer += frames;
1802 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1803 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1804 err = snd_pcm_start(substream);
1805 if (err < 0)
1806 goto _end_unlock;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 _end_unlock:
1810 snd_pcm_stream_unlock_irq(substream);
1811 _end:
1812 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1813}
1814
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001815/* sanity-check for read/write methods */
1816static int pcm_sanity_check(struct snd_pcm_substream *substream)
1817{
1818 struct snd_pcm_runtime *runtime;
1819 if (PCM_RUNTIME_CHECK(substream))
1820 return -ENXIO;
1821 runtime = substream->runtime;
1822 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1823 return -EINVAL;
1824 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1825 return -EBADFD;
1826 return 0;
1827}
1828
Takashi Iwai877211f2005-11-17 13:59:38 +01001829snd_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 -07001830{
Takashi Iwai877211f2005-11-17 13:59:38 +01001831 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001833 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001835 err = pcm_sanity_check(substream);
1836 if (err < 0)
1837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001839 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1842 runtime->channels > 1)
1843 return -EINVAL;
1844 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1845 snd_pcm_lib_write_transfer);
1846}
1847
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001848EXPORT_SYMBOL(snd_pcm_lib_write);
1849
Takashi Iwai877211f2005-11-17 13:59:38 +01001850static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 unsigned int hwoff,
1852 unsigned long data, unsigned int off,
1853 snd_pcm_uframes_t frames)
1854{
Takashi Iwai877211f2005-11-17 13:59:38 +01001855 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int err;
1857 void __user **bufs = (void __user **)data;
1858 int channels = runtime->channels;
1859 int c;
1860 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001861 if (snd_BUG_ON(!substream->ops->silence))
1862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 for (c = 0; c < channels; ++c, ++bufs) {
1864 if (*bufs == NULL) {
1865 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1866 return err;
1867 } else {
1868 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1869 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1870 return err;
1871 }
1872 }
1873 } else {
1874 /* default transfer behaviour */
1875 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 for (c = 0; c < channels; ++c, ++bufs) {
1877 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1878 if (*bufs == NULL) {
1879 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1880 } else {
1881 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1882 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1883 return -EFAULT;
1884 }
1885 }
1886 }
1887 return 0;
1888}
1889
Takashi Iwai877211f2005-11-17 13:59:38 +01001890snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 void __user **bufs,
1892 snd_pcm_uframes_t frames)
1893{
Takashi Iwai877211f2005-11-17 13:59:38 +01001894 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001896 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001898 err = pcm_sanity_check(substream);
1899 if (err < 0)
1900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001902 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1905 return -EINVAL;
1906 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1907 nonblock, snd_pcm_lib_writev_transfer);
1908}
1909
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001910EXPORT_SYMBOL(snd_pcm_lib_writev);
1911
Takashi Iwai877211f2005-11-17 13:59:38 +01001912static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 unsigned int hwoff,
1914 unsigned long data, unsigned int off,
1915 snd_pcm_uframes_t frames)
1916{
Takashi Iwai877211f2005-11-17 13:59:38 +01001917 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 int err;
1919 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1920 if (substream->ops->copy) {
1921 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1922 return err;
1923 } else {
1924 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1926 return -EFAULT;
1927 }
1928 return 0;
1929}
1930
Takashi Iwai877211f2005-11-17 13:59:38 +01001931static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 unsigned long data,
1933 snd_pcm_uframes_t size,
1934 int nonblock,
1935 transfer_f transfer)
1936{
Takashi Iwai877211f2005-11-17 13:59:38 +01001937 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 snd_pcm_uframes_t xfer = 0;
1939 snd_pcm_uframes_t offset = 0;
1940 int err = 0;
1941
1942 if (size == 0)
1943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 snd_pcm_stream_lock_irq(substream);
1946 switch (runtime->status->state) {
1947 case SNDRV_PCM_STATE_PREPARED:
1948 if (size >= runtime->start_threshold) {
1949 err = snd_pcm_start(substream);
1950 if (err < 0)
1951 goto _end_unlock;
1952 }
1953 break;
1954 case SNDRV_PCM_STATE_DRAINING:
1955 case SNDRV_PCM_STATE_RUNNING:
1956 case SNDRV_PCM_STATE_PAUSED:
1957 break;
1958 case SNDRV_PCM_STATE_XRUN:
1959 err = -EPIPE;
1960 goto _end_unlock;
1961 case SNDRV_PCM_STATE_SUSPENDED:
1962 err = -ESTRPIPE;
1963 goto _end_unlock;
1964 default:
1965 err = -EBADFD;
1966 goto _end_unlock;
1967 }
1968
1969 while (size > 0) {
1970 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1971 snd_pcm_uframes_t avail;
1972 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001973 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001976 if (!avail) {
1977 if (runtime->status->state ==
1978 SNDRV_PCM_STATE_DRAINING) {
1979 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 goto _end_unlock;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (nonblock) {
1983 err = -EAGAIN;
1984 goto _end_unlock;
1985 }
Takashi Iwai13075512008-01-08 18:08:14 +01001986 err = wait_for_avail_min(substream, &avail);
1987 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001989 if (!avail)
1990 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 frames = size > avail ? avail : size;
1993 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1994 if (frames > cont)
1995 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001996 if (snd_BUG_ON(!frames)) {
1997 snd_pcm_stream_unlock_irq(substream);
1998 return -EINVAL;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 appl_ptr = runtime->control->appl_ptr;
2001 appl_ofs = appl_ptr % runtime->buffer_size;
2002 snd_pcm_stream_unlock_irq(substream);
2003 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2004 goto _end;
2005 snd_pcm_stream_lock_irq(substream);
2006 switch (runtime->status->state) {
2007 case SNDRV_PCM_STATE_XRUN:
2008 err = -EPIPE;
2009 goto _end_unlock;
2010 case SNDRV_PCM_STATE_SUSPENDED:
2011 err = -ESTRPIPE;
2012 goto _end_unlock;
2013 default:
2014 break;
2015 }
2016 appl_ptr += frames;
2017 if (appl_ptr >= runtime->boundary)
2018 appl_ptr -= runtime->boundary;
2019 runtime->control->appl_ptr = appl_ptr;
2020 if (substream->ops->ack)
2021 substream->ops->ack(substream);
2022
2023 offset += frames;
2024 size -= frames;
2025 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 _end_unlock:
2028 snd_pcm_stream_unlock_irq(substream);
2029 _end:
2030 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2031}
2032
Takashi Iwai877211f2005-11-17 13:59:38 +01002033snd_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 -07002034{
Takashi Iwai877211f2005-11-17 13:59:38 +01002035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002037 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002039 err = pcm_sanity_check(substream);
2040 if (err < 0)
2041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002043 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2045 return -EINVAL;
2046 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2047}
2048
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002049EXPORT_SYMBOL(snd_pcm_lib_read);
2050
Takashi Iwai877211f2005-11-17 13:59:38 +01002051static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 unsigned int hwoff,
2053 unsigned long data, unsigned int off,
2054 snd_pcm_uframes_t frames)
2055{
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int err;
2058 void __user **bufs = (void __user **)data;
2059 int channels = runtime->channels;
2060 int c;
2061 if (substream->ops->copy) {
2062 for (c = 0; c < channels; ++c, ++bufs) {
2063 char __user *buf;
2064 if (*bufs == NULL)
2065 continue;
2066 buf = *bufs + samples_to_bytes(runtime, off);
2067 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2068 return err;
2069 }
2070 } else {
2071 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 for (c = 0; c < channels; ++c, ++bufs) {
2073 char *hwbuf;
2074 char __user *buf;
2075 if (*bufs == NULL)
2076 continue;
2077
2078 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2079 buf = *bufs + samples_to_bytes(runtime, off);
2080 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2081 return -EFAULT;
2082 }
2083 }
2084 return 0;
2085}
2086
Takashi Iwai877211f2005-11-17 13:59:38 +01002087snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 void __user **bufs,
2089 snd_pcm_uframes_t frames)
2090{
Takashi Iwai877211f2005-11-17 13:59:38 +01002091 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002093 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002095 err = pcm_sanity_check(substream);
2096 if (err < 0)
2097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2100 return -EBADFD;
2101
Takashi Iwai0df63e42006-04-28 15:13:41 +02002102 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2104 return -EINVAL;
2105 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108EXPORT_SYMBOL(snd_pcm_lib_readv);