blob: 9621236b2fefc890ae28a2822b90c18a87520e73 [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
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100129#define XRUN_DEBUG_BASIC (1<<0)
130#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
131#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
132#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
133#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
134
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100135#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100136#define xrun_debug(substream, mask) \
137 ((substream)->pstr->xrun_debug & (mask))
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100138#else
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200139#define xrun_debug(substream, mask) 0
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100140#endif
141
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100142#define dump_stack_on_xrun(substream) do { \
143 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
144 dump_stack(); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100145 } while (0)
146
Takashi Iwaic0070112009-06-08 15:58:48 +0200147static void pcm_debug_name(struct snd_pcm_substream *substream,
148 char *name, size_t len)
149{
150 snprintf(name, len, "pcmC%dD%d%c:%d",
151 substream->pcm->card->number,
152 substream->pcm->device,
153 substream->stream ? 'c' : 'p',
154 substream->number);
155}
156
Takashi Iwai877211f2005-11-17 13:59:38 +0100157static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200159 struct snd_pcm_runtime *runtime = substream->runtime;
160
161 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
162 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100164 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200165 char name[16];
166 pcm_debug_name(substream, name, sizeof(name));
167 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100168 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Takashi Iwai98204642009-03-19 09:59:21 +0100172static snd_pcm_uframes_t
173snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
174 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 snd_pcm_uframes_t pos;
177
178 pos = substream->ops->pointer(substream);
179 if (pos == SNDRV_PCM_POS_XRUN)
180 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100182 if (printk_ratelimit()) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200183 char name[16];
184 pcm_debug_name(substream, name, sizeof(name));
185 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
Takashi Iwai5f513e12009-03-19 10:01:47 +0100186 "buffer size = 0x%lx, period size = 0x%lx\n",
Takashi Iwaic0070112009-06-08 15:58:48 +0200187 name, pos, runtime->buffer_size,
Takashi Iwai5f513e12009-03-19 10:01:47 +0100188 runtime->period_size);
189 }
190 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 pos -= pos % runtime->min_align;
193 return pos;
194}
195
Takashi Iwai98204642009-03-19 09:59:21 +0100196static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
197 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 snd_pcm_uframes_t avail;
200
201 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
202 avail = snd_pcm_playback_avail(runtime);
203 else
204 avail = snd_pcm_capture_avail(runtime);
205 if (avail > runtime->avail_max)
206 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200207 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
208 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200210 return -EPIPE;
211 }
212 } else {
213 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200215 return -EPIPE;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 if (avail >= runtime->control->avail_min)
219 wake_up(&runtime->sleep);
220 return 0;
221}
222
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100223#define hw_ptr_error(substream, fmt, args...) \
224 do { \
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100225 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100226 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100227 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100228 } \
229 dump_stack_on_xrun(substream); \
230 } \
231 } while (0)
232
Takashi Iwai98204642009-03-19 09:59:21 +0100233static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Takashi Iwai877211f2005-11-17 13:59:38 +0100235 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 snd_pcm_uframes_t pos;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200237 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
238 snd_pcm_sframes_t hdelta, delta;
239 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200241 old_hw_ptr = runtime->status->hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
243 if (pos == SNDRV_PCM_POS_XRUN) {
244 xrun(substream);
245 return -EPIPE;
246 }
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100247 if (xrun_debug(substream, XRUN_DEBUG_PERIODUPDATE)) {
Takashi Iwaicedb8112009-07-23 11:04:13 +0200248 char name[16];
249 pcm_debug_name(substream, name, sizeof(name));
250 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
251 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200252 name, (unsigned int)pos,
253 (unsigned int)runtime->period_size,
254 (unsigned int)runtime->buffer_size,
255 (unsigned long)old_hw_ptr,
256 (unsigned long)runtime->hw_ptr_base,
257 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200258 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100259 hw_base = runtime->hw_ptr_base;
260 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100262 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100263 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100264 hw_ptr_interrupt -= runtime->boundary;
265 if (hw_base < runtime->boundary / 2)
266 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100267 delta = new_hw_ptr - hw_ptr_interrupt;
268 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100269 if (delta < 0) {
Takashi Iwai947ca212009-07-23 16:21:08 +0200270 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
Takashi Iwai79452f02009-07-22 12:51:51 +0200271 delta += runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100272 if (delta < 0) {
273 hw_ptr_error(substream,
274 "Unexpected hw_pointer value "
275 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
276 substream->stream, (long)pos,
277 (long)hw_ptr_interrupt);
Takashi Iwai79452f02009-07-22 12:51:51 +0200278#if 1
279 /* simply skipping the hwptr update seems more
280 * robust in some cases, e.g. on VMware with
281 * inaccurate timer source
282 */
283 return 0; /* skip this update */
284#else
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100285 /* rebase to interrupt position */
286 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100287 /* align hw_base to buffer_size */
288 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100289 delta = 0;
Takashi Iwai79452f02009-07-22 12:51:51 +0200290#endif
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100291 } else {
292 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100293 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100294 hw_base = 0;
295 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200298
299 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100300 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200301 goto no_jiffies_check;
302
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200303 /* Skip the jiffies check for hardwares with BATCH flag.
304 * Such hardware usually just increases the position at each IRQ,
305 * thus it can't give any strange position.
306 */
307 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
308 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200309 hdelta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200310 if (hdelta < runtime->delay)
311 goto no_jiffies_check;
312 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200313 jdelta = jiffies - runtime->hw_ptr_jiffies;
314 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
315 delta = jdelta /
316 (((runtime->period_size * HZ) / runtime->rate)
317 + HZ/100);
318 hw_ptr_error(substream,
319 "hw_ptr skipping! [Q] "
320 "(pos=%ld, delta=%ld, period=%ld, "
321 "jdelta=%lu/%lu/%lu)\n",
322 (long)pos, (long)hdelta,
323 (long)runtime->period_size, jdelta,
324 ((hdelta * HZ) / runtime->rate), delta);
325 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
326 runtime->period_size * delta;
327 if (hw_ptr_interrupt >= runtime->boundary)
328 hw_ptr_interrupt -= runtime->boundary;
329 /* rebase to interrupt position */
330 hw_base = new_hw_ptr = hw_ptr_interrupt;
331 /* align hw_base to buffer_size */
332 hw_base -= hw_base % runtime->buffer_size;
333 delta = 0;
334 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200335 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200336 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100337 hw_ptr_error(substream,
338 "Lost interrupts? "
339 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
340 substream->stream, (long)delta,
341 (long)hw_ptr_interrupt);
342 /* rebase hw_ptr_interrupt */
343 hw_ptr_interrupt =
344 new_hw_ptr - new_hw_ptr % runtime->period_size;
345 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200346 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
349 runtime->silence_size > 0)
350 snd_pcm_playback_silence(substream, new_hw_ptr);
351
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200352 if (runtime->status->hw_ptr == new_hw_ptr)
353 return 0;
354
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100355 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200357 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200358 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
359 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return snd_pcm_update_hw_ptr_post(substream, runtime);
362}
363
364/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100365int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Takashi Iwai877211f2005-11-17 13:59:38 +0100367 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100369 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200371 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 old_hw_ptr = runtime->status->hw_ptr;
374 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
375 if (pos == SNDRV_PCM_POS_XRUN) {
376 xrun(substream);
377 return -EPIPE;
378 }
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100379 if (xrun_debug(substream, XRUN_DEBUG_HWPTRUPDATE)) {
Takashi Iwaicedb8112009-07-23 11:04:13 +0200380 char name[16];
381 pcm_debug_name(substream, name, sizeof(name));
382 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
383 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200384 name, (unsigned int)pos,
385 (unsigned int)runtime->period_size,
386 (unsigned int)runtime->buffer_size,
387 (unsigned long)old_hw_ptr,
388 (unsigned long)runtime->hw_ptr_base,
389 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200390 }
391
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100392 hw_base = runtime->hw_ptr_base;
393 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100395 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200396 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100397 if (delta < 0) {
398 delta += runtime->buffer_size;
399 if (delta < 0) {
400 hw_ptr_error(substream,
401 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200402 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100403 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200404 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100407 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100408 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100409 hw_base = 0;
410 new_hw_ptr = hw_base + pos;
411 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200412 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100413 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200414 goto no_jiffies_check;
415 if (delta < runtime->delay)
416 goto no_jiffies_check;
417 delta -= runtime->delay;
418 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100419 hw_ptr_error(substream,
420 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200421 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100422 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200423 (long)runtime->period_size, jdelta,
424 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200427 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
429 runtime->silence_size > 0)
430 snd_pcm_playback_silence(substream, new_hw_ptr);
431
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200432 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200433 return 0;
434
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100435 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200437 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200438 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
439 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 return snd_pcm_update_hw_ptr_post(substream, runtime);
442}
443
444/**
445 * snd_pcm_set_ops - set the PCM operators
446 * @pcm: the pcm instance
447 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
448 * @ops: the operator table
449 *
450 * Sets the given PCM operators to the pcm instance.
451 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100452void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Takashi Iwai877211f2005-11-17 13:59:38 +0100454 struct snd_pcm_str *stream = &pcm->streams[direction];
455 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 for (substream = stream->substream; substream != NULL; substream = substream->next)
458 substream->ops = ops;
459}
460
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200461EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463/**
464 * snd_pcm_sync - set the PCM sync id
465 * @substream: the pcm substream
466 *
467 * Sets the PCM sync identifier for the card.
468 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100469void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai877211f2005-11-17 13:59:38 +0100471 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 runtime->sync.id32[0] = substream->pcm->card->number;
474 runtime->sync.id32[1] = -1;
475 runtime->sync.id32[2] = -1;
476 runtime->sync.id32[3] = -1;
477}
478
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200479EXPORT_SYMBOL(snd_pcm_set_sync);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Standard ioctl routine
483 */
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static inline unsigned int div32(unsigned int a, unsigned int b,
486 unsigned int *r)
487{
488 if (b == 0) {
489 *r = 0;
490 return UINT_MAX;
491 }
492 *r = a % b;
493 return a / b;
494}
495
496static inline unsigned int div_down(unsigned int a, unsigned int b)
497{
498 if (b == 0)
499 return UINT_MAX;
500 return a / b;
501}
502
503static inline unsigned int div_up(unsigned int a, unsigned int b)
504{
505 unsigned int r;
506 unsigned int q;
507 if (b == 0)
508 return UINT_MAX;
509 q = div32(a, b, &r);
510 if (r)
511 ++q;
512 return q;
513}
514
515static inline unsigned int mul(unsigned int a, unsigned int b)
516{
517 if (a == 0)
518 return 0;
519 if (div_down(UINT_MAX, a) < b)
520 return UINT_MAX;
521 return a * b;
522}
523
524static inline unsigned int muldiv32(unsigned int a, unsigned int b,
525 unsigned int c, unsigned int *r)
526{
527 u_int64_t n = (u_int64_t) a * b;
528 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200529 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *r = 0;
531 return UINT_MAX;
532 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200533 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (n >= UINT_MAX) {
535 *r = 0;
536 return UINT_MAX;
537 }
538 return n;
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/**
542 * snd_interval_refine - refine the interval value of configurator
543 * @i: the interval value to refine
544 * @v: the interval value to refer to
545 *
546 * Refines the interval value with the reference value.
547 * The interval is changed to the range satisfying both intervals.
548 * The interval status (min, max, integer, etc.) are evaluated.
549 *
550 * Returns non-zero if the value is changed, zero if not changed.
551 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100552int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200555 if (snd_BUG_ON(snd_interval_empty(i)))
556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (i->min < v->min) {
558 i->min = v->min;
559 i->openmin = v->openmin;
560 changed = 1;
561 } else if (i->min == v->min && !i->openmin && v->openmin) {
562 i->openmin = 1;
563 changed = 1;
564 }
565 if (i->max > v->max) {
566 i->max = v->max;
567 i->openmax = v->openmax;
568 changed = 1;
569 } else if (i->max == v->max && !i->openmax && v->openmax) {
570 i->openmax = 1;
571 changed = 1;
572 }
573 if (!i->integer && v->integer) {
574 i->integer = 1;
575 changed = 1;
576 }
577 if (i->integer) {
578 if (i->openmin) {
579 i->min++;
580 i->openmin = 0;
581 }
582 if (i->openmax) {
583 i->max--;
584 i->openmax = 0;
585 }
586 } else if (!i->openmin && !i->openmax && i->min == i->max)
587 i->integer = 1;
588 if (snd_interval_checkempty(i)) {
589 snd_interval_none(i);
590 return -EINVAL;
591 }
592 return changed;
593}
594
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200595EXPORT_SYMBOL(snd_interval_refine);
596
Takashi Iwai877211f2005-11-17 13:59:38 +0100597static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200599 if (snd_BUG_ON(snd_interval_empty(i)))
600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (snd_interval_single(i))
602 return 0;
603 i->max = i->min;
604 i->openmax = i->openmin;
605 if (i->openmax)
606 i->max++;
607 return 1;
608}
609
Takashi Iwai877211f2005-11-17 13:59:38 +0100610static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200612 if (snd_BUG_ON(snd_interval_empty(i)))
613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (snd_interval_single(i))
615 return 0;
616 i->min = i->max;
617 i->openmin = i->openmax;
618 if (i->openmin)
619 i->min--;
620 return 1;
621}
622
Takashi Iwai877211f2005-11-17 13:59:38 +0100623void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 if (a->empty || b->empty) {
626 snd_interval_none(c);
627 return;
628 }
629 c->empty = 0;
630 c->min = mul(a->min, b->min);
631 c->openmin = (a->openmin || b->openmin);
632 c->max = mul(a->max, b->max);
633 c->openmax = (a->openmax || b->openmax);
634 c->integer = (a->integer && b->integer);
635}
636
637/**
638 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200639 * @a: dividend
640 * @b: divisor
641 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * c = a / b
644 *
645 * Returns non-zero if the value is changed, zero if not changed.
646 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100647void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, 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 = div32(a->min, b->max, &r);
656 c->openmin = (r || a->openmin || b->openmax);
657 if (b->min > 0) {
658 c->max = div32(a->max, b->min, &r);
659 if (r) {
660 c->max++;
661 c->openmax = 1;
662 } else
663 c->openmax = (a->openmax || b->openmin);
664 } else {
665 c->max = UINT_MAX;
666 c->openmax = 0;
667 }
668 c->integer = 0;
669}
670
671/**
672 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200673 * @a: dividend 1
674 * @b: dividend 2
675 * @k: divisor (as integer)
676 * @c: result
677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * c = a * b / k
679 *
680 * Returns non-zero if the value is changed, zero if not changed.
681 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100682void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
683 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 unsigned int r;
686 if (a->empty || b->empty) {
687 snd_interval_none(c);
688 return;
689 }
690 c->empty = 0;
691 c->min = muldiv32(a->min, b->min, k, &r);
692 c->openmin = (r || a->openmin || b->openmin);
693 c->max = muldiv32(a->max, b->max, k, &r);
694 if (r) {
695 c->max++;
696 c->openmax = 1;
697 } else
698 c->openmax = (a->openmax || b->openmax);
699 c->integer = 0;
700}
701
702/**
703 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200704 * @a: dividend 1
705 * @k: dividend 2 (as integer)
706 * @b: divisor
707 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
709 * c = a * k / b
710 *
711 * Returns non-zero if the value is changed, zero if not changed.
712 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100713void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
714 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 unsigned int r;
717 if (a->empty || b->empty) {
718 snd_interval_none(c);
719 return;
720 }
721 c->empty = 0;
722 c->min = muldiv32(a->min, k, b->max, &r);
723 c->openmin = (r || a->openmin || b->openmax);
724 if (b->min > 0) {
725 c->max = muldiv32(a->max, k, b->min, &r);
726 if (r) {
727 c->max++;
728 c->openmax = 1;
729 } else
730 c->openmax = (a->openmax || b->openmin);
731 } else {
732 c->max = UINT_MAX;
733 c->openmax = 0;
734 }
735 c->integer = 0;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* ---- */
739
740
741/**
742 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200743 * @i: interval to refine
744 * @rats_count: number of ratnum_t
745 * @rats: ratnum_t array
746 * @nump: pointer to store the resultant numerator
747 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
749 * Returns non-zero if the value is changed, zero if not changed.
750 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100751int snd_interval_ratnum(struct snd_interval *i,
752 unsigned int rats_count, struct snd_ratnum *rats,
753 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 unsigned int best_num, best_diff, best_den;
756 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100757 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int err;
759
760 best_num = best_den = best_diff = 0;
761 for (k = 0; k < rats_count; ++k) {
762 unsigned int num = rats[k].num;
763 unsigned int den;
764 unsigned int q = i->min;
765 int diff;
766 if (q == 0)
767 q = 1;
768 den = div_down(num, q);
769 if (den < rats[k].den_min)
770 continue;
771 if (den > rats[k].den_max)
772 den = rats[k].den_max;
773 else {
774 unsigned int r;
775 r = (den - rats[k].den_min) % rats[k].den_step;
776 if (r != 0)
777 den -= r;
778 }
779 diff = num - q * den;
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.min = div_down(best_num, best_den);
792 t.openmin = !!(best_num % best_den);
793
794 best_num = best_den = best_diff = 0;
795 for (k = 0; k < rats_count; ++k) {
796 unsigned int num = rats[k].num;
797 unsigned int den;
798 unsigned int q = i->max;
799 int diff;
800 if (q == 0) {
801 i->empty = 1;
802 return -EINVAL;
803 }
804 den = div_up(num, q);
805 if (den > rats[k].den_max)
806 continue;
807 if (den < rats[k].den_min)
808 den = rats[k].den_min;
809 else {
810 unsigned int r;
811 r = (den - rats[k].den_min) % rats[k].den_step;
812 if (r != 0)
813 den += rats[k].den_step - r;
814 }
815 diff = q * den - num;
816 if (best_num == 0 ||
817 diff * best_den < best_diff * den) {
818 best_diff = diff;
819 best_den = den;
820 best_num = num;
821 }
822 }
823 if (best_den == 0) {
824 i->empty = 1;
825 return -EINVAL;
826 }
827 t.max = div_up(best_num, best_den);
828 t.openmax = !!(best_num % best_den);
829 t.integer = 0;
830 err = snd_interval_refine(i, &t);
831 if (err < 0)
832 return err;
833
834 if (snd_interval_single(i)) {
835 if (nump)
836 *nump = best_num;
837 if (denp)
838 *denp = best_den;
839 }
840 return err;
841}
842
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200843EXPORT_SYMBOL(snd_interval_ratnum);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/**
846 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200847 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100848 * @rats_count: number of struct ratden
849 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200850 * @nump: pointer to store the resultant numerator
851 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
853 * Returns non-zero if the value is changed, zero if not changed.
854 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100855static int snd_interval_ratden(struct snd_interval *i,
856 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 unsigned int *nump, unsigned int *denp)
858{
859 unsigned int best_num, best_diff, best_den;
860 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100861 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int err;
863
864 best_num = best_den = best_diff = 0;
865 for (k = 0; k < rats_count; ++k) {
866 unsigned int num;
867 unsigned int den = rats[k].den;
868 unsigned int q = i->min;
869 int diff;
870 num = mul(q, den);
871 if (num > rats[k].num_max)
872 continue;
873 if (num < rats[k].num_min)
874 num = rats[k].num_max;
875 else {
876 unsigned int r;
877 r = (num - rats[k].num_min) % rats[k].num_step;
878 if (r != 0)
879 num += rats[k].num_step - r;
880 }
881 diff = num - q * den;
882 if (best_num == 0 ||
883 diff * best_den < best_diff * den) {
884 best_diff = diff;
885 best_den = den;
886 best_num = num;
887 }
888 }
889 if (best_den == 0) {
890 i->empty = 1;
891 return -EINVAL;
892 }
893 t.min = div_down(best_num, best_den);
894 t.openmin = !!(best_num % best_den);
895
896 best_num = best_den = best_diff = 0;
897 for (k = 0; k < rats_count; ++k) {
898 unsigned int num;
899 unsigned int den = rats[k].den;
900 unsigned int q = i->max;
901 int diff;
902 num = mul(q, den);
903 if (num < rats[k].num_min)
904 continue;
905 if (num > rats[k].num_max)
906 num = rats[k].num_max;
907 else {
908 unsigned int r;
909 r = (num - rats[k].num_min) % rats[k].num_step;
910 if (r != 0)
911 num -= r;
912 }
913 diff = q * den - num;
914 if (best_num == 0 ||
915 diff * best_den < best_diff * den) {
916 best_diff = diff;
917 best_den = den;
918 best_num = num;
919 }
920 }
921 if (best_den == 0) {
922 i->empty = 1;
923 return -EINVAL;
924 }
925 t.max = div_up(best_num, best_den);
926 t.openmax = !!(best_num % best_den);
927 t.integer = 0;
928 err = snd_interval_refine(i, &t);
929 if (err < 0)
930 return err;
931
932 if (snd_interval_single(i)) {
933 if (nump)
934 *nump = best_num;
935 if (denp)
936 *denp = best_den;
937 }
938 return err;
939}
940
941/**
942 * snd_interval_list - refine the interval value from the list
943 * @i: the interval value to refine
944 * @count: the number of elements in the list
945 * @list: the value list
946 * @mask: the bit-mask to evaluate
947 *
948 * Refines the interval value from the list.
949 * When mask is non-zero, only the elements corresponding to bit 1 are
950 * evaluated.
951 *
952 * Returns non-zero if the value is changed, zero if not changed.
953 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100954int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200957 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +0100958
959 if (!count) {
960 i->empty = 1;
961 return -EINVAL;
962 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200963 snd_interval_any(&list_range);
964 list_range.min = UINT_MAX;
965 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 for (k = 0; k < count; k++) {
967 if (mask && !(mask & (1 << k)))
968 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200969 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200971 list_range.min = min(list_range.min, list[k]);
972 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200974 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200977EXPORT_SYMBOL(snd_interval_list);
978
Takashi Iwai877211f2005-11-17 13:59:38 +0100979static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 unsigned int n;
982 int changed = 0;
983 n = (i->min - min) % step;
984 if (n != 0 || i->openmin) {
985 i->min += step - n;
986 changed = 1;
987 }
988 n = (i->max - min) % step;
989 if (n != 0 || i->openmax) {
990 i->max -= n;
991 changed = 1;
992 }
993 if (snd_interval_checkempty(i)) {
994 i->empty = 1;
995 return -EINVAL;
996 }
997 return changed;
998}
999
1000/* Info constraints helpers */
1001
1002/**
1003 * snd_pcm_hw_rule_add - add the hw-constraint rule
1004 * @runtime: the pcm runtime instance
1005 * @cond: condition bits
1006 * @var: the variable to evaluate
1007 * @func: the evaluation function
1008 * @private: the private data pointer passed to function
1009 * @dep: the dependent variables
1010 *
1011 * Returns zero if successful, or a negative error code on failure.
1012 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001013int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int var,
1015 snd_pcm_hw_rule_func_t func, void *private,
1016 int dep, ...)
1017{
Takashi Iwai877211f2005-11-17 13:59:38 +01001018 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1019 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned int k;
1021 va_list args;
1022 va_start(args, dep);
1023 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001024 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned int new_rules = constrs->rules_all + 16;
1026 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1027 if (!new)
1028 return -ENOMEM;
1029 if (constrs->rules) {
1030 memcpy(new, constrs->rules,
1031 constrs->rules_num * sizeof(*c));
1032 kfree(constrs->rules);
1033 }
1034 constrs->rules = new;
1035 constrs->rules_all = new_rules;
1036 }
1037 c = &constrs->rules[constrs->rules_num];
1038 c->cond = cond;
1039 c->func = func;
1040 c->var = var;
1041 c->private = private;
1042 k = 0;
1043 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001044 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1045 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 c->deps[k++] = dep;
1047 if (dep < 0)
1048 break;
1049 dep = va_arg(args, int);
1050 }
1051 constrs->rules_num++;
1052 va_end(args);
1053 return 0;
1054}
1055
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001056EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001059 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001060 * @runtime: PCM runtime instance
1061 * @var: hw_params variable to apply the mask
1062 * @mask: the bitmap mask
1063 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001064 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001066int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 u_int32_t mask)
1068{
Takashi Iwai877211f2005-11-17 13:59:38 +01001069 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1070 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *maskp->bits &= mask;
1072 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1073 if (*maskp->bits == 0)
1074 return -EINVAL;
1075 return 0;
1076}
1077
1078/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001079 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001080 * @runtime: PCM runtime instance
1081 * @var: hw_params variable to apply the mask
1082 * @mask: the 64bit bitmap mask
1083 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001084 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001086int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 u_int64_t mask)
1088{
Takashi Iwai877211f2005-11-17 13:59:38 +01001089 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1090 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 maskp->bits[0] &= (u_int32_t)mask;
1092 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1093 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1094 if (! maskp->bits[0] && ! maskp->bits[1])
1095 return -EINVAL;
1096 return 0;
1097}
1098
1099/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001100 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001101 * @runtime: PCM runtime instance
1102 * @var: hw_params variable to apply the integer constraint
1103 *
1104 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001106int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Takashi Iwai877211f2005-11-17 13:59:38 +01001108 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return snd_interval_setinteger(constrs_interval(constrs, var));
1110}
1111
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001112EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001115 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001116 * @runtime: PCM runtime instance
1117 * @var: hw_params variable to apply the range
1118 * @min: the minimal value
1119 * @max: the maximal value
1120 *
1121 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001123int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 unsigned int min, unsigned int max)
1125{
Takashi Iwai877211f2005-11-17 13:59:38 +01001126 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1127 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 t.min = min;
1129 t.max = max;
1130 t.openmin = t.openmax = 0;
1131 t.integer = 0;
1132 return snd_interval_refine(constrs_interval(constrs, var), &t);
1133}
1134
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001135EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1136
Takashi Iwai877211f2005-11-17 13:59:38 +01001137static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1138 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai877211f2005-11-17 13:59:38 +01001140 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1142}
1143
1144
1145/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001146 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @var: hw_params variable to apply the list constraint
1150 * @l: list
1151 *
1152 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001154int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned int cond,
1156 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 return snd_pcm_hw_rule_add(runtime, cond, var,
1160 snd_pcm_hw_rule_list, l,
1161 var, -1);
1162}
1163
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001164EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1165
Takashi Iwai877211f2005-11-17 13:59:38 +01001166static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 unsigned int num = 0, den = 0;
1171 int err;
1172 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1173 r->nrats, r->rats, &num, &den);
1174 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1175 params->rate_num = num;
1176 params->rate_den = den;
1177 }
1178 return err;
1179}
1180
1181/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001182 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001183 * @runtime: PCM runtime instance
1184 * @cond: condition bits
1185 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001186 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001188int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 unsigned int cond,
1190 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001191 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 return snd_pcm_hw_rule_add(runtime, cond, var,
1194 snd_pcm_hw_rule_ratnums, r,
1195 var, -1);
1196}
1197
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001198EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1199
Takashi Iwai877211f2005-11-17 13:59:38 +01001200static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1201 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwai877211f2005-11-17 13:59:38 +01001203 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 unsigned int num = 0, den = 0;
1205 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1206 r->nrats, r->rats, &num, &den);
1207 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1208 params->rate_num = num;
1209 params->rate_den = den;
1210 }
1211 return err;
1212}
1213
1214/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001215 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001216 * @runtime: PCM runtime instance
1217 * @cond: condition bits
1218 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001219 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001221int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 unsigned int cond,
1223 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001224 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 return snd_pcm_hw_rule_add(runtime, cond, var,
1227 snd_pcm_hw_rule_ratdens, r,
1228 var, -1);
1229}
1230
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001231EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1232
Takashi Iwai877211f2005-11-17 13:59:38 +01001233static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1234 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 unsigned int l = (unsigned long) rule->private;
1237 int width = l & 0xffff;
1238 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001239 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (snd_interval_single(i) && snd_interval_value(i) == width)
1241 params->msbits = msbits;
1242 return 0;
1243}
1244
1245/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001246 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001247 * @runtime: PCM runtime instance
1248 * @cond: condition bits
1249 * @width: sample bits width
1250 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001252int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 unsigned int cond,
1254 unsigned int width,
1255 unsigned int msbits)
1256{
1257 unsigned long l = (msbits << 16) | width;
1258 return snd_pcm_hw_rule_add(runtime, cond, -1,
1259 snd_pcm_hw_rule_msbits,
1260 (void*) l,
1261 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1262}
1263
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001264EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1265
Takashi Iwai877211f2005-11-17 13:59:38 +01001266static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1267 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 unsigned long step = (unsigned long) rule->private;
1270 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1271}
1272
1273/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001274 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001275 * @runtime: PCM runtime instance
1276 * @cond: condition bits
1277 * @var: hw_params variable to apply the step constraint
1278 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001280int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned int cond,
1282 snd_pcm_hw_param_t var,
1283 unsigned long step)
1284{
1285 return snd_pcm_hw_rule_add(runtime, cond, var,
1286 snd_pcm_hw_rule_step, (void *) step,
1287 var, -1);
1288}
1289
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001290EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1291
Takashi Iwai877211f2005-11-17 13:59:38 +01001292static 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 -07001293{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001294 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1296 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1297 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1298 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1299 };
1300 return snd_interval_list(hw_param_interval(params, rule->var),
1301 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1302}
1303
1304/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001305 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001306 * @runtime: PCM runtime instance
1307 * @cond: condition bits
1308 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001310int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 unsigned int cond,
1312 snd_pcm_hw_param_t var)
1313{
1314 return snd_pcm_hw_rule_add(runtime, cond, var,
1315 snd_pcm_hw_rule_pow2, NULL,
1316 var, -1);
1317}
1318
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001319EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1320
Takashi Iwai877211f2005-11-17 13:59:38 +01001321static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001322 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 if (hw_is_mask(var)) {
1325 snd_mask_any(hw_param_mask(params, var));
1326 params->cmask |= 1 << var;
1327 params->rmask |= 1 << var;
1328 return;
1329 }
1330 if (hw_is_interval(var)) {
1331 snd_interval_any(hw_param_interval(params, var));
1332 params->cmask |= 1 << var;
1333 params->rmask |= 1 << var;
1334 return;
1335 }
1336 snd_BUG();
1337}
1338
Takashi Iwai877211f2005-11-17 13:59:38 +01001339void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 unsigned int k;
1342 memset(params, 0, sizeof(*params));
1343 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1344 _snd_pcm_hw_param_any(params, k);
1345 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1346 _snd_pcm_hw_param_any(params, k);
1347 params->info = ~0U;
1348}
1349
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001350EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001353 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001354 * @params: the hw_params instance
1355 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001356 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001358 * Return the value for field @var if it's fixed in configuration space
1359 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001361int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1362 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001365 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!snd_mask_single(mask))
1367 return -EINVAL;
1368 if (dir)
1369 *dir = 0;
1370 return snd_mask_value(mask);
1371 }
1372 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001373 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (!snd_interval_single(i))
1375 return -EINVAL;
1376 if (dir)
1377 *dir = i->openmin;
1378 return snd_interval_value(i);
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return -EINVAL;
1381}
1382
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001383EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Takashi Iwai877211f2005-11-17 13:59:38 +01001385void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 snd_pcm_hw_param_t var)
1387{
1388 if (hw_is_mask(var)) {
1389 snd_mask_none(hw_param_mask(params, var));
1390 params->cmask |= 1 << var;
1391 params->rmask |= 1 << var;
1392 } else if (hw_is_interval(var)) {
1393 snd_interval_none(hw_param_interval(params, var));
1394 params->cmask |= 1 << var;
1395 params->rmask |= 1 << var;
1396 } else {
1397 snd_BUG();
1398 }
1399}
1400
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001401EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Takashi Iwai877211f2005-11-17 13:59:38 +01001403static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001404 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 int changed;
1407 if (hw_is_mask(var))
1408 changed = snd_mask_refine_first(hw_param_mask(params, var));
1409 else if (hw_is_interval(var))
1410 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001411 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (changed) {
1414 params->cmask |= 1 << var;
1415 params->rmask |= 1 << var;
1416 }
1417 return changed;
1418}
1419
1420
1421/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001422 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001423 * @pcm: PCM instance
1424 * @params: the hw_params instance
1425 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001426 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001428 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * values > minimum. Reduce configuration space accordingly.
1430 * Return the minimum.
1431 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001432int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1433 struct snd_pcm_hw_params *params,
1434 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 int changed = _snd_pcm_hw_param_first(params, var);
1437 if (changed < 0)
1438 return changed;
1439 if (params->rmask) {
1440 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001441 if (snd_BUG_ON(err < 0))
1442 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 return snd_pcm_hw_param_value(params, var, dir);
1445}
1446
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001447EXPORT_SYMBOL(snd_pcm_hw_param_first);
1448
Takashi Iwai877211f2005-11-17 13:59:38 +01001449static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001450 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 int changed;
1453 if (hw_is_mask(var))
1454 changed = snd_mask_refine_last(hw_param_mask(params, var));
1455 else if (hw_is_interval(var))
1456 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001457 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (changed) {
1460 params->cmask |= 1 << var;
1461 params->rmask |= 1 << var;
1462 }
1463 return changed;
1464}
1465
1466
1467/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001468 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001469 * @pcm: PCM instance
1470 * @params: the hw_params instance
1471 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001472 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001474 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * values < maximum. Reduce configuration space accordingly.
1476 * Return the maximum.
1477 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001478int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1479 struct snd_pcm_hw_params *params,
1480 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 int changed = _snd_pcm_hw_param_last(params, var);
1483 if (changed < 0)
1484 return changed;
1485 if (params->rmask) {
1486 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001487 if (snd_BUG_ON(err < 0))
1488 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 return snd_pcm_hw_param_value(params, var, dir);
1491}
1492
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001493EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001496 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001497 * @pcm: PCM instance
1498 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001500 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * The configuration chosen is that obtained fixing in this order:
1502 * first access, first format, first subformat, min channels,
1503 * min rate, min period time, max buffer size, min tick time
1504 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001505int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1506 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001508 static int vars[] = {
1509 SNDRV_PCM_HW_PARAM_ACCESS,
1510 SNDRV_PCM_HW_PARAM_FORMAT,
1511 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1512 SNDRV_PCM_HW_PARAM_CHANNELS,
1513 SNDRV_PCM_HW_PARAM_RATE,
1514 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1515 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1516 SNDRV_PCM_HW_PARAM_TICK_TIME,
1517 -1
1518 };
1519 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001521 for (v = vars; *v != -1; v++) {
1522 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1523 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1524 else
1525 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001526 if (snd_BUG_ON(err < 0))
1527 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530}
1531
Takashi Iwai877211f2005-11-17 13:59:38 +01001532static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 void *arg)
1534{
Takashi Iwai877211f2005-11-17 13:59:38 +01001535 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unsigned long flags;
1537 snd_pcm_stream_lock_irqsave(substream, flags);
1538 if (snd_pcm_running(substream) &&
1539 snd_pcm_update_hw_ptr(substream) >= 0)
1540 runtime->status->hw_ptr %= runtime->buffer_size;
1541 else
1542 runtime->status->hw_ptr = 0;
1543 snd_pcm_stream_unlock_irqrestore(substream, flags);
1544 return 0;
1545}
1546
Takashi Iwai877211f2005-11-17 13:59:38 +01001547static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 void *arg)
1549{
Takashi Iwai877211f2005-11-17 13:59:38 +01001550 struct snd_pcm_channel_info *info = arg;
1551 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int width;
1553 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1554 info->offset = -1;
1555 return 0;
1556 }
1557 width = snd_pcm_format_physical_width(runtime->format);
1558 if (width < 0)
1559 return width;
1560 info->offset = 0;
1561 switch (runtime->access) {
1562 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1563 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1564 info->first = info->channel * width;
1565 info->step = runtime->channels * width;
1566 break;
1567 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1568 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1569 {
1570 size_t size = runtime->dma_bytes / runtime->channels;
1571 info->first = info->channel * size * 8;
1572 info->step = width;
1573 break;
1574 }
1575 default:
1576 snd_BUG();
1577 break;
1578 }
1579 return 0;
1580}
1581
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001582static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1583 void *arg)
1584{
1585 struct snd_pcm_hw_params *params = arg;
1586 snd_pcm_format_t format;
1587 int channels, width;
1588
1589 params->fifo_size = substream->runtime->hw.fifo_size;
1590 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1591 format = params_format(params);
1592 channels = params_channels(params);
1593 width = snd_pcm_format_physical_width(format);
1594 params->fifo_size /= width * channels;
1595 }
1596 return 0;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/**
1600 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1601 * @substream: the pcm substream instance
1602 * @cmd: ioctl command
1603 * @arg: ioctl argument
1604 *
1605 * Processes the generic ioctl commands for PCM.
1606 * Can be passed as the ioctl callback for PCM ops.
1607 *
1608 * Returns zero if successful, or a negative error code on failure.
1609 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001610int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 unsigned int cmd, void *arg)
1612{
1613 switch (cmd) {
1614 case SNDRV_PCM_IOCTL1_INFO:
1615 return 0;
1616 case SNDRV_PCM_IOCTL1_RESET:
1617 return snd_pcm_lib_ioctl_reset(substream, arg);
1618 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1619 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001620 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1621 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623 return -ENXIO;
1624}
1625
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001626EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628/**
1629 * snd_pcm_period_elapsed - update the pcm status for the next period
1630 * @substream: the pcm substream instance
1631 *
1632 * This function is called from the interrupt handler when the
1633 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001634 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 *
1636 * Even if more than one periods have elapsed since the last call, you
1637 * have to call this only once.
1638 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001639void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwai877211f2005-11-17 13:59:38 +01001641 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 unsigned long flags;
1643
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001644 if (PCM_RUNTIME_CHECK(substream))
1645 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 if (runtime->transfer_ack_begin)
1649 runtime->transfer_ack_begin(substream);
1650
1651 snd_pcm_stream_lock_irqsave(substream, flags);
1652 if (!snd_pcm_running(substream) ||
1653 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1654 goto _end;
1655
1656 if (substream->timer_running)
1657 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 _end:
1659 snd_pcm_stream_unlock_irqrestore(substream, flags);
1660 if (runtime->transfer_ack_end)
1661 runtime->transfer_ack_end(substream);
1662 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1663}
1664
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001665EXPORT_SYMBOL(snd_pcm_period_elapsed);
1666
Takashi Iwai13075512008-01-08 18:08:14 +01001667/*
1668 * Wait until avail_min data becomes available
1669 * Returns a negative error code if any error occurs during operation.
1670 * The available space is stored on availp. When err = 0 and avail = 0
1671 * on the capture stream, it indicates the stream is in DRAINING state.
1672 */
1673static int wait_for_avail_min(struct snd_pcm_substream *substream,
1674 snd_pcm_uframes_t *availp)
1675{
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1678 wait_queue_t wait;
1679 int err = 0;
1680 snd_pcm_uframes_t avail = 0;
1681 long tout;
1682
1683 init_waitqueue_entry(&wait, current);
1684 add_wait_queue(&runtime->sleep, &wait);
1685 for (;;) {
1686 if (signal_pending(current)) {
1687 err = -ERESTARTSYS;
1688 break;
1689 }
1690 set_current_state(TASK_INTERRUPTIBLE);
1691 snd_pcm_stream_unlock_irq(substream);
1692 tout = schedule_timeout(msecs_to_jiffies(10000));
1693 snd_pcm_stream_lock_irq(substream);
1694 switch (runtime->status->state) {
1695 case SNDRV_PCM_STATE_SUSPENDED:
1696 err = -ESTRPIPE;
1697 goto _endloop;
1698 case SNDRV_PCM_STATE_XRUN:
1699 err = -EPIPE;
1700 goto _endloop;
1701 case SNDRV_PCM_STATE_DRAINING:
1702 if (is_playback)
1703 err = -EPIPE;
1704 else
1705 avail = 0; /* indicate draining */
1706 goto _endloop;
1707 case SNDRV_PCM_STATE_OPEN:
1708 case SNDRV_PCM_STATE_SETUP:
1709 case SNDRV_PCM_STATE_DISCONNECTED:
1710 err = -EBADFD;
1711 goto _endloop;
1712 }
1713 if (!tout) {
1714 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1715 is_playback ? "playback" : "capture");
1716 err = -EIO;
1717 break;
1718 }
1719 if (is_playback)
1720 avail = snd_pcm_playback_avail(runtime);
1721 else
1722 avail = snd_pcm_capture_avail(runtime);
1723 if (avail >= runtime->control->avail_min)
1724 break;
1725 }
1726 _endloop:
1727 remove_wait_queue(&runtime->sleep, &wait);
1728 *availp = avail;
1729 return err;
1730}
1731
Takashi Iwai877211f2005-11-17 13:59:38 +01001732static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned int hwoff,
1734 unsigned long data, unsigned int off,
1735 snd_pcm_uframes_t frames)
1736{
Takashi Iwai877211f2005-11-17 13:59:38 +01001737 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int err;
1739 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1740 if (substream->ops->copy) {
1741 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1742 return err;
1743 } else {
1744 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1746 return -EFAULT;
1747 }
1748 return 0;
1749}
1750
Takashi Iwai877211f2005-11-17 13:59:38 +01001751typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 unsigned long data, unsigned int off,
1753 snd_pcm_uframes_t size);
1754
Takashi Iwai877211f2005-11-17 13:59:38 +01001755static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 unsigned long data,
1757 snd_pcm_uframes_t size,
1758 int nonblock,
1759 transfer_f transfer)
1760{
Takashi Iwai877211f2005-11-17 13:59:38 +01001761 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 snd_pcm_uframes_t xfer = 0;
1763 snd_pcm_uframes_t offset = 0;
1764 int err = 0;
1765
1766 if (size == 0)
1767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 snd_pcm_stream_lock_irq(substream);
1770 switch (runtime->status->state) {
1771 case SNDRV_PCM_STATE_PREPARED:
1772 case SNDRV_PCM_STATE_RUNNING:
1773 case SNDRV_PCM_STATE_PAUSED:
1774 break;
1775 case SNDRV_PCM_STATE_XRUN:
1776 err = -EPIPE;
1777 goto _end_unlock;
1778 case SNDRV_PCM_STATE_SUSPENDED:
1779 err = -ESTRPIPE;
1780 goto _end_unlock;
1781 default:
1782 err = -EBADFD;
1783 goto _end_unlock;
1784 }
1785
1786 while (size > 0) {
1787 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1788 snd_pcm_uframes_t avail;
1789 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001790 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 snd_pcm_update_hw_ptr(substream);
1792 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001793 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (nonblock) {
1795 err = -EAGAIN;
1796 goto _end_unlock;
1797 }
Takashi Iwai13075512008-01-08 18:08:14 +01001798 err = wait_for_avail_min(substream, &avail);
1799 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 frames = size > avail ? avail : size;
1803 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1804 if (frames > cont)
1805 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001806 if (snd_BUG_ON(!frames)) {
1807 snd_pcm_stream_unlock_irq(substream);
1808 return -EINVAL;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 appl_ptr = runtime->control->appl_ptr;
1811 appl_ofs = appl_ptr % runtime->buffer_size;
1812 snd_pcm_stream_unlock_irq(substream);
1813 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1814 goto _end;
1815 snd_pcm_stream_lock_irq(substream);
1816 switch (runtime->status->state) {
1817 case SNDRV_PCM_STATE_XRUN:
1818 err = -EPIPE;
1819 goto _end_unlock;
1820 case SNDRV_PCM_STATE_SUSPENDED:
1821 err = -ESTRPIPE;
1822 goto _end_unlock;
1823 default:
1824 break;
1825 }
1826 appl_ptr += frames;
1827 if (appl_ptr >= runtime->boundary)
1828 appl_ptr -= runtime->boundary;
1829 runtime->control->appl_ptr = appl_ptr;
1830 if (substream->ops->ack)
1831 substream->ops->ack(substream);
1832
1833 offset += frames;
1834 size -= frames;
1835 xfer += frames;
1836 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1837 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1838 err = snd_pcm_start(substream);
1839 if (err < 0)
1840 goto _end_unlock;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 _end_unlock:
1844 snd_pcm_stream_unlock_irq(substream);
1845 _end:
1846 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1847}
1848
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001849/* sanity-check for read/write methods */
1850static int pcm_sanity_check(struct snd_pcm_substream *substream)
1851{
1852 struct snd_pcm_runtime *runtime;
1853 if (PCM_RUNTIME_CHECK(substream))
1854 return -ENXIO;
1855 runtime = substream->runtime;
1856 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1857 return -EINVAL;
1858 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1859 return -EBADFD;
1860 return 0;
1861}
1862
Takashi Iwai877211f2005-11-17 13:59:38 +01001863snd_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 -07001864{
Takashi Iwai877211f2005-11-17 13:59:38 +01001865 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001869 err = pcm_sanity_check(substream);
1870 if (err < 0)
1871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001873 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1876 runtime->channels > 1)
1877 return -EINVAL;
1878 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1879 snd_pcm_lib_write_transfer);
1880}
1881
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001882EXPORT_SYMBOL(snd_pcm_lib_write);
1883
Takashi Iwai877211f2005-11-17 13:59:38 +01001884static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned int hwoff,
1886 unsigned long data, unsigned int off,
1887 snd_pcm_uframes_t frames)
1888{
Takashi Iwai877211f2005-11-17 13:59:38 +01001889 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int err;
1891 void __user **bufs = (void __user **)data;
1892 int channels = runtime->channels;
1893 int c;
1894 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001895 if (snd_BUG_ON(!substream->ops->silence))
1896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 for (c = 0; c < channels; ++c, ++bufs) {
1898 if (*bufs == NULL) {
1899 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1900 return err;
1901 } else {
1902 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1903 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1904 return err;
1905 }
1906 }
1907 } else {
1908 /* default transfer behaviour */
1909 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 for (c = 0; c < channels; ++c, ++bufs) {
1911 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1912 if (*bufs == NULL) {
1913 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1914 } else {
1915 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1916 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1917 return -EFAULT;
1918 }
1919 }
1920 }
1921 return 0;
1922}
1923
Takashi Iwai877211f2005-11-17 13:59:38 +01001924snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 void __user **bufs,
1926 snd_pcm_uframes_t frames)
1927{
Takashi Iwai877211f2005-11-17 13:59:38 +01001928 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001932 err = pcm_sanity_check(substream);
1933 if (err < 0)
1934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001936 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1939 return -EINVAL;
1940 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1941 nonblock, snd_pcm_lib_writev_transfer);
1942}
1943
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001944EXPORT_SYMBOL(snd_pcm_lib_writev);
1945
Takashi Iwai877211f2005-11-17 13:59:38 +01001946static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 unsigned int hwoff,
1948 unsigned long data, unsigned int off,
1949 snd_pcm_uframes_t frames)
1950{
Takashi Iwai877211f2005-11-17 13:59:38 +01001951 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int err;
1953 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1954 if (substream->ops->copy) {
1955 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1956 return err;
1957 } else {
1958 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1960 return -EFAULT;
1961 }
1962 return 0;
1963}
1964
Takashi Iwai877211f2005-11-17 13:59:38 +01001965static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 unsigned long data,
1967 snd_pcm_uframes_t size,
1968 int nonblock,
1969 transfer_f transfer)
1970{
Takashi Iwai877211f2005-11-17 13:59:38 +01001971 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 snd_pcm_uframes_t xfer = 0;
1973 snd_pcm_uframes_t offset = 0;
1974 int err = 0;
1975
1976 if (size == 0)
1977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 snd_pcm_stream_lock_irq(substream);
1980 switch (runtime->status->state) {
1981 case SNDRV_PCM_STATE_PREPARED:
1982 if (size >= runtime->start_threshold) {
1983 err = snd_pcm_start(substream);
1984 if (err < 0)
1985 goto _end_unlock;
1986 }
1987 break;
1988 case SNDRV_PCM_STATE_DRAINING:
1989 case SNDRV_PCM_STATE_RUNNING:
1990 case SNDRV_PCM_STATE_PAUSED:
1991 break;
1992 case SNDRV_PCM_STATE_XRUN:
1993 err = -EPIPE;
1994 goto _end_unlock;
1995 case SNDRV_PCM_STATE_SUSPENDED:
1996 err = -ESTRPIPE;
1997 goto _end_unlock;
1998 default:
1999 err = -EBADFD;
2000 goto _end_unlock;
2001 }
2002
2003 while (size > 0) {
2004 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2005 snd_pcm_uframes_t avail;
2006 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002007 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002010 if (!avail) {
2011 if (runtime->status->state ==
2012 SNDRV_PCM_STATE_DRAINING) {
2013 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 goto _end_unlock;
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (nonblock) {
2017 err = -EAGAIN;
2018 goto _end_unlock;
2019 }
Takashi Iwai13075512008-01-08 18:08:14 +01002020 err = wait_for_avail_min(substream, &avail);
2021 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002023 if (!avail)
2024 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 frames = size > avail ? avail : size;
2027 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2028 if (frames > cont)
2029 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002030 if (snd_BUG_ON(!frames)) {
2031 snd_pcm_stream_unlock_irq(substream);
2032 return -EINVAL;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 appl_ptr = runtime->control->appl_ptr;
2035 appl_ofs = appl_ptr % runtime->buffer_size;
2036 snd_pcm_stream_unlock_irq(substream);
2037 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2038 goto _end;
2039 snd_pcm_stream_lock_irq(substream);
2040 switch (runtime->status->state) {
2041 case SNDRV_PCM_STATE_XRUN:
2042 err = -EPIPE;
2043 goto _end_unlock;
2044 case SNDRV_PCM_STATE_SUSPENDED:
2045 err = -ESTRPIPE;
2046 goto _end_unlock;
2047 default:
2048 break;
2049 }
2050 appl_ptr += frames;
2051 if (appl_ptr >= runtime->boundary)
2052 appl_ptr -= runtime->boundary;
2053 runtime->control->appl_ptr = appl_ptr;
2054 if (substream->ops->ack)
2055 substream->ops->ack(substream);
2056
2057 offset += frames;
2058 size -= frames;
2059 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 _end_unlock:
2062 snd_pcm_stream_unlock_irq(substream);
2063 _end:
2064 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2065}
2066
Takashi Iwai877211f2005-11-17 13:59:38 +01002067snd_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 -07002068{
Takashi Iwai877211f2005-11-17 13:59:38 +01002069 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002073 err = pcm_sanity_check(substream);
2074 if (err < 0)
2075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002077 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2079 return -EINVAL;
2080 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2081}
2082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002083EXPORT_SYMBOL(snd_pcm_lib_read);
2084
Takashi Iwai877211f2005-11-17 13:59:38 +01002085static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 unsigned int hwoff,
2087 unsigned long data, unsigned int off,
2088 snd_pcm_uframes_t frames)
2089{
Takashi Iwai877211f2005-11-17 13:59:38 +01002090 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int err;
2092 void __user **bufs = (void __user **)data;
2093 int channels = runtime->channels;
2094 int c;
2095 if (substream->ops->copy) {
2096 for (c = 0; c < channels; ++c, ++bufs) {
2097 char __user *buf;
2098 if (*bufs == NULL)
2099 continue;
2100 buf = *bufs + samples_to_bytes(runtime, off);
2101 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2102 return err;
2103 }
2104 } else {
2105 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 for (c = 0; c < channels; ++c, ++bufs) {
2107 char *hwbuf;
2108 char __user *buf;
2109 if (*bufs == NULL)
2110 continue;
2111
2112 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2113 buf = *bufs + samples_to_bytes(runtime, off);
2114 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2115 return -EFAULT;
2116 }
2117 }
2118 return 0;
2119}
2120
Takashi Iwai877211f2005-11-17 13:59:38 +01002121snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 void __user **bufs,
2123 snd_pcm_uframes_t frames)
2124{
Takashi Iwai877211f2005-11-17 13:59:38 +01002125 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002127 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002129 err = pcm_sanity_check(substream);
2130 if (err < 0)
2131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2134 return -EBADFD;
2135
Takashi Iwai0df63e42006-04-28 15:13:41 +02002136 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2138 return -EINVAL;
2139 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142EXPORT_SYMBOL(snd_pcm_lib_readv);