blob: 72cfd47af6b8b4196ba6a77d52002e3a52ab9e18 [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 Iwaicedb8112009-07-23 11:04:13 +0200236 if (xrun_debug(substream, 8)) {
237 char name[16];
238 pcm_debug_name(substream, name, sizeof(name));
239 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
240 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200241 name, (unsigned int)pos,
242 (unsigned int)runtime->period_size,
243 (unsigned int)runtime->buffer_size,
244 (unsigned long)old_hw_ptr,
245 (unsigned long)runtime->hw_ptr_base,
246 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200247 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100248 hw_base = runtime->hw_ptr_base;
249 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100251 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100252 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100253 hw_ptr_interrupt -= runtime->boundary;
254 if (hw_base < runtime->boundary / 2)
255 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100256 delta = new_hw_ptr - hw_ptr_interrupt;
257 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100258 if (delta < 0) {
Takashi Iwai947ca212009-07-23 16:21:08 +0200259 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
Takashi Iwai79452f02009-07-22 12:51:51 +0200260 delta += runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100261 if (delta < 0) {
262 hw_ptr_error(substream,
263 "Unexpected hw_pointer value "
264 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
265 substream->stream, (long)pos,
266 (long)hw_ptr_interrupt);
Takashi Iwai79452f02009-07-22 12:51:51 +0200267#if 1
268 /* simply skipping the hwptr update seems more
269 * robust in some cases, e.g. on VMware with
270 * inaccurate timer source
271 */
272 return 0; /* skip this update */
273#else
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100274 /* rebase to interrupt position */
275 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100276 /* align hw_base to buffer_size */
277 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100278 delta = 0;
Takashi Iwai79452f02009-07-22 12:51:51 +0200279#endif
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100280 } else {
281 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100282 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100283 hw_base = 0;
284 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200287
288 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselac62a01a2009-05-28 11:21:52 +0200289 if (!xrun_debug(substream, 4))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200290 goto no_jiffies_check;
291
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200292 /* Skip the jiffies check for hardwares with BATCH flag.
293 * Such hardware usually just increases the position at each IRQ,
294 * thus it can't give any strange position.
295 */
296 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
297 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200298 hdelta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200299 if (hdelta < runtime->delay)
300 goto no_jiffies_check;
301 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200302 jdelta = jiffies - runtime->hw_ptr_jiffies;
303 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
304 delta = jdelta /
305 (((runtime->period_size * HZ) / runtime->rate)
306 + HZ/100);
307 hw_ptr_error(substream,
308 "hw_ptr skipping! [Q] "
309 "(pos=%ld, delta=%ld, period=%ld, "
310 "jdelta=%lu/%lu/%lu)\n",
311 (long)pos, (long)hdelta,
312 (long)runtime->period_size, jdelta,
313 ((hdelta * HZ) / runtime->rate), delta);
314 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
315 runtime->period_size * delta;
316 if (hw_ptr_interrupt >= runtime->boundary)
317 hw_ptr_interrupt -= runtime->boundary;
318 /* rebase to interrupt position */
319 hw_base = new_hw_ptr = hw_ptr_interrupt;
320 /* align hw_base to buffer_size */
321 hw_base -= hw_base % runtime->buffer_size;
322 delta = 0;
323 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200324 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200325 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100326 hw_ptr_error(substream,
327 "Lost interrupts? "
328 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
329 substream->stream, (long)delta,
330 (long)hw_ptr_interrupt);
331 /* rebase hw_ptr_interrupt */
332 hw_ptr_interrupt =
333 new_hw_ptr - new_hw_ptr % runtime->period_size;
334 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200335 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
338 runtime->silence_size > 0)
339 snd_pcm_playback_silence(substream, new_hw_ptr);
340
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200341 if (runtime->status->hw_ptr == new_hw_ptr)
342 return 0;
343
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100344 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200346 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200347 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
348 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 return snd_pcm_update_hw_ptr_post(substream, runtime);
351}
352
353/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100354int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Takashi Iwai877211f2005-11-17 13:59:38 +0100356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100358 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200360 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 old_hw_ptr = runtime->status->hw_ptr;
363 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
364 if (pos == SNDRV_PCM_POS_XRUN) {
365 xrun(substream);
366 return -EPIPE;
367 }
Takashi Iwaicedb8112009-07-23 11:04:13 +0200368 if (xrun_debug(substream, 16)) {
369 char name[16];
370 pcm_debug_name(substream, name, sizeof(name));
371 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
372 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200373 name, (unsigned int)pos,
374 (unsigned int)runtime->period_size,
375 (unsigned int)runtime->buffer_size,
376 (unsigned long)old_hw_ptr,
377 (unsigned long)runtime->hw_ptr_base,
378 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200379 }
380
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100381 hw_base = runtime->hw_ptr_base;
382 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100384 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200385 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100386 if (delta < 0) {
387 delta += runtime->buffer_size;
388 if (delta < 0) {
389 hw_ptr_error(substream,
390 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200391 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100392 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200393 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
395 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100396 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100397 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100398 hw_base = 0;
399 new_hw_ptr = hw_base + pos;
400 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200401 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200402 if (!xrun_debug(substream, 4))
403 goto no_jiffies_check;
404 if (delta < runtime->delay)
405 goto no_jiffies_check;
406 delta -= runtime->delay;
407 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100408 hw_ptr_error(substream,
409 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200410 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100411 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200412 (long)runtime->period_size, jdelta,
413 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200416 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
418 runtime->silence_size > 0)
419 snd_pcm_playback_silence(substream, new_hw_ptr);
420
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200421 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200422 return 0;
423
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100424 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200426 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200427 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
428 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return snd_pcm_update_hw_ptr_post(substream, runtime);
431}
432
433/**
434 * snd_pcm_set_ops - set the PCM operators
435 * @pcm: the pcm instance
436 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
437 * @ops: the operator table
438 *
439 * Sets the given PCM operators to the pcm instance.
440 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100441void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Takashi Iwai877211f2005-11-17 13:59:38 +0100443 struct snd_pcm_str *stream = &pcm->streams[direction];
444 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 for (substream = stream->substream; substream != NULL; substream = substream->next)
447 substream->ops = ops;
448}
449
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200450EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/**
453 * snd_pcm_sync - set the PCM sync id
454 * @substream: the pcm substream
455 *
456 * Sets the PCM sync identifier for the card.
457 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100458void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai877211f2005-11-17 13:59:38 +0100460 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 runtime->sync.id32[0] = substream->pcm->card->number;
463 runtime->sync.id32[1] = -1;
464 runtime->sync.id32[2] = -1;
465 runtime->sync.id32[3] = -1;
466}
467
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200468EXPORT_SYMBOL(snd_pcm_set_sync);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Standard ioctl routine
472 */
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static inline unsigned int div32(unsigned int a, unsigned int b,
475 unsigned int *r)
476{
477 if (b == 0) {
478 *r = 0;
479 return UINT_MAX;
480 }
481 *r = a % b;
482 return a / b;
483}
484
485static inline unsigned int div_down(unsigned int a, unsigned int b)
486{
487 if (b == 0)
488 return UINT_MAX;
489 return a / b;
490}
491
492static inline unsigned int div_up(unsigned int a, unsigned int b)
493{
494 unsigned int r;
495 unsigned int q;
496 if (b == 0)
497 return UINT_MAX;
498 q = div32(a, b, &r);
499 if (r)
500 ++q;
501 return q;
502}
503
504static inline unsigned int mul(unsigned int a, unsigned int b)
505{
506 if (a == 0)
507 return 0;
508 if (div_down(UINT_MAX, a) < b)
509 return UINT_MAX;
510 return a * b;
511}
512
513static inline unsigned int muldiv32(unsigned int a, unsigned int b,
514 unsigned int c, unsigned int *r)
515{
516 u_int64_t n = (u_int64_t) a * b;
517 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200518 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *r = 0;
520 return UINT_MAX;
521 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200522 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (n >= UINT_MAX) {
524 *r = 0;
525 return UINT_MAX;
526 }
527 return n;
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/**
531 * snd_interval_refine - refine the interval value of configurator
532 * @i: the interval value to refine
533 * @v: the interval value to refer to
534 *
535 * Refines the interval value with the reference value.
536 * The interval is changed to the range satisfying both intervals.
537 * The interval status (min, max, integer, etc.) are evaluated.
538 *
539 * Returns non-zero if the value is changed, zero if not changed.
540 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100541int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200544 if (snd_BUG_ON(snd_interval_empty(i)))
545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (i->min < v->min) {
547 i->min = v->min;
548 i->openmin = v->openmin;
549 changed = 1;
550 } else if (i->min == v->min && !i->openmin && v->openmin) {
551 i->openmin = 1;
552 changed = 1;
553 }
554 if (i->max > v->max) {
555 i->max = v->max;
556 i->openmax = v->openmax;
557 changed = 1;
558 } else if (i->max == v->max && !i->openmax && v->openmax) {
559 i->openmax = 1;
560 changed = 1;
561 }
562 if (!i->integer && v->integer) {
563 i->integer = 1;
564 changed = 1;
565 }
566 if (i->integer) {
567 if (i->openmin) {
568 i->min++;
569 i->openmin = 0;
570 }
571 if (i->openmax) {
572 i->max--;
573 i->openmax = 0;
574 }
575 } else if (!i->openmin && !i->openmax && i->min == i->max)
576 i->integer = 1;
577 if (snd_interval_checkempty(i)) {
578 snd_interval_none(i);
579 return -EINVAL;
580 }
581 return changed;
582}
583
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200584EXPORT_SYMBOL(snd_interval_refine);
585
Takashi Iwai877211f2005-11-17 13:59:38 +0100586static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200588 if (snd_BUG_ON(snd_interval_empty(i)))
589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (snd_interval_single(i))
591 return 0;
592 i->max = i->min;
593 i->openmax = i->openmin;
594 if (i->openmax)
595 i->max++;
596 return 1;
597}
598
Takashi Iwai877211f2005-11-17 13:59:38 +0100599static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200601 if (snd_BUG_ON(snd_interval_empty(i)))
602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (snd_interval_single(i))
604 return 0;
605 i->min = i->max;
606 i->openmin = i->openmax;
607 if (i->openmin)
608 i->min--;
609 return 1;
610}
611
Takashi Iwai877211f2005-11-17 13:59:38 +0100612void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 if (a->empty || b->empty) {
615 snd_interval_none(c);
616 return;
617 }
618 c->empty = 0;
619 c->min = mul(a->min, b->min);
620 c->openmin = (a->openmin || b->openmin);
621 c->max = mul(a->max, b->max);
622 c->openmax = (a->openmax || b->openmax);
623 c->integer = (a->integer && b->integer);
624}
625
626/**
627 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200628 * @a: dividend
629 * @b: divisor
630 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
632 * c = a / b
633 *
634 * Returns non-zero if the value is changed, zero if not changed.
635 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100636void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 unsigned int r;
639 if (a->empty || b->empty) {
640 snd_interval_none(c);
641 return;
642 }
643 c->empty = 0;
644 c->min = div32(a->min, b->max, &r);
645 c->openmin = (r || a->openmin || b->openmax);
646 if (b->min > 0) {
647 c->max = div32(a->max, b->min, &r);
648 if (r) {
649 c->max++;
650 c->openmax = 1;
651 } else
652 c->openmax = (a->openmax || b->openmin);
653 } else {
654 c->max = UINT_MAX;
655 c->openmax = 0;
656 }
657 c->integer = 0;
658}
659
660/**
661 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200662 * @a: dividend 1
663 * @b: dividend 2
664 * @k: divisor (as integer)
665 * @c: result
666 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * c = a * b / k
668 *
669 * Returns non-zero if the value is changed, zero if not changed.
670 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100671void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
672 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 unsigned int r;
675 if (a->empty || b->empty) {
676 snd_interval_none(c);
677 return;
678 }
679 c->empty = 0;
680 c->min = muldiv32(a->min, b->min, k, &r);
681 c->openmin = (r || a->openmin || b->openmin);
682 c->max = muldiv32(a->max, b->max, k, &r);
683 if (r) {
684 c->max++;
685 c->openmax = 1;
686 } else
687 c->openmax = (a->openmax || b->openmax);
688 c->integer = 0;
689}
690
691/**
692 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200693 * @a: dividend 1
694 * @k: dividend 2 (as integer)
695 * @b: divisor
696 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
698 * c = a * k / b
699 *
700 * Returns non-zero if the value is changed, zero if not changed.
701 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100702void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
703 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 unsigned int r;
706 if (a->empty || b->empty) {
707 snd_interval_none(c);
708 return;
709 }
710 c->empty = 0;
711 c->min = muldiv32(a->min, k, b->max, &r);
712 c->openmin = (r || a->openmin || b->openmax);
713 if (b->min > 0) {
714 c->max = muldiv32(a->max, k, b->min, &r);
715 if (r) {
716 c->max++;
717 c->openmax = 1;
718 } else
719 c->openmax = (a->openmax || b->openmin);
720 } else {
721 c->max = UINT_MAX;
722 c->openmax = 0;
723 }
724 c->integer = 0;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/* ---- */
728
729
730/**
731 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200732 * @i: interval to refine
733 * @rats_count: number of ratnum_t
734 * @rats: ratnum_t array
735 * @nump: pointer to store the resultant numerator
736 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
738 * Returns non-zero if the value is changed, zero if not changed.
739 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100740int snd_interval_ratnum(struct snd_interval *i,
741 unsigned int rats_count, struct snd_ratnum *rats,
742 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 unsigned int best_num, best_diff, best_den;
745 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100746 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int err;
748
749 best_num = best_den = best_diff = 0;
750 for (k = 0; k < rats_count; ++k) {
751 unsigned int num = rats[k].num;
752 unsigned int den;
753 unsigned int q = i->min;
754 int diff;
755 if (q == 0)
756 q = 1;
757 den = div_down(num, q);
758 if (den < rats[k].den_min)
759 continue;
760 if (den > rats[k].den_max)
761 den = rats[k].den_max;
762 else {
763 unsigned int r;
764 r = (den - rats[k].den_min) % rats[k].den_step;
765 if (r != 0)
766 den -= r;
767 }
768 diff = num - q * den;
769 if (best_num == 0 ||
770 diff * best_den < best_diff * den) {
771 best_diff = diff;
772 best_den = den;
773 best_num = num;
774 }
775 }
776 if (best_den == 0) {
777 i->empty = 1;
778 return -EINVAL;
779 }
780 t.min = div_down(best_num, best_den);
781 t.openmin = !!(best_num % best_den);
782
783 best_num = best_den = best_diff = 0;
784 for (k = 0; k < rats_count; ++k) {
785 unsigned int num = rats[k].num;
786 unsigned int den;
787 unsigned int q = i->max;
788 int diff;
789 if (q == 0) {
790 i->empty = 1;
791 return -EINVAL;
792 }
793 den = div_up(num, q);
794 if (den > rats[k].den_max)
795 continue;
796 if (den < rats[k].den_min)
797 den = rats[k].den_min;
798 else {
799 unsigned int r;
800 r = (den - rats[k].den_min) % rats[k].den_step;
801 if (r != 0)
802 den += rats[k].den_step - r;
803 }
804 diff = q * den - num;
805 if (best_num == 0 ||
806 diff * best_den < best_diff * den) {
807 best_diff = diff;
808 best_den = den;
809 best_num = num;
810 }
811 }
812 if (best_den == 0) {
813 i->empty = 1;
814 return -EINVAL;
815 }
816 t.max = div_up(best_num, best_den);
817 t.openmax = !!(best_num % best_den);
818 t.integer = 0;
819 err = snd_interval_refine(i, &t);
820 if (err < 0)
821 return err;
822
823 if (snd_interval_single(i)) {
824 if (nump)
825 *nump = best_num;
826 if (denp)
827 *denp = best_den;
828 }
829 return err;
830}
831
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200832EXPORT_SYMBOL(snd_interval_ratnum);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/**
835 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200836 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100837 * @rats_count: number of struct ratden
838 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200839 * @nump: pointer to store the resultant numerator
840 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 *
842 * Returns non-zero if the value is changed, zero if not changed.
843 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100844static int snd_interval_ratden(struct snd_interval *i,
845 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int *nump, unsigned int *denp)
847{
848 unsigned int best_num, best_diff, best_den;
849 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100850 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int err;
852
853 best_num = best_den = best_diff = 0;
854 for (k = 0; k < rats_count; ++k) {
855 unsigned int num;
856 unsigned int den = rats[k].den;
857 unsigned int q = i->min;
858 int diff;
859 num = mul(q, den);
860 if (num > rats[k].num_max)
861 continue;
862 if (num < rats[k].num_min)
863 num = rats[k].num_max;
864 else {
865 unsigned int r;
866 r = (num - rats[k].num_min) % rats[k].num_step;
867 if (r != 0)
868 num += rats[k].num_step - r;
869 }
870 diff = num - q * den;
871 if (best_num == 0 ||
872 diff * best_den < best_diff * den) {
873 best_diff = diff;
874 best_den = den;
875 best_num = num;
876 }
877 }
878 if (best_den == 0) {
879 i->empty = 1;
880 return -EINVAL;
881 }
882 t.min = div_down(best_num, best_den);
883 t.openmin = !!(best_num % best_den);
884
885 best_num = best_den = best_diff = 0;
886 for (k = 0; k < rats_count; ++k) {
887 unsigned int num;
888 unsigned int den = rats[k].den;
889 unsigned int q = i->max;
890 int diff;
891 num = mul(q, den);
892 if (num < rats[k].num_min)
893 continue;
894 if (num > rats[k].num_max)
895 num = rats[k].num_max;
896 else {
897 unsigned int r;
898 r = (num - rats[k].num_min) % rats[k].num_step;
899 if (r != 0)
900 num -= r;
901 }
902 diff = q * den - num;
903 if (best_num == 0 ||
904 diff * best_den < best_diff * den) {
905 best_diff = diff;
906 best_den = den;
907 best_num = num;
908 }
909 }
910 if (best_den == 0) {
911 i->empty = 1;
912 return -EINVAL;
913 }
914 t.max = div_up(best_num, best_den);
915 t.openmax = !!(best_num % best_den);
916 t.integer = 0;
917 err = snd_interval_refine(i, &t);
918 if (err < 0)
919 return err;
920
921 if (snd_interval_single(i)) {
922 if (nump)
923 *nump = best_num;
924 if (denp)
925 *denp = best_den;
926 }
927 return err;
928}
929
930/**
931 * snd_interval_list - refine the interval value from the list
932 * @i: the interval value to refine
933 * @count: the number of elements in the list
934 * @list: the value list
935 * @mask: the bit-mask to evaluate
936 *
937 * Refines the interval value from the list.
938 * When mask is non-zero, only the elements corresponding to bit 1 are
939 * evaluated.
940 *
941 * Returns non-zero if the value is changed, zero if not changed.
942 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100943int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 unsigned int k;
946 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100947
948 if (!count) {
949 i->empty = 1;
950 return -EINVAL;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (k = 0; k < count; k++) {
953 if (mask && !(mask & (1 << k)))
954 continue;
955 if (i->min == list[k] && !i->openmin)
956 goto _l1;
957 if (i->min < list[k]) {
958 i->min = list[k];
959 i->openmin = 0;
960 changed = 1;
961 goto _l1;
962 }
963 }
964 i->empty = 1;
965 return -EINVAL;
966 _l1:
967 for (k = count; k-- > 0;) {
968 if (mask && !(mask & (1 << k)))
969 continue;
970 if (i->max == list[k] && !i->openmax)
971 goto _l2;
972 if (i->max > list[k]) {
973 i->max = list[k];
974 i->openmax = 0;
975 changed = 1;
976 goto _l2;
977 }
978 }
979 i->empty = 1;
980 return -EINVAL;
981 _l2:
982 if (snd_interval_checkempty(i)) {
983 i->empty = 1;
984 return -EINVAL;
985 }
986 return changed;
987}
988
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200989EXPORT_SYMBOL(snd_interval_list);
990
Takashi Iwai877211f2005-11-17 13:59:38 +0100991static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 unsigned int n;
994 int changed = 0;
995 n = (i->min - min) % step;
996 if (n != 0 || i->openmin) {
997 i->min += step - n;
998 changed = 1;
999 }
1000 n = (i->max - min) % step;
1001 if (n != 0 || i->openmax) {
1002 i->max -= n;
1003 changed = 1;
1004 }
1005 if (snd_interval_checkempty(i)) {
1006 i->empty = 1;
1007 return -EINVAL;
1008 }
1009 return changed;
1010}
1011
1012/* Info constraints helpers */
1013
1014/**
1015 * snd_pcm_hw_rule_add - add the hw-constraint rule
1016 * @runtime: the pcm runtime instance
1017 * @cond: condition bits
1018 * @var: the variable to evaluate
1019 * @func: the evaluation function
1020 * @private: the private data pointer passed to function
1021 * @dep: the dependent variables
1022 *
1023 * Returns zero if successful, or a negative error code on failure.
1024 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001025int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int var,
1027 snd_pcm_hw_rule_func_t func, void *private,
1028 int dep, ...)
1029{
Takashi Iwai877211f2005-11-17 13:59:38 +01001030 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1031 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned int k;
1033 va_list args;
1034 va_start(args, dep);
1035 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001036 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unsigned int new_rules = constrs->rules_all + 16;
1038 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1039 if (!new)
1040 return -ENOMEM;
1041 if (constrs->rules) {
1042 memcpy(new, constrs->rules,
1043 constrs->rules_num * sizeof(*c));
1044 kfree(constrs->rules);
1045 }
1046 constrs->rules = new;
1047 constrs->rules_all = new_rules;
1048 }
1049 c = &constrs->rules[constrs->rules_num];
1050 c->cond = cond;
1051 c->func = func;
1052 c->var = var;
1053 c->private = private;
1054 k = 0;
1055 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001056 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1057 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 c->deps[k++] = dep;
1059 if (dep < 0)
1060 break;
1061 dep = va_arg(args, int);
1062 }
1063 constrs->rules_num++;
1064 va_end(args);
1065 return 0;
1066}
1067
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001068EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001071 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001072 * @runtime: PCM runtime instance
1073 * @var: hw_params variable to apply the mask
1074 * @mask: the bitmap mask
1075 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001076 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001078int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 u_int32_t mask)
1080{
Takashi Iwai877211f2005-11-17 13:59:38 +01001081 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1082 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *maskp->bits &= mask;
1084 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1085 if (*maskp->bits == 0)
1086 return -EINVAL;
1087 return 0;
1088}
1089
1090/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001091 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001092 * @runtime: PCM runtime instance
1093 * @var: hw_params variable to apply the mask
1094 * @mask: the 64bit bitmap mask
1095 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001096 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001098int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 u_int64_t mask)
1100{
Takashi Iwai877211f2005-11-17 13:59:38 +01001101 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1102 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 maskp->bits[0] &= (u_int32_t)mask;
1104 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1105 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1106 if (! maskp->bits[0] && ! maskp->bits[1])
1107 return -EINVAL;
1108 return 0;
1109}
1110
1111/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001112 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001113 * @runtime: PCM runtime instance
1114 * @var: hw_params variable to apply the integer constraint
1115 *
1116 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001118int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Takashi Iwai877211f2005-11-17 13:59:38 +01001120 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return snd_interval_setinteger(constrs_interval(constrs, var));
1122}
1123
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001124EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001127 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001128 * @runtime: PCM runtime instance
1129 * @var: hw_params variable to apply the range
1130 * @min: the minimal value
1131 * @max: the maximal value
1132 *
1133 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001135int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 unsigned int min, unsigned int max)
1137{
Takashi Iwai877211f2005-11-17 13:59:38 +01001138 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1139 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 t.min = min;
1141 t.max = max;
1142 t.openmin = t.openmax = 0;
1143 t.integer = 0;
1144 return snd_interval_refine(constrs_interval(constrs, var), &t);
1145}
1146
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001147EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1148
Takashi Iwai877211f2005-11-17 13:59:38 +01001149static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1150 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Takashi Iwai877211f2005-11-17 13:59:38 +01001152 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1154}
1155
1156
1157/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001158 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001159 * @runtime: PCM runtime instance
1160 * @cond: condition bits
1161 * @var: hw_params variable to apply the list constraint
1162 * @l: list
1163 *
1164 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001166int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned int cond,
1168 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 return snd_pcm_hw_rule_add(runtime, cond, var,
1172 snd_pcm_hw_rule_list, l,
1173 var, -1);
1174}
1175
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001176EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1177
Takashi Iwai877211f2005-11-17 13:59:38 +01001178static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1179 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Takashi Iwai877211f2005-11-17 13:59:38 +01001181 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 unsigned int num = 0, den = 0;
1183 int err;
1184 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1185 r->nrats, r->rats, &num, &den);
1186 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1187 params->rate_num = num;
1188 params->rate_den = den;
1189 }
1190 return err;
1191}
1192
1193/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001194 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001195 * @runtime: PCM runtime instance
1196 * @cond: condition bits
1197 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001198 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001200int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 unsigned int cond,
1202 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001203 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 return snd_pcm_hw_rule_add(runtime, cond, var,
1206 snd_pcm_hw_rule_ratnums, r,
1207 var, -1);
1208}
1209
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001210EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1211
Takashi Iwai877211f2005-11-17 13:59:38 +01001212static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1213 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unsigned int num = 0, den = 0;
1217 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1218 r->nrats, r->rats, &num, &den);
1219 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1220 params->rate_num = num;
1221 params->rate_den = den;
1222 }
1223 return err;
1224}
1225
1226/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001227 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001228 * @runtime: PCM runtime instance
1229 * @cond: condition bits
1230 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001231 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001233int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned int cond,
1235 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001236 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 return snd_pcm_hw_rule_add(runtime, cond, var,
1239 snd_pcm_hw_rule_ratdens, r,
1240 var, -1);
1241}
1242
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001243EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1244
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1246 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 unsigned int l = (unsigned long) rule->private;
1249 int width = l & 0xffff;
1250 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001251 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (snd_interval_single(i) && snd_interval_value(i) == width)
1253 params->msbits = msbits;
1254 return 0;
1255}
1256
1257/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001258 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001259 * @runtime: PCM runtime instance
1260 * @cond: condition bits
1261 * @width: sample bits width
1262 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001264int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 unsigned int cond,
1266 unsigned int width,
1267 unsigned int msbits)
1268{
1269 unsigned long l = (msbits << 16) | width;
1270 return snd_pcm_hw_rule_add(runtime, cond, -1,
1271 snd_pcm_hw_rule_msbits,
1272 (void*) l,
1273 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1274}
1275
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001276EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1277
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1279 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 unsigned long step = (unsigned long) rule->private;
1282 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1283}
1284
1285/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001286 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001287 * @runtime: PCM runtime instance
1288 * @cond: condition bits
1289 * @var: hw_params variable to apply the step constraint
1290 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001292int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 unsigned int cond,
1294 snd_pcm_hw_param_t var,
1295 unsigned long step)
1296{
1297 return snd_pcm_hw_rule_add(runtime, cond, var,
1298 snd_pcm_hw_rule_step, (void *) step,
1299 var, -1);
1300}
1301
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001302EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1303
Takashi Iwai877211f2005-11-17 13:59:38 +01001304static 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 -07001305{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001306 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1308 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1309 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1310 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1311 };
1312 return snd_interval_list(hw_param_interval(params, rule->var),
1313 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1314}
1315
1316/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001317 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001318 * @runtime: PCM runtime instance
1319 * @cond: condition bits
1320 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001322int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned int cond,
1324 snd_pcm_hw_param_t var)
1325{
1326 return snd_pcm_hw_rule_add(runtime, cond, var,
1327 snd_pcm_hw_rule_pow2, NULL,
1328 var, -1);
1329}
1330
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001331EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1332
Takashi Iwai877211f2005-11-17 13:59:38 +01001333static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001334 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 if (hw_is_mask(var)) {
1337 snd_mask_any(hw_param_mask(params, var));
1338 params->cmask |= 1 << var;
1339 params->rmask |= 1 << var;
1340 return;
1341 }
1342 if (hw_is_interval(var)) {
1343 snd_interval_any(hw_param_interval(params, var));
1344 params->cmask |= 1 << var;
1345 params->rmask |= 1 << var;
1346 return;
1347 }
1348 snd_BUG();
1349}
1350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 unsigned int k;
1354 memset(params, 0, sizeof(*params));
1355 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1356 _snd_pcm_hw_param_any(params, k);
1357 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1358 _snd_pcm_hw_param_any(params, k);
1359 params->info = ~0U;
1360}
1361
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001362EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001365 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001366 * @params: the hw_params instance
1367 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001368 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001370 * Return the value for field @var if it's fixed in configuration space
1371 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001373int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1374 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001377 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!snd_mask_single(mask))
1379 return -EINVAL;
1380 if (dir)
1381 *dir = 0;
1382 return snd_mask_value(mask);
1383 }
1384 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001385 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!snd_interval_single(i))
1387 return -EINVAL;
1388 if (dir)
1389 *dir = i->openmin;
1390 return snd_interval_value(i);
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EINVAL;
1393}
1394
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001395EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Takashi Iwai877211f2005-11-17 13:59:38 +01001397void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 snd_pcm_hw_param_t var)
1399{
1400 if (hw_is_mask(var)) {
1401 snd_mask_none(hw_param_mask(params, var));
1402 params->cmask |= 1 << var;
1403 params->rmask |= 1 << var;
1404 } else if (hw_is_interval(var)) {
1405 snd_interval_none(hw_param_interval(params, var));
1406 params->cmask |= 1 << var;
1407 params->rmask |= 1 << var;
1408 } else {
1409 snd_BUG();
1410 }
1411}
1412
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001413EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Takashi Iwai877211f2005-11-17 13:59:38 +01001415static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001416 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 int changed;
1419 if (hw_is_mask(var))
1420 changed = snd_mask_refine_first(hw_param_mask(params, var));
1421 else if (hw_is_interval(var))
1422 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001423 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (changed) {
1426 params->cmask |= 1 << var;
1427 params->rmask |= 1 << var;
1428 }
1429 return changed;
1430}
1431
1432
1433/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001435 * @pcm: PCM instance
1436 * @params: the hw_params instance
1437 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001438 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001440 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * values > minimum. Reduce configuration space accordingly.
1442 * Return the minimum.
1443 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001444int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1445 struct snd_pcm_hw_params *params,
1446 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 int changed = _snd_pcm_hw_param_first(params, var);
1449 if (changed < 0)
1450 return changed;
1451 if (params->rmask) {
1452 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001453 if (snd_BUG_ON(err < 0))
1454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 return snd_pcm_hw_param_value(params, var, dir);
1457}
1458
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001459EXPORT_SYMBOL(snd_pcm_hw_param_first);
1460
Takashi Iwai877211f2005-11-17 13:59:38 +01001461static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001462 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int changed;
1465 if (hw_is_mask(var))
1466 changed = snd_mask_refine_last(hw_param_mask(params, var));
1467 else if (hw_is_interval(var))
1468 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001469 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (changed) {
1472 params->cmask |= 1 << var;
1473 params->rmask |= 1 << var;
1474 }
1475 return changed;
1476}
1477
1478
1479/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001480 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001481 * @pcm: PCM instance
1482 * @params: the hw_params instance
1483 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001484 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001486 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * values < maximum. Reduce configuration space accordingly.
1488 * Return the maximum.
1489 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001490int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1491 struct snd_pcm_hw_params *params,
1492 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int changed = _snd_pcm_hw_param_last(params, var);
1495 if (changed < 0)
1496 return changed;
1497 if (params->rmask) {
1498 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001499 if (snd_BUG_ON(err < 0))
1500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502 return snd_pcm_hw_param_value(params, var, dir);
1503}
1504
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001505EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001508 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001509 * @pcm: PCM instance
1510 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001512 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * The configuration chosen is that obtained fixing in this order:
1514 * first access, first format, first subformat, min channels,
1515 * min rate, min period time, max buffer size, min tick time
1516 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001517int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1518 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001520 static int vars[] = {
1521 SNDRV_PCM_HW_PARAM_ACCESS,
1522 SNDRV_PCM_HW_PARAM_FORMAT,
1523 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1524 SNDRV_PCM_HW_PARAM_CHANNELS,
1525 SNDRV_PCM_HW_PARAM_RATE,
1526 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1527 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1528 SNDRV_PCM_HW_PARAM_TICK_TIME,
1529 -1
1530 };
1531 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001533 for (v = vars; *v != -1; v++) {
1534 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1535 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1536 else
1537 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001538 if (snd_BUG_ON(err < 0))
1539 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542}
1543
Takashi Iwai877211f2005-11-17 13:59:38 +01001544static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 void *arg)
1546{
Takashi Iwai877211f2005-11-17 13:59:38 +01001547 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 unsigned long flags;
1549 snd_pcm_stream_lock_irqsave(substream, flags);
1550 if (snd_pcm_running(substream) &&
1551 snd_pcm_update_hw_ptr(substream) >= 0)
1552 runtime->status->hw_ptr %= runtime->buffer_size;
1553 else
1554 runtime->status->hw_ptr = 0;
1555 snd_pcm_stream_unlock_irqrestore(substream, flags);
1556 return 0;
1557}
1558
Takashi Iwai877211f2005-11-17 13:59:38 +01001559static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 void *arg)
1561{
Takashi Iwai877211f2005-11-17 13:59:38 +01001562 struct snd_pcm_channel_info *info = arg;
1563 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int width;
1565 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1566 info->offset = -1;
1567 return 0;
1568 }
1569 width = snd_pcm_format_physical_width(runtime->format);
1570 if (width < 0)
1571 return width;
1572 info->offset = 0;
1573 switch (runtime->access) {
1574 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1575 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1576 info->first = info->channel * width;
1577 info->step = runtime->channels * width;
1578 break;
1579 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1580 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1581 {
1582 size_t size = runtime->dma_bytes / runtime->channels;
1583 info->first = info->channel * size * 8;
1584 info->step = width;
1585 break;
1586 }
1587 default:
1588 snd_BUG();
1589 break;
1590 }
1591 return 0;
1592}
1593
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001594static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1595 void *arg)
1596{
1597 struct snd_pcm_hw_params *params = arg;
1598 snd_pcm_format_t format;
1599 int channels, width;
1600
1601 params->fifo_size = substream->runtime->hw.fifo_size;
1602 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1603 format = params_format(params);
1604 channels = params_channels(params);
1605 width = snd_pcm_format_physical_width(format);
1606 params->fifo_size /= width * channels;
1607 }
1608 return 0;
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/**
1612 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1613 * @substream: the pcm substream instance
1614 * @cmd: ioctl command
1615 * @arg: ioctl argument
1616 *
1617 * Processes the generic ioctl commands for PCM.
1618 * Can be passed as the ioctl callback for PCM ops.
1619 *
1620 * Returns zero if successful, or a negative error code on failure.
1621 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001622int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 unsigned int cmd, void *arg)
1624{
1625 switch (cmd) {
1626 case SNDRV_PCM_IOCTL1_INFO:
1627 return 0;
1628 case SNDRV_PCM_IOCTL1_RESET:
1629 return snd_pcm_lib_ioctl_reset(substream, arg);
1630 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1631 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001632 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1633 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 return -ENXIO;
1636}
1637
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001638EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640/**
1641 * snd_pcm_period_elapsed - update the pcm status for the next period
1642 * @substream: the pcm substream instance
1643 *
1644 * This function is called from the interrupt handler when the
1645 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001646 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
1648 * Even if more than one periods have elapsed since the last call, you
1649 * have to call this only once.
1650 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001651void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwai877211f2005-11-17 13:59:38 +01001653 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 unsigned long flags;
1655
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001656 if (PCM_RUNTIME_CHECK(substream))
1657 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (runtime->transfer_ack_begin)
1661 runtime->transfer_ack_begin(substream);
1662
1663 snd_pcm_stream_lock_irqsave(substream, flags);
1664 if (!snd_pcm_running(substream) ||
1665 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1666 goto _end;
1667
1668 if (substream->timer_running)
1669 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 _end:
1671 snd_pcm_stream_unlock_irqrestore(substream, flags);
1672 if (runtime->transfer_ack_end)
1673 runtime->transfer_ack_end(substream);
1674 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1675}
1676
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001677EXPORT_SYMBOL(snd_pcm_period_elapsed);
1678
Takashi Iwai13075512008-01-08 18:08:14 +01001679/*
1680 * Wait until avail_min data becomes available
1681 * Returns a negative error code if any error occurs during operation.
1682 * The available space is stored on availp. When err = 0 and avail = 0
1683 * on the capture stream, it indicates the stream is in DRAINING state.
1684 */
1685static int wait_for_avail_min(struct snd_pcm_substream *substream,
1686 snd_pcm_uframes_t *availp)
1687{
1688 struct snd_pcm_runtime *runtime = substream->runtime;
1689 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1690 wait_queue_t wait;
1691 int err = 0;
1692 snd_pcm_uframes_t avail = 0;
1693 long tout;
1694
1695 init_waitqueue_entry(&wait, current);
1696 add_wait_queue(&runtime->sleep, &wait);
1697 for (;;) {
1698 if (signal_pending(current)) {
1699 err = -ERESTARTSYS;
1700 break;
1701 }
1702 set_current_state(TASK_INTERRUPTIBLE);
1703 snd_pcm_stream_unlock_irq(substream);
1704 tout = schedule_timeout(msecs_to_jiffies(10000));
1705 snd_pcm_stream_lock_irq(substream);
1706 switch (runtime->status->state) {
1707 case SNDRV_PCM_STATE_SUSPENDED:
1708 err = -ESTRPIPE;
1709 goto _endloop;
1710 case SNDRV_PCM_STATE_XRUN:
1711 err = -EPIPE;
1712 goto _endloop;
1713 case SNDRV_PCM_STATE_DRAINING:
1714 if (is_playback)
1715 err = -EPIPE;
1716 else
1717 avail = 0; /* indicate draining */
1718 goto _endloop;
1719 case SNDRV_PCM_STATE_OPEN:
1720 case SNDRV_PCM_STATE_SETUP:
1721 case SNDRV_PCM_STATE_DISCONNECTED:
1722 err = -EBADFD;
1723 goto _endloop;
1724 }
1725 if (!tout) {
1726 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1727 is_playback ? "playback" : "capture");
1728 err = -EIO;
1729 break;
1730 }
1731 if (is_playback)
1732 avail = snd_pcm_playback_avail(runtime);
1733 else
1734 avail = snd_pcm_capture_avail(runtime);
1735 if (avail >= runtime->control->avail_min)
1736 break;
1737 }
1738 _endloop:
1739 remove_wait_queue(&runtime->sleep, &wait);
1740 *availp = avail;
1741 return err;
1742}
1743
Takashi Iwai877211f2005-11-17 13:59:38 +01001744static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 unsigned int hwoff,
1746 unsigned long data, unsigned int off,
1747 snd_pcm_uframes_t frames)
1748{
Takashi Iwai877211f2005-11-17 13:59:38 +01001749 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int err;
1751 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1752 if (substream->ops->copy) {
1753 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1754 return err;
1755 } else {
1756 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1758 return -EFAULT;
1759 }
1760 return 0;
1761}
1762
Takashi Iwai877211f2005-11-17 13:59:38 +01001763typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 unsigned long data, unsigned int off,
1765 snd_pcm_uframes_t size);
1766
Takashi Iwai877211f2005-11-17 13:59:38 +01001767static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned long data,
1769 snd_pcm_uframes_t size,
1770 int nonblock,
1771 transfer_f transfer)
1772{
Takashi Iwai877211f2005-11-17 13:59:38 +01001773 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 snd_pcm_uframes_t xfer = 0;
1775 snd_pcm_uframes_t offset = 0;
1776 int err = 0;
1777
1778 if (size == 0)
1779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 snd_pcm_stream_lock_irq(substream);
1782 switch (runtime->status->state) {
1783 case SNDRV_PCM_STATE_PREPARED:
1784 case SNDRV_PCM_STATE_RUNNING:
1785 case SNDRV_PCM_STATE_PAUSED:
1786 break;
1787 case SNDRV_PCM_STATE_XRUN:
1788 err = -EPIPE;
1789 goto _end_unlock;
1790 case SNDRV_PCM_STATE_SUSPENDED:
1791 err = -ESTRPIPE;
1792 goto _end_unlock;
1793 default:
1794 err = -EBADFD;
1795 goto _end_unlock;
1796 }
1797
1798 while (size > 0) {
1799 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1800 snd_pcm_uframes_t avail;
1801 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001802 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 snd_pcm_update_hw_ptr(substream);
1804 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001805 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (nonblock) {
1807 err = -EAGAIN;
1808 goto _end_unlock;
1809 }
Takashi Iwai13075512008-01-08 18:08:14 +01001810 err = wait_for_avail_min(substream, &avail);
1811 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 frames = size > avail ? avail : size;
1815 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1816 if (frames > cont)
1817 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001818 if (snd_BUG_ON(!frames)) {
1819 snd_pcm_stream_unlock_irq(substream);
1820 return -EINVAL;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 appl_ptr = runtime->control->appl_ptr;
1823 appl_ofs = appl_ptr % runtime->buffer_size;
1824 snd_pcm_stream_unlock_irq(substream);
1825 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1826 goto _end;
1827 snd_pcm_stream_lock_irq(substream);
1828 switch (runtime->status->state) {
1829 case SNDRV_PCM_STATE_XRUN:
1830 err = -EPIPE;
1831 goto _end_unlock;
1832 case SNDRV_PCM_STATE_SUSPENDED:
1833 err = -ESTRPIPE;
1834 goto _end_unlock;
1835 default:
1836 break;
1837 }
1838 appl_ptr += frames;
1839 if (appl_ptr >= runtime->boundary)
1840 appl_ptr -= runtime->boundary;
1841 runtime->control->appl_ptr = appl_ptr;
1842 if (substream->ops->ack)
1843 substream->ops->ack(substream);
1844
1845 offset += frames;
1846 size -= frames;
1847 xfer += frames;
1848 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1849 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1850 err = snd_pcm_start(substream);
1851 if (err < 0)
1852 goto _end_unlock;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 _end_unlock:
1856 snd_pcm_stream_unlock_irq(substream);
1857 _end:
1858 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1859}
1860
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001861/* sanity-check for read/write methods */
1862static int pcm_sanity_check(struct snd_pcm_substream *substream)
1863{
1864 struct snd_pcm_runtime *runtime;
1865 if (PCM_RUNTIME_CHECK(substream))
1866 return -ENXIO;
1867 runtime = substream->runtime;
1868 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1869 return -EINVAL;
1870 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1871 return -EBADFD;
1872 return 0;
1873}
1874
Takashi Iwai877211f2005-11-17 13:59:38 +01001875snd_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 -07001876{
Takashi Iwai877211f2005-11-17 13:59:38 +01001877 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001879 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001881 err = pcm_sanity_check(substream);
1882 if (err < 0)
1883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001885 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1888 runtime->channels > 1)
1889 return -EINVAL;
1890 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1891 snd_pcm_lib_write_transfer);
1892}
1893
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001894EXPORT_SYMBOL(snd_pcm_lib_write);
1895
Takashi Iwai877211f2005-11-17 13:59:38 +01001896static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 unsigned int hwoff,
1898 unsigned long data, unsigned int off,
1899 snd_pcm_uframes_t frames)
1900{
Takashi Iwai877211f2005-11-17 13:59:38 +01001901 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 int err;
1903 void __user **bufs = (void __user **)data;
1904 int channels = runtime->channels;
1905 int c;
1906 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001907 if (snd_BUG_ON(!substream->ops->silence))
1908 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 for (c = 0; c < channels; ++c, ++bufs) {
1910 if (*bufs == NULL) {
1911 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1912 return err;
1913 } else {
1914 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1915 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1916 return err;
1917 }
1918 }
1919 } else {
1920 /* default transfer behaviour */
1921 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 for (c = 0; c < channels; ++c, ++bufs) {
1923 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1924 if (*bufs == NULL) {
1925 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1926 } else {
1927 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1928 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1929 return -EFAULT;
1930 }
1931 }
1932 }
1933 return 0;
1934}
1935
Takashi Iwai877211f2005-11-17 13:59:38 +01001936snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 void __user **bufs,
1938 snd_pcm_uframes_t frames)
1939{
Takashi Iwai877211f2005-11-17 13:59:38 +01001940 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001942 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001944 err = pcm_sanity_check(substream);
1945 if (err < 0)
1946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001948 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1951 return -EINVAL;
1952 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1953 nonblock, snd_pcm_lib_writev_transfer);
1954}
1955
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001956EXPORT_SYMBOL(snd_pcm_lib_writev);
1957
Takashi Iwai877211f2005-11-17 13:59:38 +01001958static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 unsigned int hwoff,
1960 unsigned long data, unsigned int off,
1961 snd_pcm_uframes_t frames)
1962{
Takashi Iwai877211f2005-11-17 13:59:38 +01001963 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 int err;
1965 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1966 if (substream->ops->copy) {
1967 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1968 return err;
1969 } else {
1970 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1972 return -EFAULT;
1973 }
1974 return 0;
1975}
1976
Takashi Iwai877211f2005-11-17 13:59:38 +01001977static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 unsigned long data,
1979 snd_pcm_uframes_t size,
1980 int nonblock,
1981 transfer_f transfer)
1982{
Takashi Iwai877211f2005-11-17 13:59:38 +01001983 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 snd_pcm_uframes_t xfer = 0;
1985 snd_pcm_uframes_t offset = 0;
1986 int err = 0;
1987
1988 if (size == 0)
1989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 snd_pcm_stream_lock_irq(substream);
1992 switch (runtime->status->state) {
1993 case SNDRV_PCM_STATE_PREPARED:
1994 if (size >= runtime->start_threshold) {
1995 err = snd_pcm_start(substream);
1996 if (err < 0)
1997 goto _end_unlock;
1998 }
1999 break;
2000 case SNDRV_PCM_STATE_DRAINING:
2001 case SNDRV_PCM_STATE_RUNNING:
2002 case SNDRV_PCM_STATE_PAUSED:
2003 break;
2004 case SNDRV_PCM_STATE_XRUN:
2005 err = -EPIPE;
2006 goto _end_unlock;
2007 case SNDRV_PCM_STATE_SUSPENDED:
2008 err = -ESTRPIPE;
2009 goto _end_unlock;
2010 default:
2011 err = -EBADFD;
2012 goto _end_unlock;
2013 }
2014
2015 while (size > 0) {
2016 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2017 snd_pcm_uframes_t avail;
2018 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002019 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002022 if (!avail) {
2023 if (runtime->status->state ==
2024 SNDRV_PCM_STATE_DRAINING) {
2025 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto _end_unlock;
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (nonblock) {
2029 err = -EAGAIN;
2030 goto _end_unlock;
2031 }
Takashi Iwai13075512008-01-08 18:08:14 +01002032 err = wait_for_avail_min(substream, &avail);
2033 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002035 if (!avail)
2036 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 frames = size > avail ? avail : size;
2039 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2040 if (frames > cont)
2041 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002042 if (snd_BUG_ON(!frames)) {
2043 snd_pcm_stream_unlock_irq(substream);
2044 return -EINVAL;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 appl_ptr = runtime->control->appl_ptr;
2047 appl_ofs = appl_ptr % runtime->buffer_size;
2048 snd_pcm_stream_unlock_irq(substream);
2049 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2050 goto _end;
2051 snd_pcm_stream_lock_irq(substream);
2052 switch (runtime->status->state) {
2053 case SNDRV_PCM_STATE_XRUN:
2054 err = -EPIPE;
2055 goto _end_unlock;
2056 case SNDRV_PCM_STATE_SUSPENDED:
2057 err = -ESTRPIPE;
2058 goto _end_unlock;
2059 default:
2060 break;
2061 }
2062 appl_ptr += frames;
2063 if (appl_ptr >= runtime->boundary)
2064 appl_ptr -= runtime->boundary;
2065 runtime->control->appl_ptr = appl_ptr;
2066 if (substream->ops->ack)
2067 substream->ops->ack(substream);
2068
2069 offset += frames;
2070 size -= frames;
2071 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 _end_unlock:
2074 snd_pcm_stream_unlock_irq(substream);
2075 _end:
2076 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2077}
2078
Takashi Iwai877211f2005-11-17 13:59:38 +01002079snd_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 -07002080{
Takashi Iwai877211f2005-11-17 13:59:38 +01002081 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002085 err = pcm_sanity_check(substream);
2086 if (err < 0)
2087 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002089 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2091 return -EINVAL;
2092 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2093}
2094
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002095EXPORT_SYMBOL(snd_pcm_lib_read);
2096
Takashi Iwai877211f2005-11-17 13:59:38 +01002097static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 unsigned int hwoff,
2099 unsigned long data, unsigned int off,
2100 snd_pcm_uframes_t frames)
2101{
Takashi Iwai877211f2005-11-17 13:59:38 +01002102 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 int err;
2104 void __user **bufs = (void __user **)data;
2105 int channels = runtime->channels;
2106 int c;
2107 if (substream->ops->copy) {
2108 for (c = 0; c < channels; ++c, ++bufs) {
2109 char __user *buf;
2110 if (*bufs == NULL)
2111 continue;
2112 buf = *bufs + samples_to_bytes(runtime, off);
2113 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2114 return err;
2115 }
2116 } else {
2117 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 for (c = 0; c < channels; ++c, ++bufs) {
2119 char *hwbuf;
2120 char __user *buf;
2121 if (*bufs == NULL)
2122 continue;
2123
2124 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2125 buf = *bufs + samples_to_bytes(runtime, off);
2126 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2127 return -EFAULT;
2128 }
2129 }
2130 return 0;
2131}
2132
Takashi Iwai877211f2005-11-17 13:59:38 +01002133snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 void __user **bufs,
2135 snd_pcm_uframes_t frames)
2136{
Takashi Iwai877211f2005-11-17 13:59:38 +01002137 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002139 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002141 err = pcm_sanity_check(substream);
2142 if (err < 0)
2143 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2146 return -EBADFD;
2147
Takashi Iwai0df63e42006-04-28 15:13:41 +02002148 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2150 return -EINVAL;
2151 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154EXPORT_SYMBOL(snd_pcm_lib_readv);