blob: 333e4dd29450c7e9e823ebec46b7af4bcb6c62fc [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>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020025#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
27#include <sound/control.h>
28#include <sound/info.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/timer.h>
32
33/*
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
39 *
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 */
Takashi Iwai877211f2005-11-17 13:59:38 +010042void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai877211f2005-11-17 13:59:38 +010044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 snd_pcm_uframes_t frames, ofs, transfer;
46
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
51 if (n < 0)
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
55 else
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
58 }
Takashi Iwai235475c2005-12-07 15:28:07 +010059 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 return;
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
67 } else {
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
73 runtime->boundary;
74 } else {
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020082 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020084 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87 frames = runtime->buffer_size - runtime->silence_filled;
88 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020089 if (snd_BUG_ON(frames > runtime->buffer_size))
90 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (frames == 0)
92 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020093 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 while (frames > 0) {
95 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
96 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
97 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
98 if (substream->ops->silence) {
99 int err;
100 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200101 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } else {
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
105 }
106 } else {
107 unsigned int c;
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
111 int err;
112 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200113 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 } else {
116 size_t dma_csize = runtime->dma_bytes / channels;
117 for (c = 0; c < channels; ++c) {
118 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
119 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
120 }
121 }
122 }
123 runtime->silence_filled += transfer;
124 frames -= transfer;
125 ofs = 0;
126 }
127}
128
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100129#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200130#define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100131#else
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200132#define xrun_debug(substream, mask) 0
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100133#endif
134
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200135#define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
137 dump_stack(); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100138 } while (0)
139
Takashi Iwaic0070112009-06-08 15:58:48 +0200140static void pcm_debug_name(struct snd_pcm_substream *substream,
141 char *name, size_t len)
142{
143 snprintf(name, len, "pcmC%dD%d%c:%d",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p',
147 substream->number);
148}
149
Takashi Iwai877211f2005-11-17 13:59:38 +0100150static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200152 struct snd_pcm_runtime *runtime = substream->runtime;
153
154 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
155 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200157 if (xrun_debug(substream, 1)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200158 char name[16];
159 pcm_debug_name(substream, name, sizeof(name));
160 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100161 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Takashi Iwai98204642009-03-19 09:59:21 +0100165static snd_pcm_uframes_t
166snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
167 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 snd_pcm_uframes_t pos;
170
171 pos = substream->ops->pointer(substream);
172 if (pos == SNDRV_PCM_POS_XRUN)
173 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100175 if (printk_ratelimit()) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200176 char name[16];
177 pcm_debug_name(substream, name, sizeof(name));
178 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
Takashi Iwai5f513e12009-03-19 10:01:47 +0100179 "buffer size = 0x%lx, period size = 0x%lx\n",
Takashi Iwaic0070112009-06-08 15:58:48 +0200180 name, pos, runtime->buffer_size,
Takashi Iwai5f513e12009-03-19 10:01:47 +0100181 runtime->period_size);
182 }
183 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pos -= pos % runtime->min_align;
186 return pos;
187}
188
Takashi Iwai98204642009-03-19 09:59:21 +0100189static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 snd_pcm_uframes_t avail;
193
194 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
195 avail = snd_pcm_playback_avail(runtime);
196 else
197 avail = snd_pcm_capture_avail(runtime);
198 if (avail > runtime->avail_max)
199 runtime->avail_max = avail;
200 if (avail >= runtime->stop_threshold) {
201 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
202 snd_pcm_drain_done(substream);
203 else
204 xrun(substream);
205 return -EPIPE;
206 }
207 if (avail >= runtime->control->avail_min)
208 wake_up(&runtime->sleep);
209 return 0;
210}
211
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100212#define hw_ptr_error(substream, fmt, args...) \
213 do { \
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200214 if (xrun_debug(substream, 1)) { \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100215 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100216 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100217 } \
218 dump_stack_on_xrun(substream); \
219 } \
220 } while (0)
221
Takashi Iwai98204642009-03-19 09:59:21 +0100222static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Takashi Iwai877211f2005-11-17 13:59:38 +0100224 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 snd_pcm_uframes_t pos;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200226 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
227 snd_pcm_sframes_t hdelta, delta;
228 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200230 old_hw_ptr = runtime->status->hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
232 if (pos == SNDRV_PCM_POS_XRUN) {
233 xrun(substream);
234 return -EPIPE;
235 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100236 hw_base = runtime->hw_ptr_base;
237 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100239 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100240 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100241 hw_ptr_interrupt -= runtime->boundary;
242 if (hw_base < runtime->boundary / 2)
243 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100244 delta = new_hw_ptr - hw_ptr_interrupt;
245 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100246 if (delta < 0) {
247 delta += runtime->buffer_size;
248 if (delta < 0) {
249 hw_ptr_error(substream,
250 "Unexpected hw_pointer value "
251 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
252 substream->stream, (long)pos,
253 (long)hw_ptr_interrupt);
254 /* rebase to interrupt position */
255 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100256 /* align hw_base to buffer_size */
257 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100258 delta = 0;
259 } else {
260 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100261 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100262 hw_base = 0;
263 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200266
267 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200268 if (!xrun_debug(substream, 4))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200269 goto no_jiffies_check;
270
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200271 /* Skip the jiffies check for hardwares with BATCH flag.
272 * Such hardware usually just increases the position at each IRQ,
273 * thus it can't give any strange position.
274 */
275 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
276 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200277 hdelta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200278 if (hdelta < runtime->delay)
279 goto no_jiffies_check;
280 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200281 jdelta = jiffies - runtime->hw_ptr_jiffies;
282 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
283 delta = jdelta /
284 (((runtime->period_size * HZ) / runtime->rate)
285 + HZ/100);
286 hw_ptr_error(substream,
287 "hw_ptr skipping! [Q] "
288 "(pos=%ld, delta=%ld, period=%ld, "
289 "jdelta=%lu/%lu/%lu)\n",
290 (long)pos, (long)hdelta,
291 (long)runtime->period_size, jdelta,
292 ((hdelta * HZ) / runtime->rate), delta);
293 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
294 runtime->period_size * delta;
295 if (hw_ptr_interrupt >= runtime->boundary)
296 hw_ptr_interrupt -= runtime->boundary;
297 /* rebase to interrupt position */
298 hw_base = new_hw_ptr = hw_ptr_interrupt;
299 /* align hw_base to buffer_size */
300 hw_base -= hw_base % runtime->buffer_size;
301 delta = 0;
302 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200303 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200304 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100305 hw_ptr_error(substream,
306 "Lost interrupts? "
307 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
308 substream->stream, (long)delta,
309 (long)hw_ptr_interrupt);
310 /* rebase hw_ptr_interrupt */
311 hw_ptr_interrupt =
312 new_hw_ptr - new_hw_ptr % runtime->period_size;
313 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200314 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
317 runtime->silence_size > 0)
318 snd_pcm_playback_silence(substream, new_hw_ptr);
319
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200320 if (runtime->status->hw_ptr == new_hw_ptr)
321 return 0;
322
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100323 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200325 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200326 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
327 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 return snd_pcm_update_hw_ptr_post(substream, runtime);
330}
331
332/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100333int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Takashi Iwai877211f2005-11-17 13:59:38 +0100335 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100337 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200339 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 old_hw_ptr = runtime->status->hw_ptr;
342 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
343 if (pos == SNDRV_PCM_POS_XRUN) {
344 xrun(substream);
345 return -EPIPE;
346 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100347 hw_base = runtime->hw_ptr_base;
348 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100350 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200351 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100352 if (delta < 0) {
353 delta += runtime->buffer_size;
354 if (delta < 0) {
355 hw_ptr_error(substream,
356 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200357 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100358 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200359 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100362 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100363 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100364 hw_base = 0;
365 new_hw_ptr = hw_base + pos;
366 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200367 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200368 if (!xrun_debug(substream, 4))
369 goto no_jiffies_check;
370 if (delta < runtime->delay)
371 goto no_jiffies_check;
372 delta -= runtime->delay;
373 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100374 hw_ptr_error(substream,
375 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200376 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100377 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200378 (long)runtime->period_size, jdelta,
379 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200382 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
384 runtime->silence_size > 0)
385 snd_pcm_playback_silence(substream, new_hw_ptr);
386
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200387 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200388 return 0;
389
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100390 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200392 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200393 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
394 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return snd_pcm_update_hw_ptr_post(substream, runtime);
397}
398
399/**
400 * snd_pcm_set_ops - set the PCM operators
401 * @pcm: the pcm instance
402 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
403 * @ops: the operator table
404 *
405 * Sets the given PCM operators to the pcm instance.
406 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100407void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Takashi Iwai877211f2005-11-17 13:59:38 +0100409 struct snd_pcm_str *stream = &pcm->streams[direction];
410 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 for (substream = stream->substream; substream != NULL; substream = substream->next)
413 substream->ops = ops;
414}
415
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200416EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/**
419 * snd_pcm_sync - set the PCM sync id
420 * @substream: the pcm substream
421 *
422 * Sets the PCM sync identifier for the card.
423 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100424void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Takashi Iwai877211f2005-11-17 13:59:38 +0100426 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 runtime->sync.id32[0] = substream->pcm->card->number;
429 runtime->sync.id32[1] = -1;
430 runtime->sync.id32[2] = -1;
431 runtime->sync.id32[3] = -1;
432}
433
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200434EXPORT_SYMBOL(snd_pcm_set_sync);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
437 * Standard ioctl routine
438 */
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static inline unsigned int div32(unsigned int a, unsigned int b,
441 unsigned int *r)
442{
443 if (b == 0) {
444 *r = 0;
445 return UINT_MAX;
446 }
447 *r = a % b;
448 return a / b;
449}
450
451static inline unsigned int div_down(unsigned int a, unsigned int b)
452{
453 if (b == 0)
454 return UINT_MAX;
455 return a / b;
456}
457
458static inline unsigned int div_up(unsigned int a, unsigned int b)
459{
460 unsigned int r;
461 unsigned int q;
462 if (b == 0)
463 return UINT_MAX;
464 q = div32(a, b, &r);
465 if (r)
466 ++q;
467 return q;
468}
469
470static inline unsigned int mul(unsigned int a, unsigned int b)
471{
472 if (a == 0)
473 return 0;
474 if (div_down(UINT_MAX, a) < b)
475 return UINT_MAX;
476 return a * b;
477}
478
479static inline unsigned int muldiv32(unsigned int a, unsigned int b,
480 unsigned int c, unsigned int *r)
481{
482 u_int64_t n = (u_int64_t) a * b;
483 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200484 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *r = 0;
486 return UINT_MAX;
487 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200488 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (n >= UINT_MAX) {
490 *r = 0;
491 return UINT_MAX;
492 }
493 return n;
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/**
497 * snd_interval_refine - refine the interval value of configurator
498 * @i: the interval value to refine
499 * @v: the interval value to refer to
500 *
501 * Refines the interval value with the reference value.
502 * The interval is changed to the range satisfying both intervals.
503 * The interval status (min, max, integer, etc.) are evaluated.
504 *
505 * Returns non-zero if the value is changed, zero if not changed.
506 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100507int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200510 if (snd_BUG_ON(snd_interval_empty(i)))
511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (i->min < v->min) {
513 i->min = v->min;
514 i->openmin = v->openmin;
515 changed = 1;
516 } else if (i->min == v->min && !i->openmin && v->openmin) {
517 i->openmin = 1;
518 changed = 1;
519 }
520 if (i->max > v->max) {
521 i->max = v->max;
522 i->openmax = v->openmax;
523 changed = 1;
524 } else if (i->max == v->max && !i->openmax && v->openmax) {
525 i->openmax = 1;
526 changed = 1;
527 }
528 if (!i->integer && v->integer) {
529 i->integer = 1;
530 changed = 1;
531 }
532 if (i->integer) {
533 if (i->openmin) {
534 i->min++;
535 i->openmin = 0;
536 }
537 if (i->openmax) {
538 i->max--;
539 i->openmax = 0;
540 }
541 } else if (!i->openmin && !i->openmax && i->min == i->max)
542 i->integer = 1;
543 if (snd_interval_checkempty(i)) {
544 snd_interval_none(i);
545 return -EINVAL;
546 }
547 return changed;
548}
549
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200550EXPORT_SYMBOL(snd_interval_refine);
551
Takashi Iwai877211f2005-11-17 13:59:38 +0100552static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200554 if (snd_BUG_ON(snd_interval_empty(i)))
555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (snd_interval_single(i))
557 return 0;
558 i->max = i->min;
559 i->openmax = i->openmin;
560 if (i->openmax)
561 i->max++;
562 return 1;
563}
564
Takashi Iwai877211f2005-11-17 13:59:38 +0100565static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200567 if (snd_BUG_ON(snd_interval_empty(i)))
568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (snd_interval_single(i))
570 return 0;
571 i->min = i->max;
572 i->openmin = i->openmax;
573 if (i->openmin)
574 i->min--;
575 return 1;
576}
577
Takashi Iwai877211f2005-11-17 13:59:38 +0100578void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 if (a->empty || b->empty) {
581 snd_interval_none(c);
582 return;
583 }
584 c->empty = 0;
585 c->min = mul(a->min, b->min);
586 c->openmin = (a->openmin || b->openmin);
587 c->max = mul(a->max, b->max);
588 c->openmax = (a->openmax || b->openmax);
589 c->integer = (a->integer && b->integer);
590}
591
592/**
593 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200594 * @a: dividend
595 * @b: divisor
596 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
598 * c = a / b
599 *
600 * Returns non-zero if the value is changed, zero if not changed.
601 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100602void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 unsigned int r;
605 if (a->empty || b->empty) {
606 snd_interval_none(c);
607 return;
608 }
609 c->empty = 0;
610 c->min = div32(a->min, b->max, &r);
611 c->openmin = (r || a->openmin || b->openmax);
612 if (b->min > 0) {
613 c->max = div32(a->max, b->min, &r);
614 if (r) {
615 c->max++;
616 c->openmax = 1;
617 } else
618 c->openmax = (a->openmax || b->openmin);
619 } else {
620 c->max = UINT_MAX;
621 c->openmax = 0;
622 }
623 c->integer = 0;
624}
625
626/**
627 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200628 * @a: dividend 1
629 * @b: dividend 2
630 * @k: divisor (as integer)
631 * @c: result
632 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * c = a * b / k
634 *
635 * Returns non-zero if the value is changed, zero if not changed.
636 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100637void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
638 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 unsigned int r;
641 if (a->empty || b->empty) {
642 snd_interval_none(c);
643 return;
644 }
645 c->empty = 0;
646 c->min = muldiv32(a->min, b->min, k, &r);
647 c->openmin = (r || a->openmin || b->openmin);
648 c->max = muldiv32(a->max, b->max, k, &r);
649 if (r) {
650 c->max++;
651 c->openmax = 1;
652 } else
653 c->openmax = (a->openmax || b->openmax);
654 c->integer = 0;
655}
656
657/**
658 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200659 * @a: dividend 1
660 * @k: dividend 2 (as integer)
661 * @b: divisor
662 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
664 * c = a * k / b
665 *
666 * Returns non-zero if the value is changed, zero if not changed.
667 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100668void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
669 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 unsigned int r;
672 if (a->empty || b->empty) {
673 snd_interval_none(c);
674 return;
675 }
676 c->empty = 0;
677 c->min = muldiv32(a->min, k, b->max, &r);
678 c->openmin = (r || a->openmin || b->openmax);
679 if (b->min > 0) {
680 c->max = muldiv32(a->max, k, b->min, &r);
681 if (r) {
682 c->max++;
683 c->openmax = 1;
684 } else
685 c->openmax = (a->openmax || b->openmin);
686 } else {
687 c->max = UINT_MAX;
688 c->openmax = 0;
689 }
690 c->integer = 0;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/* ---- */
694
695
696/**
697 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200698 * @i: interval to refine
699 * @rats_count: number of ratnum_t
700 * @rats: ratnum_t array
701 * @nump: pointer to store the resultant numerator
702 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
704 * Returns non-zero if the value is changed, zero if not changed.
705 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100706int snd_interval_ratnum(struct snd_interval *i,
707 unsigned int rats_count, struct snd_ratnum *rats,
708 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 unsigned int best_num, best_diff, best_den;
711 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100712 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int err;
714
715 best_num = best_den = best_diff = 0;
716 for (k = 0; k < rats_count; ++k) {
717 unsigned int num = rats[k].num;
718 unsigned int den;
719 unsigned int q = i->min;
720 int diff;
721 if (q == 0)
722 q = 1;
723 den = div_down(num, q);
724 if (den < rats[k].den_min)
725 continue;
726 if (den > rats[k].den_max)
727 den = rats[k].den_max;
728 else {
729 unsigned int r;
730 r = (den - rats[k].den_min) % rats[k].den_step;
731 if (r != 0)
732 den -= r;
733 }
734 diff = num - q * den;
735 if (best_num == 0 ||
736 diff * best_den < best_diff * den) {
737 best_diff = diff;
738 best_den = den;
739 best_num = num;
740 }
741 }
742 if (best_den == 0) {
743 i->empty = 1;
744 return -EINVAL;
745 }
746 t.min = div_down(best_num, best_den);
747 t.openmin = !!(best_num % best_den);
748
749 best_num = best_den = best_diff = 0;
750 for (k = 0; k < rats_count; ++k) {
751 unsigned int num = rats[k].num;
752 unsigned int den;
753 unsigned int q = i->max;
754 int diff;
755 if (q == 0) {
756 i->empty = 1;
757 return -EINVAL;
758 }
759 den = div_up(num, q);
760 if (den > rats[k].den_max)
761 continue;
762 if (den < rats[k].den_min)
763 den = rats[k].den_min;
764 else {
765 unsigned int r;
766 r = (den - rats[k].den_min) % rats[k].den_step;
767 if (r != 0)
768 den += rats[k].den_step - r;
769 }
770 diff = q * den - num;
771 if (best_num == 0 ||
772 diff * best_den < best_diff * den) {
773 best_diff = diff;
774 best_den = den;
775 best_num = num;
776 }
777 }
778 if (best_den == 0) {
779 i->empty = 1;
780 return -EINVAL;
781 }
782 t.max = div_up(best_num, best_den);
783 t.openmax = !!(best_num % best_den);
784 t.integer = 0;
785 err = snd_interval_refine(i, &t);
786 if (err < 0)
787 return err;
788
789 if (snd_interval_single(i)) {
790 if (nump)
791 *nump = best_num;
792 if (denp)
793 *denp = best_den;
794 }
795 return err;
796}
797
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200798EXPORT_SYMBOL(snd_interval_ratnum);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
801 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200802 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100803 * @rats_count: number of struct ratden
804 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200805 * @nump: pointer to store the resultant numerator
806 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
808 * Returns non-zero if the value is changed, zero if not changed.
809 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100810static int snd_interval_ratden(struct snd_interval *i,
811 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int *nump, unsigned int *denp)
813{
814 unsigned int best_num, best_diff, best_den;
815 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100816 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int err;
818
819 best_num = best_den = best_diff = 0;
820 for (k = 0; k < rats_count; ++k) {
821 unsigned int num;
822 unsigned int den = rats[k].den;
823 unsigned int q = i->min;
824 int diff;
825 num = mul(q, den);
826 if (num > rats[k].num_max)
827 continue;
828 if (num < rats[k].num_min)
829 num = rats[k].num_max;
830 else {
831 unsigned int r;
832 r = (num - rats[k].num_min) % rats[k].num_step;
833 if (r != 0)
834 num += rats[k].num_step - r;
835 }
836 diff = num - q * den;
837 if (best_num == 0 ||
838 diff * best_den < best_diff * den) {
839 best_diff = diff;
840 best_den = den;
841 best_num = num;
842 }
843 }
844 if (best_den == 0) {
845 i->empty = 1;
846 return -EINVAL;
847 }
848 t.min = div_down(best_num, best_den);
849 t.openmin = !!(best_num % best_den);
850
851 best_num = best_den = best_diff = 0;
852 for (k = 0; k < rats_count; ++k) {
853 unsigned int num;
854 unsigned int den = rats[k].den;
855 unsigned int q = i->max;
856 int diff;
857 num = mul(q, den);
858 if (num < rats[k].num_min)
859 continue;
860 if (num > rats[k].num_max)
861 num = rats[k].num_max;
862 else {
863 unsigned int r;
864 r = (num - rats[k].num_min) % rats[k].num_step;
865 if (r != 0)
866 num -= r;
867 }
868 diff = q * den - num;
869 if (best_num == 0 ||
870 diff * best_den < best_diff * den) {
871 best_diff = diff;
872 best_den = den;
873 best_num = num;
874 }
875 }
876 if (best_den == 0) {
877 i->empty = 1;
878 return -EINVAL;
879 }
880 t.max = div_up(best_num, best_den);
881 t.openmax = !!(best_num % best_den);
882 t.integer = 0;
883 err = snd_interval_refine(i, &t);
884 if (err < 0)
885 return err;
886
887 if (snd_interval_single(i)) {
888 if (nump)
889 *nump = best_num;
890 if (denp)
891 *denp = best_den;
892 }
893 return err;
894}
895
896/**
897 * snd_interval_list - refine the interval value from the list
898 * @i: the interval value to refine
899 * @count: the number of elements in the list
900 * @list: the value list
901 * @mask: the bit-mask to evaluate
902 *
903 * Refines the interval value from the list.
904 * When mask is non-zero, only the elements corresponding to bit 1 are
905 * evaluated.
906 *
907 * Returns non-zero if the value is changed, zero if not changed.
908 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100909int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 unsigned int k;
912 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100913
914 if (!count) {
915 i->empty = 1;
916 return -EINVAL;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 for (k = 0; k < count; k++) {
919 if (mask && !(mask & (1 << k)))
920 continue;
921 if (i->min == list[k] && !i->openmin)
922 goto _l1;
923 if (i->min < list[k]) {
924 i->min = list[k];
925 i->openmin = 0;
926 changed = 1;
927 goto _l1;
928 }
929 }
930 i->empty = 1;
931 return -EINVAL;
932 _l1:
933 for (k = count; k-- > 0;) {
934 if (mask && !(mask & (1 << k)))
935 continue;
936 if (i->max == list[k] && !i->openmax)
937 goto _l2;
938 if (i->max > list[k]) {
939 i->max = list[k];
940 i->openmax = 0;
941 changed = 1;
942 goto _l2;
943 }
944 }
945 i->empty = 1;
946 return -EINVAL;
947 _l2:
948 if (snd_interval_checkempty(i)) {
949 i->empty = 1;
950 return -EINVAL;
951 }
952 return changed;
953}
954
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200955EXPORT_SYMBOL(snd_interval_list);
956
Takashi Iwai877211f2005-11-17 13:59:38 +0100957static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 unsigned int n;
960 int changed = 0;
961 n = (i->min - min) % step;
962 if (n != 0 || i->openmin) {
963 i->min += step - n;
964 changed = 1;
965 }
966 n = (i->max - min) % step;
967 if (n != 0 || i->openmax) {
968 i->max -= n;
969 changed = 1;
970 }
971 if (snd_interval_checkempty(i)) {
972 i->empty = 1;
973 return -EINVAL;
974 }
975 return changed;
976}
977
978/* Info constraints helpers */
979
980/**
981 * snd_pcm_hw_rule_add - add the hw-constraint rule
982 * @runtime: the pcm runtime instance
983 * @cond: condition bits
984 * @var: the variable to evaluate
985 * @func: the evaluation function
986 * @private: the private data pointer passed to function
987 * @dep: the dependent variables
988 *
989 * Returns zero if successful, or a negative error code on failure.
990 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100991int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int var,
993 snd_pcm_hw_rule_func_t func, void *private,
994 int dep, ...)
995{
Takashi Iwai877211f2005-11-17 13:59:38 +0100996 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
997 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unsigned int k;
999 va_list args;
1000 va_start(args, dep);
1001 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001002 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned int new_rules = constrs->rules_all + 16;
1004 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1005 if (!new)
1006 return -ENOMEM;
1007 if (constrs->rules) {
1008 memcpy(new, constrs->rules,
1009 constrs->rules_num * sizeof(*c));
1010 kfree(constrs->rules);
1011 }
1012 constrs->rules = new;
1013 constrs->rules_all = new_rules;
1014 }
1015 c = &constrs->rules[constrs->rules_num];
1016 c->cond = cond;
1017 c->func = func;
1018 c->var = var;
1019 c->private = private;
1020 k = 0;
1021 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001022 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 c->deps[k++] = dep;
1025 if (dep < 0)
1026 break;
1027 dep = va_arg(args, int);
1028 }
1029 constrs->rules_num++;
1030 va_end(args);
1031 return 0;
1032}
1033
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001034EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001037 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001038 * @runtime: PCM runtime instance
1039 * @var: hw_params variable to apply the mask
1040 * @mask: the bitmap mask
1041 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001042 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001044int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 u_int32_t mask)
1046{
Takashi Iwai877211f2005-11-17 13:59:38 +01001047 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1048 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *maskp->bits &= mask;
1050 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1051 if (*maskp->bits == 0)
1052 return -EINVAL;
1053 return 0;
1054}
1055
1056/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001057 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001058 * @runtime: PCM runtime instance
1059 * @var: hw_params variable to apply the mask
1060 * @mask: the 64bit bitmap mask
1061 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001062 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001064int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 u_int64_t mask)
1066{
Takashi Iwai877211f2005-11-17 13:59:38 +01001067 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1068 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 maskp->bits[0] &= (u_int32_t)mask;
1070 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1071 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1072 if (! maskp->bits[0] && ! maskp->bits[1])
1073 return -EINVAL;
1074 return 0;
1075}
1076
1077/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001078 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001079 * @runtime: PCM runtime instance
1080 * @var: hw_params variable to apply the integer constraint
1081 *
1082 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001084int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Takashi Iwai877211f2005-11-17 13:59:38 +01001086 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return snd_interval_setinteger(constrs_interval(constrs, var));
1088}
1089
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001090EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001093 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001094 * @runtime: PCM runtime instance
1095 * @var: hw_params variable to apply the range
1096 * @min: the minimal value
1097 * @max: the maximal value
1098 *
1099 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001101int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unsigned int min, unsigned int max)
1103{
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1105 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 t.min = min;
1107 t.max = max;
1108 t.openmin = t.openmax = 0;
1109 t.integer = 0;
1110 return snd_interval_refine(constrs_interval(constrs, var), &t);
1111}
1112
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001113EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1114
Takashi Iwai877211f2005-11-17 13:59:38 +01001115static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1116 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Takashi Iwai877211f2005-11-17 13:59:38 +01001118 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1120}
1121
1122
1123/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001124 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001125 * @runtime: PCM runtime instance
1126 * @cond: condition bits
1127 * @var: hw_params variable to apply the list constraint
1128 * @l: list
1129 *
1130 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001132int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned int cond,
1134 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 return snd_pcm_hw_rule_add(runtime, cond, var,
1138 snd_pcm_hw_rule_list, l,
1139 var, -1);
1140}
1141
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001142EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1143
Takashi Iwai877211f2005-11-17 13:59:38 +01001144static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1145 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Takashi Iwai877211f2005-11-17 13:59:38 +01001147 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned int num = 0, den = 0;
1149 int err;
1150 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1151 r->nrats, r->rats, &num, &den);
1152 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1153 params->rate_num = num;
1154 params->rate_den = den;
1155 }
1156 return err;
1157}
1158
1159/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001160 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001161 * @runtime: PCM runtime instance
1162 * @cond: condition bits
1163 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001164 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001166int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned int cond,
1168 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 return snd_pcm_hw_rule_add(runtime, cond, var,
1172 snd_pcm_hw_rule_ratnums, r,
1173 var, -1);
1174}
1175
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001176EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1177
Takashi Iwai877211f2005-11-17 13:59:38 +01001178static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1179 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Takashi Iwai877211f2005-11-17 13:59:38 +01001181 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 unsigned int num = 0, den = 0;
1183 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1184 r->nrats, r->rats, &num, &den);
1185 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1186 params->rate_num = num;
1187 params->rate_den = den;
1188 }
1189 return err;
1190}
1191
1192/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001193 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001194 * @runtime: PCM runtime instance
1195 * @cond: condition bits
1196 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001197 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001199int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 unsigned int cond,
1201 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001202 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 return snd_pcm_hw_rule_add(runtime, cond, var,
1205 snd_pcm_hw_rule_ratdens, r,
1206 var, -1);
1207}
1208
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001209EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1210
Takashi Iwai877211f2005-11-17 13:59:38 +01001211static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1212 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 unsigned int l = (unsigned long) rule->private;
1215 int width = l & 0xffff;
1216 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001217 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (snd_interval_single(i) && snd_interval_value(i) == width)
1219 params->msbits = msbits;
1220 return 0;
1221}
1222
1223/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001224 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001225 * @runtime: PCM runtime instance
1226 * @cond: condition bits
1227 * @width: sample bits width
1228 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001230int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int cond,
1232 unsigned int width,
1233 unsigned int msbits)
1234{
1235 unsigned long l = (msbits << 16) | width;
1236 return snd_pcm_hw_rule_add(runtime, cond, -1,
1237 snd_pcm_hw_rule_msbits,
1238 (void*) l,
1239 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1240}
1241
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001242EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1243
Takashi Iwai877211f2005-11-17 13:59:38 +01001244static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1245 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 unsigned long step = (unsigned long) rule->private;
1248 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1249}
1250
1251/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001252 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001253 * @runtime: PCM runtime instance
1254 * @cond: condition bits
1255 * @var: hw_params variable to apply the step constraint
1256 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001258int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 unsigned int cond,
1260 snd_pcm_hw_param_t var,
1261 unsigned long step)
1262{
1263 return snd_pcm_hw_rule_add(runtime, cond, var,
1264 snd_pcm_hw_rule_step, (void *) step,
1265 var, -1);
1266}
1267
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001268EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1269
Takashi Iwai877211f2005-11-17 13:59:38 +01001270static 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 -07001271{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001272 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1274 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1275 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1276 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1277 };
1278 return snd_interval_list(hw_param_interval(params, rule->var),
1279 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1280}
1281
1282/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001283 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001284 * @runtime: PCM runtime instance
1285 * @cond: condition bits
1286 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001288int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned int cond,
1290 snd_pcm_hw_param_t var)
1291{
1292 return snd_pcm_hw_rule_add(runtime, cond, var,
1293 snd_pcm_hw_rule_pow2, NULL,
1294 var, -1);
1295}
1296
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001297EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1298
Takashi Iwai877211f2005-11-17 13:59:38 +01001299static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001300 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (hw_is_mask(var)) {
1303 snd_mask_any(hw_param_mask(params, var));
1304 params->cmask |= 1 << var;
1305 params->rmask |= 1 << var;
1306 return;
1307 }
1308 if (hw_is_interval(var)) {
1309 snd_interval_any(hw_param_interval(params, var));
1310 params->cmask |= 1 << var;
1311 params->rmask |= 1 << var;
1312 return;
1313 }
1314 snd_BUG();
1315}
1316
Takashi Iwai877211f2005-11-17 13:59:38 +01001317void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 unsigned int k;
1320 memset(params, 0, sizeof(*params));
1321 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1322 _snd_pcm_hw_param_any(params, k);
1323 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1324 _snd_pcm_hw_param_any(params, k);
1325 params->info = ~0U;
1326}
1327
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001328EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001331 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001332 * @params: the hw_params instance
1333 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001334 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001336 * Return the value for field @var if it's fixed in configuration space
1337 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001339int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1340 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001343 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!snd_mask_single(mask))
1345 return -EINVAL;
1346 if (dir)
1347 *dir = 0;
1348 return snd_mask_value(mask);
1349 }
1350 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001351 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!snd_interval_single(i))
1353 return -EINVAL;
1354 if (dir)
1355 *dir = i->openmin;
1356 return snd_interval_value(i);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EINVAL;
1359}
1360
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001361EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai877211f2005-11-17 13:59:38 +01001363void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 snd_pcm_hw_param_t var)
1365{
1366 if (hw_is_mask(var)) {
1367 snd_mask_none(hw_param_mask(params, var));
1368 params->cmask |= 1 << var;
1369 params->rmask |= 1 << var;
1370 } else if (hw_is_interval(var)) {
1371 snd_interval_none(hw_param_interval(params, var));
1372 params->cmask |= 1 << var;
1373 params->rmask |= 1 << var;
1374 } else {
1375 snd_BUG();
1376 }
1377}
1378
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001379EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Takashi Iwai877211f2005-11-17 13:59:38 +01001381static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001382 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 int changed;
1385 if (hw_is_mask(var))
1386 changed = snd_mask_refine_first(hw_param_mask(params, var));
1387 else if (hw_is_interval(var))
1388 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001389 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (changed) {
1392 params->cmask |= 1 << var;
1393 params->rmask |= 1 << var;
1394 }
1395 return changed;
1396}
1397
1398
1399/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001400 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001401 * @pcm: PCM instance
1402 * @params: the hw_params instance
1403 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001404 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001406 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * values > minimum. Reduce configuration space accordingly.
1408 * Return the minimum.
1409 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001410int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1411 struct snd_pcm_hw_params *params,
1412 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 int changed = _snd_pcm_hw_param_first(params, var);
1415 if (changed < 0)
1416 return changed;
1417 if (params->rmask) {
1418 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001419 if (snd_BUG_ON(err < 0))
1420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 return snd_pcm_hw_param_value(params, var, dir);
1423}
1424
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001425EXPORT_SYMBOL(snd_pcm_hw_param_first);
1426
Takashi Iwai877211f2005-11-17 13:59:38 +01001427static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001428 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 int changed;
1431 if (hw_is_mask(var))
1432 changed = snd_mask_refine_last(hw_param_mask(params, var));
1433 else if (hw_is_interval(var))
1434 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001435 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (changed) {
1438 params->cmask |= 1 << var;
1439 params->rmask |= 1 << var;
1440 }
1441 return changed;
1442}
1443
1444
1445/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001446 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001447 * @pcm: PCM instance
1448 * @params: the hw_params instance
1449 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001450 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001452 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * values < maximum. Reduce configuration space accordingly.
1454 * Return the maximum.
1455 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001456int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1457 struct snd_pcm_hw_params *params,
1458 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int changed = _snd_pcm_hw_param_last(params, var);
1461 if (changed < 0)
1462 return changed;
1463 if (params->rmask) {
1464 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001465 if (snd_BUG_ON(err < 0))
1466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 return snd_pcm_hw_param_value(params, var, dir);
1469}
1470
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001471EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001474 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001475 * @pcm: PCM instance
1476 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001478 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * The configuration chosen is that obtained fixing in this order:
1480 * first access, first format, first subformat, min channels,
1481 * min rate, min period time, max buffer size, min tick time
1482 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001483int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1484 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001486 static int vars[] = {
1487 SNDRV_PCM_HW_PARAM_ACCESS,
1488 SNDRV_PCM_HW_PARAM_FORMAT,
1489 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1490 SNDRV_PCM_HW_PARAM_CHANNELS,
1491 SNDRV_PCM_HW_PARAM_RATE,
1492 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1493 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1494 SNDRV_PCM_HW_PARAM_TICK_TIME,
1495 -1
1496 };
1497 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001499 for (v = vars; *v != -1; v++) {
1500 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1501 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1502 else
1503 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001504 if (snd_BUG_ON(err < 0))
1505 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 0;
1508}
1509
Takashi Iwai877211f2005-11-17 13:59:38 +01001510static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 void *arg)
1512{
Takashi Iwai877211f2005-11-17 13:59:38 +01001513 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 unsigned long flags;
1515 snd_pcm_stream_lock_irqsave(substream, flags);
1516 if (snd_pcm_running(substream) &&
1517 snd_pcm_update_hw_ptr(substream) >= 0)
1518 runtime->status->hw_ptr %= runtime->buffer_size;
1519 else
1520 runtime->status->hw_ptr = 0;
1521 snd_pcm_stream_unlock_irqrestore(substream, flags);
1522 return 0;
1523}
1524
Takashi Iwai877211f2005-11-17 13:59:38 +01001525static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 void *arg)
1527{
Takashi Iwai877211f2005-11-17 13:59:38 +01001528 struct snd_pcm_channel_info *info = arg;
1529 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 int width;
1531 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1532 info->offset = -1;
1533 return 0;
1534 }
1535 width = snd_pcm_format_physical_width(runtime->format);
1536 if (width < 0)
1537 return width;
1538 info->offset = 0;
1539 switch (runtime->access) {
1540 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1541 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1542 info->first = info->channel * width;
1543 info->step = runtime->channels * width;
1544 break;
1545 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1546 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1547 {
1548 size_t size = runtime->dma_bytes / runtime->channels;
1549 info->first = info->channel * size * 8;
1550 info->step = width;
1551 break;
1552 }
1553 default:
1554 snd_BUG();
1555 break;
1556 }
1557 return 0;
1558}
1559
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001560static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1561 void *arg)
1562{
1563 struct snd_pcm_hw_params *params = arg;
1564 snd_pcm_format_t format;
1565 int channels, width;
1566
1567 params->fifo_size = substream->runtime->hw.fifo_size;
1568 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1569 format = params_format(params);
1570 channels = params_channels(params);
1571 width = snd_pcm_format_physical_width(format);
1572 params->fifo_size /= width * channels;
1573 }
1574 return 0;
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/**
1578 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1579 * @substream: the pcm substream instance
1580 * @cmd: ioctl command
1581 * @arg: ioctl argument
1582 *
1583 * Processes the generic ioctl commands for PCM.
1584 * Can be passed as the ioctl callback for PCM ops.
1585 *
1586 * Returns zero if successful, or a negative error code on failure.
1587 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001588int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 unsigned int cmd, void *arg)
1590{
1591 switch (cmd) {
1592 case SNDRV_PCM_IOCTL1_INFO:
1593 return 0;
1594 case SNDRV_PCM_IOCTL1_RESET:
1595 return snd_pcm_lib_ioctl_reset(substream, arg);
1596 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1597 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001598 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1599 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 return -ENXIO;
1602}
1603
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001604EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/**
1607 * snd_pcm_period_elapsed - update the pcm status for the next period
1608 * @substream: the pcm substream instance
1609 *
1610 * This function is called from the interrupt handler when the
1611 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001612 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 *
1614 * Even if more than one periods have elapsed since the last call, you
1615 * have to call this only once.
1616 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001617void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Takashi Iwai877211f2005-11-17 13:59:38 +01001619 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 unsigned long flags;
1621
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001622 if (PCM_RUNTIME_CHECK(substream))
1623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (runtime->transfer_ack_begin)
1627 runtime->transfer_ack_begin(substream);
1628
1629 snd_pcm_stream_lock_irqsave(substream, flags);
1630 if (!snd_pcm_running(substream) ||
1631 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1632 goto _end;
1633
1634 if (substream->timer_running)
1635 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 _end:
1637 snd_pcm_stream_unlock_irqrestore(substream, flags);
1638 if (runtime->transfer_ack_end)
1639 runtime->transfer_ack_end(substream);
1640 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1641}
1642
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001643EXPORT_SYMBOL(snd_pcm_period_elapsed);
1644
Takashi Iwai13075512008-01-08 18:08:14 +01001645/*
1646 * Wait until avail_min data becomes available
1647 * Returns a negative error code if any error occurs during operation.
1648 * The available space is stored on availp. When err = 0 and avail = 0
1649 * on the capture stream, it indicates the stream is in DRAINING state.
1650 */
1651static int wait_for_avail_min(struct snd_pcm_substream *substream,
1652 snd_pcm_uframes_t *availp)
1653{
1654 struct snd_pcm_runtime *runtime = substream->runtime;
1655 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1656 wait_queue_t wait;
1657 int err = 0;
1658 snd_pcm_uframes_t avail = 0;
1659 long tout;
1660
1661 init_waitqueue_entry(&wait, current);
1662 add_wait_queue(&runtime->sleep, &wait);
1663 for (;;) {
1664 if (signal_pending(current)) {
1665 err = -ERESTARTSYS;
1666 break;
1667 }
1668 set_current_state(TASK_INTERRUPTIBLE);
1669 snd_pcm_stream_unlock_irq(substream);
1670 tout = schedule_timeout(msecs_to_jiffies(10000));
1671 snd_pcm_stream_lock_irq(substream);
1672 switch (runtime->status->state) {
1673 case SNDRV_PCM_STATE_SUSPENDED:
1674 err = -ESTRPIPE;
1675 goto _endloop;
1676 case SNDRV_PCM_STATE_XRUN:
1677 err = -EPIPE;
1678 goto _endloop;
1679 case SNDRV_PCM_STATE_DRAINING:
1680 if (is_playback)
1681 err = -EPIPE;
1682 else
1683 avail = 0; /* indicate draining */
1684 goto _endloop;
1685 case SNDRV_PCM_STATE_OPEN:
1686 case SNDRV_PCM_STATE_SETUP:
1687 case SNDRV_PCM_STATE_DISCONNECTED:
1688 err = -EBADFD;
1689 goto _endloop;
1690 }
1691 if (!tout) {
1692 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1693 is_playback ? "playback" : "capture");
1694 err = -EIO;
1695 break;
1696 }
1697 if (is_playback)
1698 avail = snd_pcm_playback_avail(runtime);
1699 else
1700 avail = snd_pcm_capture_avail(runtime);
1701 if (avail >= runtime->control->avail_min)
1702 break;
1703 }
1704 _endloop:
1705 remove_wait_queue(&runtime->sleep, &wait);
1706 *availp = avail;
1707 return err;
1708}
1709
Takashi Iwai877211f2005-11-17 13:59:38 +01001710static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 unsigned int hwoff,
1712 unsigned long data, unsigned int off,
1713 snd_pcm_uframes_t frames)
1714{
Takashi Iwai877211f2005-11-17 13:59:38 +01001715 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int err;
1717 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1718 if (substream->ops->copy) {
1719 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1720 return err;
1721 } else {
1722 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1724 return -EFAULT;
1725 }
1726 return 0;
1727}
1728
Takashi Iwai877211f2005-11-17 13:59:38 +01001729typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 unsigned long data, unsigned int off,
1731 snd_pcm_uframes_t size);
1732
Takashi Iwai877211f2005-11-17 13:59:38 +01001733static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 unsigned long data,
1735 snd_pcm_uframes_t size,
1736 int nonblock,
1737 transfer_f transfer)
1738{
Takashi Iwai877211f2005-11-17 13:59:38 +01001739 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 snd_pcm_uframes_t xfer = 0;
1741 snd_pcm_uframes_t offset = 0;
1742 int err = 0;
1743
1744 if (size == 0)
1745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 snd_pcm_stream_lock_irq(substream);
1748 switch (runtime->status->state) {
1749 case SNDRV_PCM_STATE_PREPARED:
1750 case SNDRV_PCM_STATE_RUNNING:
1751 case SNDRV_PCM_STATE_PAUSED:
1752 break;
1753 case SNDRV_PCM_STATE_XRUN:
1754 err = -EPIPE;
1755 goto _end_unlock;
1756 case SNDRV_PCM_STATE_SUSPENDED:
1757 err = -ESTRPIPE;
1758 goto _end_unlock;
1759 default:
1760 err = -EBADFD;
1761 goto _end_unlock;
1762 }
1763
1764 while (size > 0) {
1765 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1766 snd_pcm_uframes_t avail;
1767 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001768 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 snd_pcm_update_hw_ptr(substream);
1770 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001771 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (nonblock) {
1773 err = -EAGAIN;
1774 goto _end_unlock;
1775 }
Takashi Iwai13075512008-01-08 18:08:14 +01001776 err = wait_for_avail_min(substream, &avail);
1777 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 frames = size > avail ? avail : size;
1781 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1782 if (frames > cont)
1783 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001784 if (snd_BUG_ON(!frames)) {
1785 snd_pcm_stream_unlock_irq(substream);
1786 return -EINVAL;
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 appl_ptr = runtime->control->appl_ptr;
1789 appl_ofs = appl_ptr % runtime->buffer_size;
1790 snd_pcm_stream_unlock_irq(substream);
1791 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1792 goto _end;
1793 snd_pcm_stream_lock_irq(substream);
1794 switch (runtime->status->state) {
1795 case SNDRV_PCM_STATE_XRUN:
1796 err = -EPIPE;
1797 goto _end_unlock;
1798 case SNDRV_PCM_STATE_SUSPENDED:
1799 err = -ESTRPIPE;
1800 goto _end_unlock;
1801 default:
1802 break;
1803 }
1804 appl_ptr += frames;
1805 if (appl_ptr >= runtime->boundary)
1806 appl_ptr -= runtime->boundary;
1807 runtime->control->appl_ptr = appl_ptr;
1808 if (substream->ops->ack)
1809 substream->ops->ack(substream);
1810
1811 offset += frames;
1812 size -= frames;
1813 xfer += frames;
1814 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1815 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1816 err = snd_pcm_start(substream);
1817 if (err < 0)
1818 goto _end_unlock;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821 _end_unlock:
1822 snd_pcm_stream_unlock_irq(substream);
1823 _end:
1824 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1825}
1826
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001827/* sanity-check for read/write methods */
1828static int pcm_sanity_check(struct snd_pcm_substream *substream)
1829{
1830 struct snd_pcm_runtime *runtime;
1831 if (PCM_RUNTIME_CHECK(substream))
1832 return -ENXIO;
1833 runtime = substream->runtime;
1834 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1835 return -EINVAL;
1836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1837 return -EBADFD;
1838 return 0;
1839}
1840
Takashi Iwai877211f2005-11-17 13:59:38 +01001841snd_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 -07001842{
Takashi Iwai877211f2005-11-17 13:59:38 +01001843 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001847 err = pcm_sanity_check(substream);
1848 if (err < 0)
1849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001851 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1854 runtime->channels > 1)
1855 return -EINVAL;
1856 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1857 snd_pcm_lib_write_transfer);
1858}
1859
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001860EXPORT_SYMBOL(snd_pcm_lib_write);
1861
Takashi Iwai877211f2005-11-17 13:59:38 +01001862static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned int hwoff,
1864 unsigned long data, unsigned int off,
1865 snd_pcm_uframes_t frames)
1866{
Takashi Iwai877211f2005-11-17 13:59:38 +01001867 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int err;
1869 void __user **bufs = (void __user **)data;
1870 int channels = runtime->channels;
1871 int c;
1872 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001873 if (snd_BUG_ON(!substream->ops->silence))
1874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 for (c = 0; c < channels; ++c, ++bufs) {
1876 if (*bufs == NULL) {
1877 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1878 return err;
1879 } else {
1880 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1881 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1882 return err;
1883 }
1884 }
1885 } else {
1886 /* default transfer behaviour */
1887 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 for (c = 0; c < channels; ++c, ++bufs) {
1889 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1890 if (*bufs == NULL) {
1891 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1892 } else {
1893 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1894 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1895 return -EFAULT;
1896 }
1897 }
1898 }
1899 return 0;
1900}
1901
Takashi Iwai877211f2005-11-17 13:59:38 +01001902snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 void __user **bufs,
1904 snd_pcm_uframes_t frames)
1905{
Takashi Iwai877211f2005-11-17 13:59:38 +01001906 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001908 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001910 err = pcm_sanity_check(substream);
1911 if (err < 0)
1912 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001914 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1917 return -EINVAL;
1918 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1919 nonblock, snd_pcm_lib_writev_transfer);
1920}
1921
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001922EXPORT_SYMBOL(snd_pcm_lib_writev);
1923
Takashi Iwai877211f2005-11-17 13:59:38 +01001924static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 unsigned int hwoff,
1926 unsigned long data, unsigned int off,
1927 snd_pcm_uframes_t frames)
1928{
Takashi Iwai877211f2005-11-17 13:59:38 +01001929 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int err;
1931 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1932 if (substream->ops->copy) {
1933 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1934 return err;
1935 } else {
1936 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1938 return -EFAULT;
1939 }
1940 return 0;
1941}
1942
Takashi Iwai877211f2005-11-17 13:59:38 +01001943static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 unsigned long data,
1945 snd_pcm_uframes_t size,
1946 int nonblock,
1947 transfer_f transfer)
1948{
Takashi Iwai877211f2005-11-17 13:59:38 +01001949 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 snd_pcm_uframes_t xfer = 0;
1951 snd_pcm_uframes_t offset = 0;
1952 int err = 0;
1953
1954 if (size == 0)
1955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 snd_pcm_stream_lock_irq(substream);
1958 switch (runtime->status->state) {
1959 case SNDRV_PCM_STATE_PREPARED:
1960 if (size >= runtime->start_threshold) {
1961 err = snd_pcm_start(substream);
1962 if (err < 0)
1963 goto _end_unlock;
1964 }
1965 break;
1966 case SNDRV_PCM_STATE_DRAINING:
1967 case SNDRV_PCM_STATE_RUNNING:
1968 case SNDRV_PCM_STATE_PAUSED:
1969 break;
1970 case SNDRV_PCM_STATE_XRUN:
1971 err = -EPIPE;
1972 goto _end_unlock;
1973 case SNDRV_PCM_STATE_SUSPENDED:
1974 err = -ESTRPIPE;
1975 goto _end_unlock;
1976 default:
1977 err = -EBADFD;
1978 goto _end_unlock;
1979 }
1980
1981 while (size > 0) {
1982 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1983 snd_pcm_uframes_t avail;
1984 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001985 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001988 if (!avail) {
1989 if (runtime->status->state ==
1990 SNDRV_PCM_STATE_DRAINING) {
1991 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto _end_unlock;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (nonblock) {
1995 err = -EAGAIN;
1996 goto _end_unlock;
1997 }
Takashi Iwai13075512008-01-08 18:08:14 +01001998 err = wait_for_avail_min(substream, &avail);
1999 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002001 if (!avail)
2002 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 frames = size > avail ? avail : size;
2005 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2006 if (frames > cont)
2007 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002008 if (snd_BUG_ON(!frames)) {
2009 snd_pcm_stream_unlock_irq(substream);
2010 return -EINVAL;
2011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 appl_ptr = runtime->control->appl_ptr;
2013 appl_ofs = appl_ptr % runtime->buffer_size;
2014 snd_pcm_stream_unlock_irq(substream);
2015 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2016 goto _end;
2017 snd_pcm_stream_lock_irq(substream);
2018 switch (runtime->status->state) {
2019 case SNDRV_PCM_STATE_XRUN:
2020 err = -EPIPE;
2021 goto _end_unlock;
2022 case SNDRV_PCM_STATE_SUSPENDED:
2023 err = -ESTRPIPE;
2024 goto _end_unlock;
2025 default:
2026 break;
2027 }
2028 appl_ptr += frames;
2029 if (appl_ptr >= runtime->boundary)
2030 appl_ptr -= runtime->boundary;
2031 runtime->control->appl_ptr = appl_ptr;
2032 if (substream->ops->ack)
2033 substream->ops->ack(substream);
2034
2035 offset += frames;
2036 size -= frames;
2037 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039 _end_unlock:
2040 snd_pcm_stream_unlock_irq(substream);
2041 _end:
2042 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2043}
2044
Takashi Iwai877211f2005-11-17 13:59:38 +01002045snd_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 -07002046{
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002051 err = pcm_sanity_check(substream);
2052 if (err < 0)
2053 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002055 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2057 return -EINVAL;
2058 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2059}
2060
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002061EXPORT_SYMBOL(snd_pcm_lib_read);
2062
Takashi Iwai877211f2005-11-17 13:59:38 +01002063static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 unsigned int hwoff,
2065 unsigned long data, unsigned int off,
2066 snd_pcm_uframes_t frames)
2067{
Takashi Iwai877211f2005-11-17 13:59:38 +01002068 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 int err;
2070 void __user **bufs = (void __user **)data;
2071 int channels = runtime->channels;
2072 int c;
2073 if (substream->ops->copy) {
2074 for (c = 0; c < channels; ++c, ++bufs) {
2075 char __user *buf;
2076 if (*bufs == NULL)
2077 continue;
2078 buf = *bufs + samples_to_bytes(runtime, off);
2079 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2080 return err;
2081 }
2082 } else {
2083 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 for (c = 0; c < channels; ++c, ++bufs) {
2085 char *hwbuf;
2086 char __user *buf;
2087 if (*bufs == NULL)
2088 continue;
2089
2090 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2091 buf = *bufs + samples_to_bytes(runtime, off);
2092 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2093 return -EFAULT;
2094 }
2095 }
2096 return 0;
2097}
2098
Takashi Iwai877211f2005-11-17 13:59:38 +01002099snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 void __user **bufs,
2101 snd_pcm_uframes_t frames)
2102{
Takashi Iwai877211f2005-11-17 13:59:38 +01002103 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002105 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002107 err = pcm_sanity_check(substream);
2108 if (err < 0)
2109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2112 return -EBADFD;
2113
Takashi Iwai0df63e42006-04-28 15:13:41 +02002114 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2116 return -EINVAL;
2117 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120EXPORT_SYMBOL(snd_pcm_lib_readv);