blob: 3b673e2f991d67b609f9c712cc4281a8cffc5111 [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) {
Takashi Iwai79452f02009-07-22 12:51:51 +0200247 if (runtime->periods == 1)
248 delta += runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100249 if (delta < 0) {
250 hw_ptr_error(substream,
251 "Unexpected hw_pointer value "
252 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
253 substream->stream, (long)pos,
254 (long)hw_ptr_interrupt);
Takashi Iwai79452f02009-07-22 12:51:51 +0200255#if 1
256 /* simply skipping the hwptr update seems more
257 * robust in some cases, e.g. on VMware with
258 * inaccurate timer source
259 */
260 return 0; /* skip this update */
261#else
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100262 /* rebase to interrupt position */
263 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100264 /* align hw_base to buffer_size */
265 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100266 delta = 0;
Takashi Iwai79452f02009-07-22 12:51:51 +0200267#endif
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100268 } else {
269 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100270 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100271 hw_base = 0;
272 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200275
276 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200277 if (!xrun_debug(substream, 4))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200278 goto no_jiffies_check;
279
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200280 /* Skip the jiffies check for hardwares with BATCH flag.
281 * Such hardware usually just increases the position at each IRQ,
282 * thus it can't give any strange position.
283 */
284 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
285 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200286 hdelta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200287 if (hdelta < runtime->delay)
288 goto no_jiffies_check;
289 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200290 jdelta = jiffies - runtime->hw_ptr_jiffies;
291 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
292 delta = jdelta /
293 (((runtime->period_size * HZ) / runtime->rate)
294 + HZ/100);
295 hw_ptr_error(substream,
296 "hw_ptr skipping! [Q] "
297 "(pos=%ld, delta=%ld, period=%ld, "
298 "jdelta=%lu/%lu/%lu)\n",
299 (long)pos, (long)hdelta,
300 (long)runtime->period_size, jdelta,
301 ((hdelta * HZ) / runtime->rate), delta);
302 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
303 runtime->period_size * delta;
304 if (hw_ptr_interrupt >= runtime->boundary)
305 hw_ptr_interrupt -= runtime->boundary;
306 /* rebase to interrupt position */
307 hw_base = new_hw_ptr = hw_ptr_interrupt;
308 /* align hw_base to buffer_size */
309 hw_base -= hw_base % runtime->buffer_size;
310 delta = 0;
311 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200312 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200313 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100314 hw_ptr_error(substream,
315 "Lost interrupts? "
316 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
317 substream->stream, (long)delta,
318 (long)hw_ptr_interrupt);
319 /* rebase hw_ptr_interrupt */
320 hw_ptr_interrupt =
321 new_hw_ptr - new_hw_ptr % runtime->period_size;
322 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200323 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
326 runtime->silence_size > 0)
327 snd_pcm_playback_silence(substream, new_hw_ptr);
328
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200329 if (runtime->status->hw_ptr == new_hw_ptr)
330 return 0;
331
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100332 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200334 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200335 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
336 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return snd_pcm_update_hw_ptr_post(substream, runtime);
339}
340
341/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100342int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai877211f2005-11-17 13:59:38 +0100344 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100346 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200348 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 old_hw_ptr = runtime->status->hw_ptr;
351 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
352 if (pos == SNDRV_PCM_POS_XRUN) {
353 xrun(substream);
354 return -EPIPE;
355 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100356 hw_base = runtime->hw_ptr_base;
357 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100359 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200360 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100361 if (delta < 0) {
362 delta += runtime->buffer_size;
363 if (delta < 0) {
364 hw_ptr_error(substream,
365 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200366 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100367 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200368 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100371 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100372 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100373 hw_base = 0;
374 new_hw_ptr = hw_base + pos;
375 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200376 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200377 if (!xrun_debug(substream, 4))
378 goto no_jiffies_check;
379 if (delta < runtime->delay)
380 goto no_jiffies_check;
381 delta -= runtime->delay;
382 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100383 hw_ptr_error(substream,
384 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200385 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100386 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200387 (long)runtime->period_size, jdelta,
388 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200391 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
393 runtime->silence_size > 0)
394 snd_pcm_playback_silence(substream, new_hw_ptr);
395
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200396 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200397 return 0;
398
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100399 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200401 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200402 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
403 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 return snd_pcm_update_hw_ptr_post(substream, runtime);
406}
407
408/**
409 * snd_pcm_set_ops - set the PCM operators
410 * @pcm: the pcm instance
411 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
412 * @ops: the operator table
413 *
414 * Sets the given PCM operators to the pcm instance.
415 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100416void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Takashi Iwai877211f2005-11-17 13:59:38 +0100418 struct snd_pcm_str *stream = &pcm->streams[direction];
419 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 for (substream = stream->substream; substream != NULL; substream = substream->next)
422 substream->ops = ops;
423}
424
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200425EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/**
428 * snd_pcm_sync - set the PCM sync id
429 * @substream: the pcm substream
430 *
431 * Sets the PCM sync identifier for the card.
432 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100433void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Takashi Iwai877211f2005-11-17 13:59:38 +0100435 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 runtime->sync.id32[0] = substream->pcm->card->number;
438 runtime->sync.id32[1] = -1;
439 runtime->sync.id32[2] = -1;
440 runtime->sync.id32[3] = -1;
441}
442
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200443EXPORT_SYMBOL(snd_pcm_set_sync);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/*
446 * Standard ioctl routine
447 */
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static inline unsigned int div32(unsigned int a, unsigned int b,
450 unsigned int *r)
451{
452 if (b == 0) {
453 *r = 0;
454 return UINT_MAX;
455 }
456 *r = a % b;
457 return a / b;
458}
459
460static inline unsigned int div_down(unsigned int a, unsigned int b)
461{
462 if (b == 0)
463 return UINT_MAX;
464 return a / b;
465}
466
467static inline unsigned int div_up(unsigned int a, unsigned int b)
468{
469 unsigned int r;
470 unsigned int q;
471 if (b == 0)
472 return UINT_MAX;
473 q = div32(a, b, &r);
474 if (r)
475 ++q;
476 return q;
477}
478
479static inline unsigned int mul(unsigned int a, unsigned int b)
480{
481 if (a == 0)
482 return 0;
483 if (div_down(UINT_MAX, a) < b)
484 return UINT_MAX;
485 return a * b;
486}
487
488static inline unsigned int muldiv32(unsigned int a, unsigned int b,
489 unsigned int c, unsigned int *r)
490{
491 u_int64_t n = (u_int64_t) a * b;
492 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200493 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *r = 0;
495 return UINT_MAX;
496 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200497 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (n >= UINT_MAX) {
499 *r = 0;
500 return UINT_MAX;
501 }
502 return n;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
506 * snd_interval_refine - refine the interval value of configurator
507 * @i: the interval value to refine
508 * @v: the interval value to refer to
509 *
510 * Refines the interval value with the reference value.
511 * The interval is changed to the range satisfying both intervals.
512 * The interval status (min, max, integer, etc.) are evaluated.
513 *
514 * Returns non-zero if the value is changed, zero if not changed.
515 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100516int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200519 if (snd_BUG_ON(snd_interval_empty(i)))
520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (i->min < v->min) {
522 i->min = v->min;
523 i->openmin = v->openmin;
524 changed = 1;
525 } else if (i->min == v->min && !i->openmin && v->openmin) {
526 i->openmin = 1;
527 changed = 1;
528 }
529 if (i->max > v->max) {
530 i->max = v->max;
531 i->openmax = v->openmax;
532 changed = 1;
533 } else if (i->max == v->max && !i->openmax && v->openmax) {
534 i->openmax = 1;
535 changed = 1;
536 }
537 if (!i->integer && v->integer) {
538 i->integer = 1;
539 changed = 1;
540 }
541 if (i->integer) {
542 if (i->openmin) {
543 i->min++;
544 i->openmin = 0;
545 }
546 if (i->openmax) {
547 i->max--;
548 i->openmax = 0;
549 }
550 } else if (!i->openmin && !i->openmax && i->min == i->max)
551 i->integer = 1;
552 if (snd_interval_checkempty(i)) {
553 snd_interval_none(i);
554 return -EINVAL;
555 }
556 return changed;
557}
558
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200559EXPORT_SYMBOL(snd_interval_refine);
560
Takashi Iwai877211f2005-11-17 13:59:38 +0100561static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200563 if (snd_BUG_ON(snd_interval_empty(i)))
564 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (snd_interval_single(i))
566 return 0;
567 i->max = i->min;
568 i->openmax = i->openmin;
569 if (i->openmax)
570 i->max++;
571 return 1;
572}
573
Takashi Iwai877211f2005-11-17 13:59:38 +0100574static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200576 if (snd_BUG_ON(snd_interval_empty(i)))
577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (snd_interval_single(i))
579 return 0;
580 i->min = i->max;
581 i->openmin = i->openmax;
582 if (i->openmin)
583 i->min--;
584 return 1;
585}
586
Takashi Iwai877211f2005-11-17 13:59:38 +0100587void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (a->empty || b->empty) {
590 snd_interval_none(c);
591 return;
592 }
593 c->empty = 0;
594 c->min = mul(a->min, b->min);
595 c->openmin = (a->openmin || b->openmin);
596 c->max = mul(a->max, b->max);
597 c->openmax = (a->openmax || b->openmax);
598 c->integer = (a->integer && b->integer);
599}
600
601/**
602 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200603 * @a: dividend
604 * @b: divisor
605 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *
607 * c = a / b
608 *
609 * Returns non-zero if the value is changed, zero if not changed.
610 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100611void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 unsigned int r;
614 if (a->empty || b->empty) {
615 snd_interval_none(c);
616 return;
617 }
618 c->empty = 0;
619 c->min = div32(a->min, b->max, &r);
620 c->openmin = (r || a->openmin || b->openmax);
621 if (b->min > 0) {
622 c->max = div32(a->max, b->min, &r);
623 if (r) {
624 c->max++;
625 c->openmax = 1;
626 } else
627 c->openmax = (a->openmax || b->openmin);
628 } else {
629 c->max = UINT_MAX;
630 c->openmax = 0;
631 }
632 c->integer = 0;
633}
634
635/**
636 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200637 * @a: dividend 1
638 * @b: dividend 2
639 * @k: divisor (as integer)
640 * @c: result
641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * c = a * b / k
643 *
644 * Returns non-zero if the value is changed, zero if not changed.
645 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100646void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
647 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 unsigned int r;
650 if (a->empty || b->empty) {
651 snd_interval_none(c);
652 return;
653 }
654 c->empty = 0;
655 c->min = muldiv32(a->min, b->min, k, &r);
656 c->openmin = (r || a->openmin || b->openmin);
657 c->max = muldiv32(a->max, b->max, k, &r);
658 if (r) {
659 c->max++;
660 c->openmax = 1;
661 } else
662 c->openmax = (a->openmax || b->openmax);
663 c->integer = 0;
664}
665
666/**
667 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200668 * @a: dividend 1
669 * @k: dividend 2 (as integer)
670 * @b: divisor
671 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
673 * c = a * k / b
674 *
675 * Returns non-zero if the value is changed, zero if not changed.
676 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100677void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
678 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 unsigned int r;
681 if (a->empty || b->empty) {
682 snd_interval_none(c);
683 return;
684 }
685 c->empty = 0;
686 c->min = muldiv32(a->min, k, b->max, &r);
687 c->openmin = (r || a->openmin || b->openmax);
688 if (b->min > 0) {
689 c->max = muldiv32(a->max, k, b->min, &r);
690 if (r) {
691 c->max++;
692 c->openmax = 1;
693 } else
694 c->openmax = (a->openmax || b->openmin);
695 } else {
696 c->max = UINT_MAX;
697 c->openmax = 0;
698 }
699 c->integer = 0;
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* ---- */
703
704
705/**
706 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200707 * @i: interval to refine
708 * @rats_count: number of ratnum_t
709 * @rats: ratnum_t array
710 * @nump: pointer to store the resultant numerator
711 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *
713 * Returns non-zero if the value is changed, zero if not changed.
714 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100715int snd_interval_ratnum(struct snd_interval *i,
716 unsigned int rats_count, struct snd_ratnum *rats,
717 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 unsigned int best_num, best_diff, best_den;
720 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100721 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int err;
723
724 best_num = best_den = best_diff = 0;
725 for (k = 0; k < rats_count; ++k) {
726 unsigned int num = rats[k].num;
727 unsigned int den;
728 unsigned int q = i->min;
729 int diff;
730 if (q == 0)
731 q = 1;
732 den = div_down(num, q);
733 if (den < rats[k].den_min)
734 continue;
735 if (den > rats[k].den_max)
736 den = rats[k].den_max;
737 else {
738 unsigned int r;
739 r = (den - rats[k].den_min) % rats[k].den_step;
740 if (r != 0)
741 den -= r;
742 }
743 diff = num - q * den;
744 if (best_num == 0 ||
745 diff * best_den < best_diff * den) {
746 best_diff = diff;
747 best_den = den;
748 best_num = num;
749 }
750 }
751 if (best_den == 0) {
752 i->empty = 1;
753 return -EINVAL;
754 }
755 t.min = div_down(best_num, best_den);
756 t.openmin = !!(best_num % best_den);
757
758 best_num = best_den = best_diff = 0;
759 for (k = 0; k < rats_count; ++k) {
760 unsigned int num = rats[k].num;
761 unsigned int den;
762 unsigned int q = i->max;
763 int diff;
764 if (q == 0) {
765 i->empty = 1;
766 return -EINVAL;
767 }
768 den = div_up(num, q);
769 if (den > rats[k].den_max)
770 continue;
771 if (den < rats[k].den_min)
772 den = rats[k].den_min;
773 else {
774 unsigned int r;
775 r = (den - rats[k].den_min) % rats[k].den_step;
776 if (r != 0)
777 den += rats[k].den_step - r;
778 }
779 diff = q * den - num;
780 if (best_num == 0 ||
781 diff * best_den < best_diff * den) {
782 best_diff = diff;
783 best_den = den;
784 best_num = num;
785 }
786 }
787 if (best_den == 0) {
788 i->empty = 1;
789 return -EINVAL;
790 }
791 t.max = div_up(best_num, best_den);
792 t.openmax = !!(best_num % best_den);
793 t.integer = 0;
794 err = snd_interval_refine(i, &t);
795 if (err < 0)
796 return err;
797
798 if (snd_interval_single(i)) {
799 if (nump)
800 *nump = best_num;
801 if (denp)
802 *denp = best_den;
803 }
804 return err;
805}
806
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200807EXPORT_SYMBOL(snd_interval_ratnum);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/**
810 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200811 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100812 * @rats_count: number of struct ratden
813 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200814 * @nump: pointer to store the resultant numerator
815 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
817 * Returns non-zero if the value is changed, zero if not changed.
818 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100819static int snd_interval_ratden(struct snd_interval *i,
820 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 unsigned int *nump, unsigned int *denp)
822{
823 unsigned int best_num, best_diff, best_den;
824 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100825 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int err;
827
828 best_num = best_den = best_diff = 0;
829 for (k = 0; k < rats_count; ++k) {
830 unsigned int num;
831 unsigned int den = rats[k].den;
832 unsigned int q = i->min;
833 int diff;
834 num = mul(q, den);
835 if (num > rats[k].num_max)
836 continue;
837 if (num < rats[k].num_min)
838 num = rats[k].num_max;
839 else {
840 unsigned int r;
841 r = (num - rats[k].num_min) % rats[k].num_step;
842 if (r != 0)
843 num += rats[k].num_step - r;
844 }
845 diff = num - q * den;
846 if (best_num == 0 ||
847 diff * best_den < best_diff * den) {
848 best_diff = diff;
849 best_den = den;
850 best_num = num;
851 }
852 }
853 if (best_den == 0) {
854 i->empty = 1;
855 return -EINVAL;
856 }
857 t.min = div_down(best_num, best_den);
858 t.openmin = !!(best_num % best_den);
859
860 best_num = best_den = best_diff = 0;
861 for (k = 0; k < rats_count; ++k) {
862 unsigned int num;
863 unsigned int den = rats[k].den;
864 unsigned int q = i->max;
865 int diff;
866 num = mul(q, den);
867 if (num < rats[k].num_min)
868 continue;
869 if (num > rats[k].num_max)
870 num = rats[k].num_max;
871 else {
872 unsigned int r;
873 r = (num - rats[k].num_min) % rats[k].num_step;
874 if (r != 0)
875 num -= r;
876 }
877 diff = q * den - num;
878 if (best_num == 0 ||
879 diff * best_den < best_diff * den) {
880 best_diff = diff;
881 best_den = den;
882 best_num = num;
883 }
884 }
885 if (best_den == 0) {
886 i->empty = 1;
887 return -EINVAL;
888 }
889 t.max = div_up(best_num, best_den);
890 t.openmax = !!(best_num % best_den);
891 t.integer = 0;
892 err = snd_interval_refine(i, &t);
893 if (err < 0)
894 return err;
895
896 if (snd_interval_single(i)) {
897 if (nump)
898 *nump = best_num;
899 if (denp)
900 *denp = best_den;
901 }
902 return err;
903}
904
905/**
906 * snd_interval_list - refine the interval value from the list
907 * @i: the interval value to refine
908 * @count: the number of elements in the list
909 * @list: the value list
910 * @mask: the bit-mask to evaluate
911 *
912 * Refines the interval value from the list.
913 * When mask is non-zero, only the elements corresponding to bit 1 are
914 * evaluated.
915 *
916 * Returns non-zero if the value is changed, zero if not changed.
917 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100918int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 unsigned int k;
921 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100922
923 if (!count) {
924 i->empty = 1;
925 return -EINVAL;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 for (k = 0; k < count; k++) {
928 if (mask && !(mask & (1 << k)))
929 continue;
930 if (i->min == list[k] && !i->openmin)
931 goto _l1;
932 if (i->min < list[k]) {
933 i->min = list[k];
934 i->openmin = 0;
935 changed = 1;
936 goto _l1;
937 }
938 }
939 i->empty = 1;
940 return -EINVAL;
941 _l1:
942 for (k = count; k-- > 0;) {
943 if (mask && !(mask & (1 << k)))
944 continue;
945 if (i->max == list[k] && !i->openmax)
946 goto _l2;
947 if (i->max > list[k]) {
948 i->max = list[k];
949 i->openmax = 0;
950 changed = 1;
951 goto _l2;
952 }
953 }
954 i->empty = 1;
955 return -EINVAL;
956 _l2:
957 if (snd_interval_checkempty(i)) {
958 i->empty = 1;
959 return -EINVAL;
960 }
961 return changed;
962}
963
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200964EXPORT_SYMBOL(snd_interval_list);
965
Takashi Iwai877211f2005-11-17 13:59:38 +0100966static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 unsigned int n;
969 int changed = 0;
970 n = (i->min - min) % step;
971 if (n != 0 || i->openmin) {
972 i->min += step - n;
973 changed = 1;
974 }
975 n = (i->max - min) % step;
976 if (n != 0 || i->openmax) {
977 i->max -= n;
978 changed = 1;
979 }
980 if (snd_interval_checkempty(i)) {
981 i->empty = 1;
982 return -EINVAL;
983 }
984 return changed;
985}
986
987/* Info constraints helpers */
988
989/**
990 * snd_pcm_hw_rule_add - add the hw-constraint rule
991 * @runtime: the pcm runtime instance
992 * @cond: condition bits
993 * @var: the variable to evaluate
994 * @func: the evaluation function
995 * @private: the private data pointer passed to function
996 * @dep: the dependent variables
997 *
998 * Returns zero if successful, or a negative error code on failure.
999 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001000int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int var,
1002 snd_pcm_hw_rule_func_t func, void *private,
1003 int dep, ...)
1004{
Takashi Iwai877211f2005-11-17 13:59:38 +01001005 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1006 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 unsigned int k;
1008 va_list args;
1009 va_start(args, dep);
1010 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unsigned int new_rules = constrs->rules_all + 16;
1013 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1014 if (!new)
1015 return -ENOMEM;
1016 if (constrs->rules) {
1017 memcpy(new, constrs->rules,
1018 constrs->rules_num * sizeof(*c));
1019 kfree(constrs->rules);
1020 }
1021 constrs->rules = new;
1022 constrs->rules_all = new_rules;
1023 }
1024 c = &constrs->rules[constrs->rules_num];
1025 c->cond = cond;
1026 c->func = func;
1027 c->var = var;
1028 c->private = private;
1029 k = 0;
1030 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001031 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 c->deps[k++] = dep;
1034 if (dep < 0)
1035 break;
1036 dep = va_arg(args, int);
1037 }
1038 constrs->rules_num++;
1039 va_end(args);
1040 return 0;
1041}
1042
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001043EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001046 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001047 * @runtime: PCM runtime instance
1048 * @var: hw_params variable to apply the mask
1049 * @mask: the bitmap mask
1050 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001051 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001053int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 u_int32_t mask)
1055{
Takashi Iwai877211f2005-11-17 13:59:38 +01001056 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1057 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *maskp->bits &= mask;
1059 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1060 if (*maskp->bits == 0)
1061 return -EINVAL;
1062 return 0;
1063}
1064
1065/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001066 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001067 * @runtime: PCM runtime instance
1068 * @var: hw_params variable to apply the mask
1069 * @mask: the 64bit bitmap mask
1070 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001071 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001073int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 u_int64_t mask)
1075{
Takashi Iwai877211f2005-11-17 13:59:38 +01001076 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1077 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 maskp->bits[0] &= (u_int32_t)mask;
1079 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1080 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1081 if (! maskp->bits[0] && ! maskp->bits[1])
1082 return -EINVAL;
1083 return 0;
1084}
1085
1086/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001087 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001088 * @runtime: PCM runtime instance
1089 * @var: hw_params variable to apply the integer constraint
1090 *
1091 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001093int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Takashi Iwai877211f2005-11-17 13:59:38 +01001095 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return snd_interval_setinteger(constrs_interval(constrs, var));
1097}
1098
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001099EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001102 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001103 * @runtime: PCM runtime instance
1104 * @var: hw_params variable to apply the range
1105 * @min: the minimal value
1106 * @max: the maximal value
1107 *
1108 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001110int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 unsigned int min, unsigned int max)
1112{
Takashi Iwai877211f2005-11-17 13:59:38 +01001113 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1114 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 t.min = min;
1116 t.max = max;
1117 t.openmin = t.openmax = 0;
1118 t.integer = 0;
1119 return snd_interval_refine(constrs_interval(constrs, var), &t);
1120}
1121
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001122EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1123
Takashi Iwai877211f2005-11-17 13:59:38 +01001124static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1125 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwai877211f2005-11-17 13:59:38 +01001127 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1129}
1130
1131
1132/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001133 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001134 * @runtime: PCM runtime instance
1135 * @cond: condition bits
1136 * @var: hw_params variable to apply the list constraint
1137 * @l: list
1138 *
1139 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001141int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned int cond,
1143 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001144 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 return snd_pcm_hw_rule_add(runtime, cond, var,
1147 snd_pcm_hw_rule_list, l,
1148 var, -1);
1149}
1150
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001151EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1152
Takashi Iwai877211f2005-11-17 13:59:38 +01001153static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1154 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai877211f2005-11-17 13:59:38 +01001156 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 unsigned int num = 0, den = 0;
1158 int err;
1159 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1160 r->nrats, r->rats, &num, &den);
1161 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1162 params->rate_num = num;
1163 params->rate_den = den;
1164 }
1165 return err;
1166}
1167
1168/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001169 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001170 * @runtime: PCM runtime instance
1171 * @cond: condition bits
1172 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001173 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001175int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned int cond,
1177 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001178 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 return snd_pcm_hw_rule_add(runtime, cond, var,
1181 snd_pcm_hw_rule_ratnums, r,
1182 var, -1);
1183}
1184
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001185EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1188 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Takashi Iwai877211f2005-11-17 13:59:38 +01001190 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 unsigned int num = 0, den = 0;
1192 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1193 r->nrats, r->rats, &num, &den);
1194 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1195 params->rate_num = num;
1196 params->rate_den = den;
1197 }
1198 return err;
1199}
1200
1201/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001202 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001203 * @runtime: PCM runtime instance
1204 * @cond: condition bits
1205 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001208int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unsigned int cond,
1210 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001211 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 return snd_pcm_hw_rule_add(runtime, cond, var,
1214 snd_pcm_hw_rule_ratdens, r,
1215 var, -1);
1216}
1217
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001218EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1219
Takashi Iwai877211f2005-11-17 13:59:38 +01001220static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1221 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 unsigned int l = (unsigned long) rule->private;
1224 int width = l & 0xffff;
1225 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001226 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (snd_interval_single(i) && snd_interval_value(i) == width)
1228 params->msbits = msbits;
1229 return 0;
1230}
1231
1232/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001233 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001234 * @runtime: PCM runtime instance
1235 * @cond: condition bits
1236 * @width: sample bits width
1237 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001239int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 unsigned int cond,
1241 unsigned int width,
1242 unsigned int msbits)
1243{
1244 unsigned long l = (msbits << 16) | width;
1245 return snd_pcm_hw_rule_add(runtime, cond, -1,
1246 snd_pcm_hw_rule_msbits,
1247 (void*) l,
1248 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1249}
1250
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001251EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1252
Takashi Iwai877211f2005-11-17 13:59:38 +01001253static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1254 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 unsigned long step = (unsigned long) rule->private;
1257 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1258}
1259
1260/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001261 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001262 * @runtime: PCM runtime instance
1263 * @cond: condition bits
1264 * @var: hw_params variable to apply the step constraint
1265 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001267int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned int cond,
1269 snd_pcm_hw_param_t var,
1270 unsigned long step)
1271{
1272 return snd_pcm_hw_rule_add(runtime, cond, var,
1273 snd_pcm_hw_rule_step, (void *) step,
1274 var, -1);
1275}
1276
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001277EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1278
Takashi Iwai877211f2005-11-17 13:59:38 +01001279static 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 -07001280{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001281 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1283 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1284 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1285 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1286 };
1287 return snd_interval_list(hw_param_interval(params, rule->var),
1288 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1289}
1290
1291/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001292 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001293 * @runtime: PCM runtime instance
1294 * @cond: condition bits
1295 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001297int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 unsigned int cond,
1299 snd_pcm_hw_param_t var)
1300{
1301 return snd_pcm_hw_rule_add(runtime, cond, var,
1302 snd_pcm_hw_rule_pow2, NULL,
1303 var, -1);
1304}
1305
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001306EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1307
Takashi Iwai877211f2005-11-17 13:59:38 +01001308static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001309 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 if (hw_is_mask(var)) {
1312 snd_mask_any(hw_param_mask(params, var));
1313 params->cmask |= 1 << var;
1314 params->rmask |= 1 << var;
1315 return;
1316 }
1317 if (hw_is_interval(var)) {
1318 snd_interval_any(hw_param_interval(params, var));
1319 params->cmask |= 1 << var;
1320 params->rmask |= 1 << var;
1321 return;
1322 }
1323 snd_BUG();
1324}
1325
Takashi Iwai877211f2005-11-17 13:59:38 +01001326void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 unsigned int k;
1329 memset(params, 0, sizeof(*params));
1330 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1331 _snd_pcm_hw_param_any(params, k);
1332 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1333 _snd_pcm_hw_param_any(params, k);
1334 params->info = ~0U;
1335}
1336
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001337EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001340 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001341 * @params: the hw_params instance
1342 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001343 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001345 * Return the value for field @var if it's fixed in configuration space
1346 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001348int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1349 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001352 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (!snd_mask_single(mask))
1354 return -EINVAL;
1355 if (dir)
1356 *dir = 0;
1357 return snd_mask_value(mask);
1358 }
1359 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001360 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!snd_interval_single(i))
1362 return -EINVAL;
1363 if (dir)
1364 *dir = i->openmin;
1365 return snd_interval_value(i);
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EINVAL;
1368}
1369
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001370EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Takashi Iwai877211f2005-11-17 13:59:38 +01001372void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 snd_pcm_hw_param_t var)
1374{
1375 if (hw_is_mask(var)) {
1376 snd_mask_none(hw_param_mask(params, var));
1377 params->cmask |= 1 << var;
1378 params->rmask |= 1 << var;
1379 } else if (hw_is_interval(var)) {
1380 snd_interval_none(hw_param_interval(params, var));
1381 params->cmask |= 1 << var;
1382 params->rmask |= 1 << var;
1383 } else {
1384 snd_BUG();
1385 }
1386}
1387
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001388EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Takashi Iwai877211f2005-11-17 13:59:38 +01001390static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001391 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 int changed;
1394 if (hw_is_mask(var))
1395 changed = snd_mask_refine_first(hw_param_mask(params, var));
1396 else if (hw_is_interval(var))
1397 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001398 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (changed) {
1401 params->cmask |= 1 << var;
1402 params->rmask |= 1 << var;
1403 }
1404 return changed;
1405}
1406
1407
1408/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001409 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001410 * @pcm: PCM instance
1411 * @params: the hw_params instance
1412 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001413 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001415 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * values > minimum. Reduce configuration space accordingly.
1417 * Return the minimum.
1418 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001419int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1420 struct snd_pcm_hw_params *params,
1421 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 int changed = _snd_pcm_hw_param_first(params, var);
1424 if (changed < 0)
1425 return changed;
1426 if (params->rmask) {
1427 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001428 if (snd_BUG_ON(err < 0))
1429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 return snd_pcm_hw_param_value(params, var, dir);
1432}
1433
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001434EXPORT_SYMBOL(snd_pcm_hw_param_first);
1435
Takashi Iwai877211f2005-11-17 13:59:38 +01001436static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001437 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int changed;
1440 if (hw_is_mask(var))
1441 changed = snd_mask_refine_last(hw_param_mask(params, var));
1442 else if (hw_is_interval(var))
1443 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001444 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (changed) {
1447 params->cmask |= 1 << var;
1448 params->rmask |= 1 << var;
1449 }
1450 return changed;
1451}
1452
1453
1454/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001455 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001456 * @pcm: PCM instance
1457 * @params: the hw_params instance
1458 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001459 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001461 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * values < maximum. Reduce configuration space accordingly.
1463 * Return the maximum.
1464 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001465int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1466 struct snd_pcm_hw_params *params,
1467 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 int changed = _snd_pcm_hw_param_last(params, var);
1470 if (changed < 0)
1471 return changed;
1472 if (params->rmask) {
1473 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001474 if (snd_BUG_ON(err < 0))
1475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477 return snd_pcm_hw_param_value(params, var, dir);
1478}
1479
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001480EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001483 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001484 * @pcm: PCM instance
1485 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001487 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * The configuration chosen is that obtained fixing in this order:
1489 * first access, first format, first subformat, min channels,
1490 * min rate, min period time, max buffer size, min tick time
1491 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001492int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1493 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001495 static int vars[] = {
1496 SNDRV_PCM_HW_PARAM_ACCESS,
1497 SNDRV_PCM_HW_PARAM_FORMAT,
1498 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1499 SNDRV_PCM_HW_PARAM_CHANNELS,
1500 SNDRV_PCM_HW_PARAM_RATE,
1501 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1502 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1503 SNDRV_PCM_HW_PARAM_TICK_TIME,
1504 -1
1505 };
1506 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001508 for (v = vars; *v != -1; v++) {
1509 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1510 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1511 else
1512 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001513 if (snd_BUG_ON(err < 0))
1514 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return 0;
1517}
1518
Takashi Iwai877211f2005-11-17 13:59:38 +01001519static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 void *arg)
1521{
Takashi Iwai877211f2005-11-17 13:59:38 +01001522 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 unsigned long flags;
1524 snd_pcm_stream_lock_irqsave(substream, flags);
1525 if (snd_pcm_running(substream) &&
1526 snd_pcm_update_hw_ptr(substream) >= 0)
1527 runtime->status->hw_ptr %= runtime->buffer_size;
1528 else
1529 runtime->status->hw_ptr = 0;
1530 snd_pcm_stream_unlock_irqrestore(substream, flags);
1531 return 0;
1532}
1533
Takashi Iwai877211f2005-11-17 13:59:38 +01001534static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 void *arg)
1536{
Takashi Iwai877211f2005-11-17 13:59:38 +01001537 struct snd_pcm_channel_info *info = arg;
1538 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int width;
1540 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1541 info->offset = -1;
1542 return 0;
1543 }
1544 width = snd_pcm_format_physical_width(runtime->format);
1545 if (width < 0)
1546 return width;
1547 info->offset = 0;
1548 switch (runtime->access) {
1549 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1550 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1551 info->first = info->channel * width;
1552 info->step = runtime->channels * width;
1553 break;
1554 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1555 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1556 {
1557 size_t size = runtime->dma_bytes / runtime->channels;
1558 info->first = info->channel * size * 8;
1559 info->step = width;
1560 break;
1561 }
1562 default:
1563 snd_BUG();
1564 break;
1565 }
1566 return 0;
1567}
1568
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001569static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1570 void *arg)
1571{
1572 struct snd_pcm_hw_params *params = arg;
1573 snd_pcm_format_t format;
1574 int channels, width;
1575
1576 params->fifo_size = substream->runtime->hw.fifo_size;
1577 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1578 format = params_format(params);
1579 channels = params_channels(params);
1580 width = snd_pcm_format_physical_width(format);
1581 params->fifo_size /= width * channels;
1582 }
1583 return 0;
1584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/**
1587 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1588 * @substream: the pcm substream instance
1589 * @cmd: ioctl command
1590 * @arg: ioctl argument
1591 *
1592 * Processes the generic ioctl commands for PCM.
1593 * Can be passed as the ioctl callback for PCM ops.
1594 *
1595 * Returns zero if successful, or a negative error code on failure.
1596 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001597int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 unsigned int cmd, void *arg)
1599{
1600 switch (cmd) {
1601 case SNDRV_PCM_IOCTL1_INFO:
1602 return 0;
1603 case SNDRV_PCM_IOCTL1_RESET:
1604 return snd_pcm_lib_ioctl_reset(substream, arg);
1605 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1606 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001607 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1608 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 return -ENXIO;
1611}
1612
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001613EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/**
1616 * snd_pcm_period_elapsed - update the pcm status for the next period
1617 * @substream: the pcm substream instance
1618 *
1619 * This function is called from the interrupt handler when the
1620 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001621 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 *
1623 * Even if more than one periods have elapsed since the last call, you
1624 * have to call this only once.
1625 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001626void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Takashi Iwai877211f2005-11-17 13:59:38 +01001628 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 unsigned long flags;
1630
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001631 if (PCM_RUNTIME_CHECK(substream))
1632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (runtime->transfer_ack_begin)
1636 runtime->transfer_ack_begin(substream);
1637
1638 snd_pcm_stream_lock_irqsave(substream, flags);
1639 if (!snd_pcm_running(substream) ||
1640 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1641 goto _end;
1642
1643 if (substream->timer_running)
1644 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 _end:
1646 snd_pcm_stream_unlock_irqrestore(substream, flags);
1647 if (runtime->transfer_ack_end)
1648 runtime->transfer_ack_end(substream);
1649 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1650}
1651
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001652EXPORT_SYMBOL(snd_pcm_period_elapsed);
1653
Takashi Iwai13075512008-01-08 18:08:14 +01001654/*
1655 * Wait until avail_min data becomes available
1656 * Returns a negative error code if any error occurs during operation.
1657 * The available space is stored on availp. When err = 0 and avail = 0
1658 * on the capture stream, it indicates the stream is in DRAINING state.
1659 */
1660static int wait_for_avail_min(struct snd_pcm_substream *substream,
1661 snd_pcm_uframes_t *availp)
1662{
1663 struct snd_pcm_runtime *runtime = substream->runtime;
1664 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1665 wait_queue_t wait;
1666 int err = 0;
1667 snd_pcm_uframes_t avail = 0;
1668 long tout;
1669
1670 init_waitqueue_entry(&wait, current);
1671 add_wait_queue(&runtime->sleep, &wait);
1672 for (;;) {
1673 if (signal_pending(current)) {
1674 err = -ERESTARTSYS;
1675 break;
1676 }
1677 set_current_state(TASK_INTERRUPTIBLE);
1678 snd_pcm_stream_unlock_irq(substream);
1679 tout = schedule_timeout(msecs_to_jiffies(10000));
1680 snd_pcm_stream_lock_irq(substream);
1681 switch (runtime->status->state) {
1682 case SNDRV_PCM_STATE_SUSPENDED:
1683 err = -ESTRPIPE;
1684 goto _endloop;
1685 case SNDRV_PCM_STATE_XRUN:
1686 err = -EPIPE;
1687 goto _endloop;
1688 case SNDRV_PCM_STATE_DRAINING:
1689 if (is_playback)
1690 err = -EPIPE;
1691 else
1692 avail = 0; /* indicate draining */
1693 goto _endloop;
1694 case SNDRV_PCM_STATE_OPEN:
1695 case SNDRV_PCM_STATE_SETUP:
1696 case SNDRV_PCM_STATE_DISCONNECTED:
1697 err = -EBADFD;
1698 goto _endloop;
1699 }
1700 if (!tout) {
1701 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1702 is_playback ? "playback" : "capture");
1703 err = -EIO;
1704 break;
1705 }
1706 if (is_playback)
1707 avail = snd_pcm_playback_avail(runtime);
1708 else
1709 avail = snd_pcm_capture_avail(runtime);
1710 if (avail >= runtime->control->avail_min)
1711 break;
1712 }
1713 _endloop:
1714 remove_wait_queue(&runtime->sleep, &wait);
1715 *availp = avail;
1716 return err;
1717}
1718
Takashi Iwai877211f2005-11-17 13:59:38 +01001719static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned int hwoff,
1721 unsigned long data, unsigned int off,
1722 snd_pcm_uframes_t frames)
1723{
Takashi Iwai877211f2005-11-17 13:59:38 +01001724 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int err;
1726 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1727 if (substream->ops->copy) {
1728 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1729 return err;
1730 } else {
1731 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1733 return -EFAULT;
1734 }
1735 return 0;
1736}
1737
Takashi Iwai877211f2005-11-17 13:59:38 +01001738typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 unsigned long data, unsigned int off,
1740 snd_pcm_uframes_t size);
1741
Takashi Iwai877211f2005-11-17 13:59:38 +01001742static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned long data,
1744 snd_pcm_uframes_t size,
1745 int nonblock,
1746 transfer_f transfer)
1747{
Takashi Iwai877211f2005-11-17 13:59:38 +01001748 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 snd_pcm_uframes_t xfer = 0;
1750 snd_pcm_uframes_t offset = 0;
1751 int err = 0;
1752
1753 if (size == 0)
1754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 snd_pcm_stream_lock_irq(substream);
1757 switch (runtime->status->state) {
1758 case SNDRV_PCM_STATE_PREPARED:
1759 case SNDRV_PCM_STATE_RUNNING:
1760 case SNDRV_PCM_STATE_PAUSED:
1761 break;
1762 case SNDRV_PCM_STATE_XRUN:
1763 err = -EPIPE;
1764 goto _end_unlock;
1765 case SNDRV_PCM_STATE_SUSPENDED:
1766 err = -ESTRPIPE;
1767 goto _end_unlock;
1768 default:
1769 err = -EBADFD;
1770 goto _end_unlock;
1771 }
1772
1773 while (size > 0) {
1774 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1775 snd_pcm_uframes_t avail;
1776 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001777 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 snd_pcm_update_hw_ptr(substream);
1779 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001780 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (nonblock) {
1782 err = -EAGAIN;
1783 goto _end_unlock;
1784 }
Takashi Iwai13075512008-01-08 18:08:14 +01001785 err = wait_for_avail_min(substream, &avail);
1786 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 frames = size > avail ? avail : size;
1790 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1791 if (frames > cont)
1792 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001793 if (snd_BUG_ON(!frames)) {
1794 snd_pcm_stream_unlock_irq(substream);
1795 return -EINVAL;
1796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 appl_ptr = runtime->control->appl_ptr;
1798 appl_ofs = appl_ptr % runtime->buffer_size;
1799 snd_pcm_stream_unlock_irq(substream);
1800 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1801 goto _end;
1802 snd_pcm_stream_lock_irq(substream);
1803 switch (runtime->status->state) {
1804 case SNDRV_PCM_STATE_XRUN:
1805 err = -EPIPE;
1806 goto _end_unlock;
1807 case SNDRV_PCM_STATE_SUSPENDED:
1808 err = -ESTRPIPE;
1809 goto _end_unlock;
1810 default:
1811 break;
1812 }
1813 appl_ptr += frames;
1814 if (appl_ptr >= runtime->boundary)
1815 appl_ptr -= runtime->boundary;
1816 runtime->control->appl_ptr = appl_ptr;
1817 if (substream->ops->ack)
1818 substream->ops->ack(substream);
1819
1820 offset += frames;
1821 size -= frames;
1822 xfer += frames;
1823 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1824 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1825 err = snd_pcm_start(substream);
1826 if (err < 0)
1827 goto _end_unlock;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 _end_unlock:
1831 snd_pcm_stream_unlock_irq(substream);
1832 _end:
1833 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1834}
1835
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001836/* sanity-check for read/write methods */
1837static int pcm_sanity_check(struct snd_pcm_substream *substream)
1838{
1839 struct snd_pcm_runtime *runtime;
1840 if (PCM_RUNTIME_CHECK(substream))
1841 return -ENXIO;
1842 runtime = substream->runtime;
1843 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1844 return -EINVAL;
1845 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1846 return -EBADFD;
1847 return 0;
1848}
1849
Takashi Iwai877211f2005-11-17 13:59:38 +01001850snd_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 -07001851{
Takashi Iwai877211f2005-11-17 13:59:38 +01001852 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001854 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001856 err = pcm_sanity_check(substream);
1857 if (err < 0)
1858 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001860 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1863 runtime->channels > 1)
1864 return -EINVAL;
1865 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1866 snd_pcm_lib_write_transfer);
1867}
1868
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001869EXPORT_SYMBOL(snd_pcm_lib_write);
1870
Takashi Iwai877211f2005-11-17 13:59:38 +01001871static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 unsigned int hwoff,
1873 unsigned long data, unsigned int off,
1874 snd_pcm_uframes_t frames)
1875{
Takashi Iwai877211f2005-11-17 13:59:38 +01001876 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int err;
1878 void __user **bufs = (void __user **)data;
1879 int channels = runtime->channels;
1880 int c;
1881 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001882 if (snd_BUG_ON(!substream->ops->silence))
1883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 for (c = 0; c < channels; ++c, ++bufs) {
1885 if (*bufs == NULL) {
1886 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1887 return err;
1888 } else {
1889 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1890 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1891 return err;
1892 }
1893 }
1894 } else {
1895 /* default transfer behaviour */
1896 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 for (c = 0; c < channels; ++c, ++bufs) {
1898 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1899 if (*bufs == NULL) {
1900 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1901 } else {
1902 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1903 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1904 return -EFAULT;
1905 }
1906 }
1907 }
1908 return 0;
1909}
1910
Takashi Iwai877211f2005-11-17 13:59:38 +01001911snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 void __user **bufs,
1913 snd_pcm_uframes_t frames)
1914{
Takashi Iwai877211f2005-11-17 13:59:38 +01001915 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001917 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001919 err = pcm_sanity_check(substream);
1920 if (err < 0)
1921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001923 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1926 return -EINVAL;
1927 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1928 nonblock, snd_pcm_lib_writev_transfer);
1929}
1930
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001931EXPORT_SYMBOL(snd_pcm_lib_writev);
1932
Takashi Iwai877211f2005-11-17 13:59:38 +01001933static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 unsigned int hwoff,
1935 unsigned long data, unsigned int off,
1936 snd_pcm_uframes_t frames)
1937{
Takashi Iwai877211f2005-11-17 13:59:38 +01001938 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 int err;
1940 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1941 if (substream->ops->copy) {
1942 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1943 return err;
1944 } else {
1945 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1947 return -EFAULT;
1948 }
1949 return 0;
1950}
1951
Takashi Iwai877211f2005-11-17 13:59:38 +01001952static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 unsigned long data,
1954 snd_pcm_uframes_t size,
1955 int nonblock,
1956 transfer_f transfer)
1957{
Takashi Iwai877211f2005-11-17 13:59:38 +01001958 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 snd_pcm_uframes_t xfer = 0;
1960 snd_pcm_uframes_t offset = 0;
1961 int err = 0;
1962
1963 if (size == 0)
1964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 snd_pcm_stream_lock_irq(substream);
1967 switch (runtime->status->state) {
1968 case SNDRV_PCM_STATE_PREPARED:
1969 if (size >= runtime->start_threshold) {
1970 err = snd_pcm_start(substream);
1971 if (err < 0)
1972 goto _end_unlock;
1973 }
1974 break;
1975 case SNDRV_PCM_STATE_DRAINING:
1976 case SNDRV_PCM_STATE_RUNNING:
1977 case SNDRV_PCM_STATE_PAUSED:
1978 break;
1979 case SNDRV_PCM_STATE_XRUN:
1980 err = -EPIPE;
1981 goto _end_unlock;
1982 case SNDRV_PCM_STATE_SUSPENDED:
1983 err = -ESTRPIPE;
1984 goto _end_unlock;
1985 default:
1986 err = -EBADFD;
1987 goto _end_unlock;
1988 }
1989
1990 while (size > 0) {
1991 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1992 snd_pcm_uframes_t avail;
1993 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001994 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001997 if (!avail) {
1998 if (runtime->status->state ==
1999 SNDRV_PCM_STATE_DRAINING) {
2000 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto _end_unlock;
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (nonblock) {
2004 err = -EAGAIN;
2005 goto _end_unlock;
2006 }
Takashi Iwai13075512008-01-08 18:08:14 +01002007 err = wait_for_avail_min(substream, &avail);
2008 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002010 if (!avail)
2011 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 frames = size > avail ? avail : size;
2014 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2015 if (frames > cont)
2016 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002017 if (snd_BUG_ON(!frames)) {
2018 snd_pcm_stream_unlock_irq(substream);
2019 return -EINVAL;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 appl_ptr = runtime->control->appl_ptr;
2022 appl_ofs = appl_ptr % runtime->buffer_size;
2023 snd_pcm_stream_unlock_irq(substream);
2024 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2025 goto _end;
2026 snd_pcm_stream_lock_irq(substream);
2027 switch (runtime->status->state) {
2028 case SNDRV_PCM_STATE_XRUN:
2029 err = -EPIPE;
2030 goto _end_unlock;
2031 case SNDRV_PCM_STATE_SUSPENDED:
2032 err = -ESTRPIPE;
2033 goto _end_unlock;
2034 default:
2035 break;
2036 }
2037 appl_ptr += frames;
2038 if (appl_ptr >= runtime->boundary)
2039 appl_ptr -= runtime->boundary;
2040 runtime->control->appl_ptr = appl_ptr;
2041 if (substream->ops->ack)
2042 substream->ops->ack(substream);
2043
2044 offset += frames;
2045 size -= frames;
2046 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 _end_unlock:
2049 snd_pcm_stream_unlock_irq(substream);
2050 _end:
2051 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2052}
2053
Takashi Iwai877211f2005-11-17 13:59:38 +01002054snd_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 -07002055{
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002058 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002060 err = pcm_sanity_check(substream);
2061 if (err < 0)
2062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002064 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2066 return -EINVAL;
2067 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2068}
2069
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002070EXPORT_SYMBOL(snd_pcm_lib_read);
2071
Takashi Iwai877211f2005-11-17 13:59:38 +01002072static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 unsigned int hwoff,
2074 unsigned long data, unsigned int off,
2075 snd_pcm_uframes_t frames)
2076{
Takashi Iwai877211f2005-11-17 13:59:38 +01002077 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int err;
2079 void __user **bufs = (void __user **)data;
2080 int channels = runtime->channels;
2081 int c;
2082 if (substream->ops->copy) {
2083 for (c = 0; c < channels; ++c, ++bufs) {
2084 char __user *buf;
2085 if (*bufs == NULL)
2086 continue;
2087 buf = *bufs + samples_to_bytes(runtime, off);
2088 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2089 return err;
2090 }
2091 } else {
2092 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 for (c = 0; c < channels; ++c, ++bufs) {
2094 char *hwbuf;
2095 char __user *buf;
2096 if (*bufs == NULL)
2097 continue;
2098
2099 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2100 buf = *bufs + samples_to_bytes(runtime, off);
2101 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2102 return -EFAULT;
2103 }
2104 }
2105 return 0;
2106}
2107
Takashi Iwai877211f2005-11-17 13:59:38 +01002108snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 void __user **bufs,
2110 snd_pcm_uframes_t frames)
2111{
Takashi Iwai877211f2005-11-17 13:59:38 +01002112 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002114 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002116 err = pcm_sanity_check(substream);
2117 if (err < 0)
2118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2121 return -EBADFD;
2122
Takashi Iwai0df63e42006-04-28 15:13:41 +02002123 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2125 return -EINVAL;
2126 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129EXPORT_SYMBOL(snd_pcm_lib_readv);