blob: 1990afb8a73546b113826a54c6919156c8425197 [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 Iwaic0070112009-06-08 15:58:48 +0200129static void pcm_debug_name(struct snd_pcm_substream *substream,
130 char *name, size_t len)
131{
132 snprintf(name, len, "pcmC%dD%d%c:%d",
133 substream->pcm->card->number,
134 substream->pcm->device,
135 substream->stream ? 'c' : 'p',
136 substream->number);
137}
138
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100139#define XRUN_DEBUG_BASIC (1<<0)
140#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
141#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
142#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
143#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
144#define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
145#define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
146
147#ifdef CONFIG_SND_PCM_XRUN_DEBUG
148
149#define xrun_debug(substream, mask) \
150 ((substream)->pstr->xrun_debug & (mask))
151
152#define dump_stack_on_xrun(substream) do { \
153 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
154 dump_stack(); \
155 } while (0)
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
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100172#define hw_ptr_error(substream, fmt, args...) \
173 do { \
174 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
175 if (printk_ratelimit()) { \
176 snd_printd("PCM: " fmt, ##args); \
177 } \
178 dump_stack_on_xrun(substream); \
179 } \
180 } while (0)
181
182#define XRUN_LOG_CNT 10
183
184struct hwptr_log_entry {
185 unsigned long jiffies;
186 snd_pcm_uframes_t pos;
187 snd_pcm_uframes_t period_size;
188 snd_pcm_uframes_t buffer_size;
189 snd_pcm_uframes_t old_hw_ptr;
190 snd_pcm_uframes_t hw_ptr_base;
191 snd_pcm_uframes_t hw_ptr_interrupt;
192};
193
194struct snd_pcm_hwptr_log {
195 unsigned int idx;
196 unsigned int hit: 1;
197 struct hwptr_log_entry entries[XRUN_LOG_CNT];
198};
199
200static void xrun_log(struct snd_pcm_substream *substream,
201 snd_pcm_uframes_t pos)
202{
203 struct snd_pcm_runtime *runtime = substream->runtime;
204 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
205 struct hwptr_log_entry *entry;
206
207 if (log == NULL) {
208 log = kzalloc(sizeof(*log), GFP_ATOMIC);
209 if (log == NULL)
210 return;
211 runtime->hwptr_log = log;
212 } else {
213 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
214 return;
215 }
216 entry = &log->entries[log->idx];
217 entry->jiffies = jiffies;
218 entry->pos = pos;
219 entry->period_size = runtime->period_size;
220 entry->buffer_size = runtime->buffer_size;;
221 entry->old_hw_ptr = runtime->status->hw_ptr;
222 entry->hw_ptr_base = runtime->hw_ptr_base;
223 entry->hw_ptr_interrupt = runtime->hw_ptr_interrupt;;
224 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
225}
226
227static void xrun_log_show(struct snd_pcm_substream *substream)
228{
229 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
230 struct hwptr_log_entry *entry;
231 char name[16];
232 unsigned int idx;
233 int cnt;
234
235 if (log == NULL)
236 return;
237 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
238 return;
239 pcm_debug_name(substream, name, sizeof(name));
240 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
241 entry = &log->entries[idx];
242 if (entry->period_size == 0)
243 break;
244 snd_printd("hwptr log: %s: j=%lu, pos=0x%lx/0x%lx/0x%lx, "
245 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
246 name, entry->jiffies, (unsigned long)entry->pos,
247 (unsigned long)entry->period_size,
248 (unsigned long)entry->buffer_size,
249 (unsigned long)entry->old_hw_ptr,
250 (unsigned long)entry->hw_ptr_base,
251 (unsigned long)entry->hw_ptr_interrupt);
252 idx++;
253 idx %= XRUN_LOG_CNT;
254 }
255 log->hit = 1;
256}
257
258#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
259
260#define xrun_debug(substream, mask) 0
261#define xrun(substream) do { } while (0)
262#define hw_ptr_error(substream, fmt, args...) do { } while (0)
263#define xrun_log(substream, pos) do { } while (0)
264#define xrun_log_show(substream) do { } while (0)
265
266#endif
267
Takashi Iwai98204642009-03-19 09:59:21 +0100268static snd_pcm_uframes_t
269snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
270 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 snd_pcm_uframes_t pos;
273
274 pos = substream->ops->pointer(substream);
275 if (pos == SNDRV_PCM_POS_XRUN)
276 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100278 if (printk_ratelimit()) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200279 char name[16];
280 pcm_debug_name(substream, name, sizeof(name));
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100281 xrun_log_show(substream);
Takashi Iwaic0070112009-06-08 15:58:48 +0200282 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
Takashi Iwai5f513e12009-03-19 10:01:47 +0100283 "buffer size = 0x%lx, period size = 0x%lx\n",
Takashi Iwaic0070112009-06-08 15:58:48 +0200284 name, pos, runtime->buffer_size,
Takashi Iwai5f513e12009-03-19 10:01:47 +0100285 runtime->period_size);
286 }
287 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 pos -= pos % runtime->min_align;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100290 if (xrun_debug(substream, XRUN_DEBUG_LOG))
291 xrun_log(substream, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return pos;
293}
294
Takashi Iwai98204642009-03-19 09:59:21 +0100295static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
296 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 snd_pcm_uframes_t avail;
299
300 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
301 avail = snd_pcm_playback_avail(runtime);
302 else
303 avail = snd_pcm_capture_avail(runtime);
304 if (avail > runtime->avail_max)
305 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200306 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
307 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200309 return -EPIPE;
310 }
311 } else {
312 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200314 return -EPIPE;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 if (avail >= runtime->control->avail_min)
318 wake_up(&runtime->sleep);
319 return 0;
320}
321
Takashi Iwai98204642009-03-19 09:59:21 +0100322static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Takashi Iwai877211f2005-11-17 13:59:38 +0100324 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 snd_pcm_uframes_t pos;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200326 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
327 snd_pcm_sframes_t hdelta, delta;
328 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200330 old_hw_ptr = runtime->status->hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
332 if (pos == SNDRV_PCM_POS_XRUN) {
333 xrun(substream);
334 return -EPIPE;
335 }
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100336 if (xrun_debug(substream, XRUN_DEBUG_PERIODUPDATE)) {
Takashi Iwaicedb8112009-07-23 11:04:13 +0200337 char name[16];
338 pcm_debug_name(substream, name, sizeof(name));
339 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
340 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200341 name, (unsigned int)pos,
342 (unsigned int)runtime->period_size,
343 (unsigned int)runtime->buffer_size,
344 (unsigned long)old_hw_ptr,
345 (unsigned long)runtime->hw_ptr_base,
346 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200347 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100348 hw_base = runtime->hw_ptr_base;
349 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100351 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100352 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100353 hw_ptr_interrupt -= runtime->boundary;
354 if (hw_base < runtime->boundary / 2)
355 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100356 delta = new_hw_ptr - hw_ptr_interrupt;
357 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100358 if (delta < 0) {
Takashi Iwai947ca212009-07-23 16:21:08 +0200359 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
Takashi Iwai79452f02009-07-22 12:51:51 +0200360 delta += runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100361 if (delta < 0) {
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100362 xrun_log_show(substream);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100363 hw_ptr_error(substream,
364 "Unexpected hw_pointer value "
365 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
366 substream->stream, (long)pos,
367 (long)hw_ptr_interrupt);
Takashi Iwai79452f02009-07-22 12:51:51 +0200368#if 1
369 /* simply skipping the hwptr update seems more
370 * robust in some cases, e.g. on VMware with
371 * inaccurate timer source
372 */
373 return 0; /* skip this update */
374#else
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100375 /* rebase to interrupt position */
376 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100377 /* align hw_base to buffer_size */
378 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100379 delta = 0;
Takashi Iwai79452f02009-07-22 12:51:51 +0200380#endif
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100381 } else {
382 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100383 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100384 hw_base = 0;
385 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200388
389 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100390 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200391 goto no_jiffies_check;
392
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200393 /* Skip the jiffies check for hardwares with BATCH flag.
394 * Such hardware usually just increases the position at each IRQ,
395 * thus it can't give any strange position.
396 */
397 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
398 goto no_jiffies_check;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200399 hdelta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200400 if (hdelta < runtime->delay)
401 goto no_jiffies_check;
402 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200403 jdelta = jiffies - runtime->hw_ptr_jiffies;
404 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
405 delta = jdelta /
406 (((runtime->period_size * HZ) / runtime->rate)
407 + HZ/100);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100408 xrun_log_show(substream);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200409 hw_ptr_error(substream,
410 "hw_ptr skipping! [Q] "
411 "(pos=%ld, delta=%ld, period=%ld, "
412 "jdelta=%lu/%lu/%lu)\n",
413 (long)pos, (long)hdelta,
414 (long)runtime->period_size, jdelta,
415 ((hdelta * HZ) / runtime->rate), delta);
416 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
417 runtime->period_size * delta;
418 if (hw_ptr_interrupt >= runtime->boundary)
419 hw_ptr_interrupt -= runtime->boundary;
420 /* rebase to interrupt position */
421 hw_base = new_hw_ptr = hw_ptr_interrupt;
422 /* align hw_base to buffer_size */
423 hw_base -= hw_base % runtime->buffer_size;
424 delta = 0;
425 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200426 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200427 if (delta > runtime->period_size + runtime->period_size / 2) {
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100428 xrun_log_show(substream);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100429 hw_ptr_error(substream,
430 "Lost interrupts? "
431 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
432 substream->stream, (long)delta,
433 (long)hw_ptr_interrupt);
434 /* rebase hw_ptr_interrupt */
435 hw_ptr_interrupt =
436 new_hw_ptr - new_hw_ptr % runtime->period_size;
437 }
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200438 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
441 runtime->silence_size > 0)
442 snd_pcm_playback_silence(substream, new_hw_ptr);
443
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200444 if (runtime->status->hw_ptr == new_hw_ptr)
445 return 0;
446
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100447 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200449 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200450 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
451 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return snd_pcm_update_hw_ptr_post(substream, runtime);
454}
455
456/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100457int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai877211f2005-11-17 13:59:38 +0100459 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100461 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 snd_pcm_sframes_t delta;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200463 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 old_hw_ptr = runtime->status->hw_ptr;
466 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
467 if (pos == SNDRV_PCM_POS_XRUN) {
468 xrun(substream);
469 return -EPIPE;
470 }
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100471 if (xrun_debug(substream, XRUN_DEBUG_HWPTRUPDATE)) {
Takashi Iwaicedb8112009-07-23 11:04:13 +0200472 char name[16];
473 pcm_debug_name(substream, name, sizeof(name));
474 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
475 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
Takashi Iwai89350642009-07-23 14:28:37 +0200476 name, (unsigned int)pos,
477 (unsigned int)runtime->period_size,
478 (unsigned int)runtime->buffer_size,
479 (unsigned long)old_hw_ptr,
480 (unsigned long)runtime->hw_ptr_base,
481 (unsigned long)runtime->hw_ptr_interrupt);
Takashi Iwaicedb8112009-07-23 11:04:13 +0200482 }
483
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100484 hw_base = runtime->hw_ptr_base;
485 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100487 delta = new_hw_ptr - old_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200488 jdelta = jiffies - runtime->hw_ptr_jiffies;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100489 if (delta < 0) {
490 delta += runtime->buffer_size;
491 if (delta < 0) {
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100492 xrun_log_show(substream);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100493 hw_ptr_error(substream,
494 "Unexpected hw_pointer value [2] "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200495 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100496 substream->stream, (long)pos,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200497 (long)old_hw_ptr, jdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100500 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100501 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100502 hw_base = 0;
503 new_hw_ptr = hw_base + pos;
504 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200505 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100506 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200507 goto no_jiffies_check;
508 if (delta < runtime->delay)
509 goto no_jiffies_check;
510 delta -= runtime->delay;
511 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100512 xrun_log_show(substream);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100513 hw_ptr_error(substream,
514 "hw_ptr skipping! "
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200515 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100516 (long)pos, (long)delta,
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200517 (long)runtime->period_size, jdelta,
518 ((delta * HZ) / runtime->rate));
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200521 no_jiffies_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
523 runtime->silence_size > 0)
524 snd_pcm_playback_silence(substream, new_hw_ptr);
525
Jaroslav Kyselad86bf922009-06-06 18:32:06 +0200526 if (runtime->status->hw_ptr == new_hw_ptr)
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200527 return 0;
528
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100529 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200531 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200532 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
533 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 return snd_pcm_update_hw_ptr_post(substream, runtime);
536}
537
538/**
539 * snd_pcm_set_ops - set the PCM operators
540 * @pcm: the pcm instance
541 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
542 * @ops: the operator table
543 *
544 * Sets the given PCM operators to the pcm instance.
545 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100546void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Takashi Iwai877211f2005-11-17 13:59:38 +0100548 struct snd_pcm_str *stream = &pcm->streams[direction];
549 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 for (substream = stream->substream; substream != NULL; substream = substream->next)
552 substream->ops = ops;
553}
554
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200555EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/**
558 * snd_pcm_sync - set the PCM sync id
559 * @substream: the pcm substream
560 *
561 * Sets the PCM sync identifier for the card.
562 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100563void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai877211f2005-11-17 13:59:38 +0100565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 runtime->sync.id32[0] = substream->pcm->card->number;
568 runtime->sync.id32[1] = -1;
569 runtime->sync.id32[2] = -1;
570 runtime->sync.id32[3] = -1;
571}
572
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200573EXPORT_SYMBOL(snd_pcm_set_sync);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
576 * Standard ioctl routine
577 */
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static inline unsigned int div32(unsigned int a, unsigned int b,
580 unsigned int *r)
581{
582 if (b == 0) {
583 *r = 0;
584 return UINT_MAX;
585 }
586 *r = a % b;
587 return a / b;
588}
589
590static inline unsigned int div_down(unsigned int a, unsigned int b)
591{
592 if (b == 0)
593 return UINT_MAX;
594 return a / b;
595}
596
597static inline unsigned int div_up(unsigned int a, unsigned int b)
598{
599 unsigned int r;
600 unsigned int q;
601 if (b == 0)
602 return UINT_MAX;
603 q = div32(a, b, &r);
604 if (r)
605 ++q;
606 return q;
607}
608
609static inline unsigned int mul(unsigned int a, unsigned int b)
610{
611 if (a == 0)
612 return 0;
613 if (div_down(UINT_MAX, a) < b)
614 return UINT_MAX;
615 return a * b;
616}
617
618static inline unsigned int muldiv32(unsigned int a, unsigned int b,
619 unsigned int c, unsigned int *r)
620{
621 u_int64_t n = (u_int64_t) a * b;
622 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200623 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *r = 0;
625 return UINT_MAX;
626 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200627 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (n >= UINT_MAX) {
629 *r = 0;
630 return UINT_MAX;
631 }
632 return n;
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/**
636 * snd_interval_refine - refine the interval value of configurator
637 * @i: the interval value to refine
638 * @v: the interval value to refer to
639 *
640 * Refines the interval value with the reference value.
641 * The interval is changed to the range satisfying both intervals.
642 * The interval status (min, max, integer, etc.) are evaluated.
643 *
644 * Returns non-zero if the value is changed, zero if not changed.
645 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100646int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200649 if (snd_BUG_ON(snd_interval_empty(i)))
650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (i->min < v->min) {
652 i->min = v->min;
653 i->openmin = v->openmin;
654 changed = 1;
655 } else if (i->min == v->min && !i->openmin && v->openmin) {
656 i->openmin = 1;
657 changed = 1;
658 }
659 if (i->max > v->max) {
660 i->max = v->max;
661 i->openmax = v->openmax;
662 changed = 1;
663 } else if (i->max == v->max && !i->openmax && v->openmax) {
664 i->openmax = 1;
665 changed = 1;
666 }
667 if (!i->integer && v->integer) {
668 i->integer = 1;
669 changed = 1;
670 }
671 if (i->integer) {
672 if (i->openmin) {
673 i->min++;
674 i->openmin = 0;
675 }
676 if (i->openmax) {
677 i->max--;
678 i->openmax = 0;
679 }
680 } else if (!i->openmin && !i->openmax && i->min == i->max)
681 i->integer = 1;
682 if (snd_interval_checkempty(i)) {
683 snd_interval_none(i);
684 return -EINVAL;
685 }
686 return changed;
687}
688
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200689EXPORT_SYMBOL(snd_interval_refine);
690
Takashi Iwai877211f2005-11-17 13:59:38 +0100691static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200693 if (snd_BUG_ON(snd_interval_empty(i)))
694 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (snd_interval_single(i))
696 return 0;
697 i->max = i->min;
698 i->openmax = i->openmin;
699 if (i->openmax)
700 i->max++;
701 return 1;
702}
703
Takashi Iwai877211f2005-11-17 13:59:38 +0100704static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200706 if (snd_BUG_ON(snd_interval_empty(i)))
707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (snd_interval_single(i))
709 return 0;
710 i->min = i->max;
711 i->openmin = i->openmax;
712 if (i->openmin)
713 i->min--;
714 return 1;
715}
716
Takashi Iwai877211f2005-11-17 13:59:38 +0100717void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 if (a->empty || b->empty) {
720 snd_interval_none(c);
721 return;
722 }
723 c->empty = 0;
724 c->min = mul(a->min, b->min);
725 c->openmin = (a->openmin || b->openmin);
726 c->max = mul(a->max, b->max);
727 c->openmax = (a->openmax || b->openmax);
728 c->integer = (a->integer && b->integer);
729}
730
731/**
732 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200733 * @a: dividend
734 * @b: divisor
735 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *
737 * c = a / b
738 *
739 * Returns non-zero if the value is changed, zero if not changed.
740 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100741void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 unsigned int r;
744 if (a->empty || b->empty) {
745 snd_interval_none(c);
746 return;
747 }
748 c->empty = 0;
749 c->min = div32(a->min, b->max, &r);
750 c->openmin = (r || a->openmin || b->openmax);
751 if (b->min > 0) {
752 c->max = div32(a->max, b->min, &r);
753 if (r) {
754 c->max++;
755 c->openmax = 1;
756 } else
757 c->openmax = (a->openmax || b->openmin);
758 } else {
759 c->max = UINT_MAX;
760 c->openmax = 0;
761 }
762 c->integer = 0;
763}
764
765/**
766 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200767 * @a: dividend 1
768 * @b: dividend 2
769 * @k: divisor (as integer)
770 * @c: result
771 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * c = a * b / k
773 *
774 * Returns non-zero if the value is changed, zero if not changed.
775 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100776void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
777 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 unsigned int r;
780 if (a->empty || b->empty) {
781 snd_interval_none(c);
782 return;
783 }
784 c->empty = 0;
785 c->min = muldiv32(a->min, b->min, k, &r);
786 c->openmin = (r || a->openmin || b->openmin);
787 c->max = muldiv32(a->max, b->max, k, &r);
788 if (r) {
789 c->max++;
790 c->openmax = 1;
791 } else
792 c->openmax = (a->openmax || b->openmax);
793 c->integer = 0;
794}
795
796/**
797 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200798 * @a: dividend 1
799 * @k: dividend 2 (as integer)
800 * @b: divisor
801 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * c = a * k / b
804 *
805 * Returns non-zero if the value is changed, zero if not changed.
806 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100807void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
808 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 unsigned int r;
811 if (a->empty || b->empty) {
812 snd_interval_none(c);
813 return;
814 }
815 c->empty = 0;
816 c->min = muldiv32(a->min, k, b->max, &r);
817 c->openmin = (r || a->openmin || b->openmax);
818 if (b->min > 0) {
819 c->max = muldiv32(a->max, k, b->min, &r);
820 if (r) {
821 c->max++;
822 c->openmax = 1;
823 } else
824 c->openmax = (a->openmax || b->openmin);
825 } else {
826 c->max = UINT_MAX;
827 c->openmax = 0;
828 }
829 c->integer = 0;
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/* ---- */
833
834
835/**
836 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200837 * @i: interval to refine
838 * @rats_count: number of ratnum_t
839 * @rats: ratnum_t array
840 * @nump: pointer to store the resultant numerator
841 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
843 * Returns non-zero if the value is changed, zero if not changed.
844 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100845int snd_interval_ratnum(struct snd_interval *i,
846 unsigned int rats_count, struct snd_ratnum *rats,
847 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 unsigned int best_num, best_diff, best_den;
850 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int err;
853
854 best_num = best_den = best_diff = 0;
855 for (k = 0; k < rats_count; ++k) {
856 unsigned int num = rats[k].num;
857 unsigned int den;
858 unsigned int q = i->min;
859 int diff;
860 if (q == 0)
861 q = 1;
862 den = div_down(num, q);
863 if (den < rats[k].den_min)
864 continue;
865 if (den > rats[k].den_max)
866 den = rats[k].den_max;
867 else {
868 unsigned int r;
869 r = (den - rats[k].den_min) % rats[k].den_step;
870 if (r != 0)
871 den -= r;
872 }
873 diff = num - q * den;
874 if (best_num == 0 ||
875 diff * best_den < best_diff * den) {
876 best_diff = diff;
877 best_den = den;
878 best_num = num;
879 }
880 }
881 if (best_den == 0) {
882 i->empty = 1;
883 return -EINVAL;
884 }
885 t.min = div_down(best_num, best_den);
886 t.openmin = !!(best_num % best_den);
887
888 best_num = best_den = best_diff = 0;
889 for (k = 0; k < rats_count; ++k) {
890 unsigned int num = rats[k].num;
891 unsigned int den;
892 unsigned int q = i->max;
893 int diff;
894 if (q == 0) {
895 i->empty = 1;
896 return -EINVAL;
897 }
898 den = div_up(num, q);
899 if (den > rats[k].den_max)
900 continue;
901 if (den < rats[k].den_min)
902 den = rats[k].den_min;
903 else {
904 unsigned int r;
905 r = (den - rats[k].den_min) % rats[k].den_step;
906 if (r != 0)
907 den += rats[k].den_step - r;
908 }
909 diff = q * den - num;
910 if (best_num == 0 ||
911 diff * best_den < best_diff * den) {
912 best_diff = diff;
913 best_den = den;
914 best_num = num;
915 }
916 }
917 if (best_den == 0) {
918 i->empty = 1;
919 return -EINVAL;
920 }
921 t.max = div_up(best_num, best_den);
922 t.openmax = !!(best_num % best_den);
923 t.integer = 0;
924 err = snd_interval_refine(i, &t);
925 if (err < 0)
926 return err;
927
928 if (snd_interval_single(i)) {
929 if (nump)
930 *nump = best_num;
931 if (denp)
932 *denp = best_den;
933 }
934 return err;
935}
936
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200937EXPORT_SYMBOL(snd_interval_ratnum);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/**
940 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200941 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100942 * @rats_count: number of struct ratden
943 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200944 * @nump: pointer to store the resultant numerator
945 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
947 * Returns non-zero if the value is changed, zero if not changed.
948 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100949static int snd_interval_ratden(struct snd_interval *i,
950 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned int *nump, unsigned int *denp)
952{
953 unsigned int best_num, best_diff, best_den;
954 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100955 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int err;
957
958 best_num = best_den = best_diff = 0;
959 for (k = 0; k < rats_count; ++k) {
960 unsigned int num;
961 unsigned int den = rats[k].den;
962 unsigned int q = i->min;
963 int diff;
964 num = mul(q, den);
965 if (num > rats[k].num_max)
966 continue;
967 if (num < rats[k].num_min)
968 num = rats[k].num_max;
969 else {
970 unsigned int r;
971 r = (num - rats[k].num_min) % rats[k].num_step;
972 if (r != 0)
973 num += rats[k].num_step - r;
974 }
975 diff = num - q * den;
976 if (best_num == 0 ||
977 diff * best_den < best_diff * den) {
978 best_diff = diff;
979 best_den = den;
980 best_num = num;
981 }
982 }
983 if (best_den == 0) {
984 i->empty = 1;
985 return -EINVAL;
986 }
987 t.min = div_down(best_num, best_den);
988 t.openmin = !!(best_num % best_den);
989
990 best_num = best_den = best_diff = 0;
991 for (k = 0; k < rats_count; ++k) {
992 unsigned int num;
993 unsigned int den = rats[k].den;
994 unsigned int q = i->max;
995 int diff;
996 num = mul(q, den);
997 if (num < rats[k].num_min)
998 continue;
999 if (num > rats[k].num_max)
1000 num = rats[k].num_max;
1001 else {
1002 unsigned int r;
1003 r = (num - rats[k].num_min) % rats[k].num_step;
1004 if (r != 0)
1005 num -= r;
1006 }
1007 diff = q * den - num;
1008 if (best_num == 0 ||
1009 diff * best_den < best_diff * den) {
1010 best_diff = diff;
1011 best_den = den;
1012 best_num = num;
1013 }
1014 }
1015 if (best_den == 0) {
1016 i->empty = 1;
1017 return -EINVAL;
1018 }
1019 t.max = div_up(best_num, best_den);
1020 t.openmax = !!(best_num % best_den);
1021 t.integer = 0;
1022 err = snd_interval_refine(i, &t);
1023 if (err < 0)
1024 return err;
1025
1026 if (snd_interval_single(i)) {
1027 if (nump)
1028 *nump = best_num;
1029 if (denp)
1030 *denp = best_den;
1031 }
1032 return err;
1033}
1034
1035/**
1036 * snd_interval_list - refine the interval value from the list
1037 * @i: the interval value to refine
1038 * @count: the number of elements in the list
1039 * @list: the value list
1040 * @mask: the bit-mask to evaluate
1041 *
1042 * Refines the interval value from the list.
1043 * When mask is non-zero, only the elements corresponding to bit 1 are
1044 * evaluated.
1045 *
1046 * Returns non-zero if the value is changed, zero if not changed.
1047 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001048int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001051 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001052
1053 if (!count) {
1054 i->empty = 1;
1055 return -EINVAL;
1056 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001057 snd_interval_any(&list_range);
1058 list_range.min = UINT_MAX;
1059 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 for (k = 0; k < count; k++) {
1061 if (mask && !(mask & (1 << k)))
1062 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001063 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001065 list_range.min = min(list_range.min, list[k]);
1066 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001068 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001071EXPORT_SYMBOL(snd_interval_list);
1072
Takashi Iwai877211f2005-11-17 13:59:38 +01001073static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 unsigned int n;
1076 int changed = 0;
1077 n = (i->min - min) % step;
1078 if (n != 0 || i->openmin) {
1079 i->min += step - n;
1080 changed = 1;
1081 }
1082 n = (i->max - min) % step;
1083 if (n != 0 || i->openmax) {
1084 i->max -= n;
1085 changed = 1;
1086 }
1087 if (snd_interval_checkempty(i)) {
1088 i->empty = 1;
1089 return -EINVAL;
1090 }
1091 return changed;
1092}
1093
1094/* Info constraints helpers */
1095
1096/**
1097 * snd_pcm_hw_rule_add - add the hw-constraint rule
1098 * @runtime: the pcm runtime instance
1099 * @cond: condition bits
1100 * @var: the variable to evaluate
1101 * @func: the evaluation function
1102 * @private: the private data pointer passed to function
1103 * @dep: the dependent variables
1104 *
1105 * Returns zero if successful, or a negative error code on failure.
1106 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001107int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int var,
1109 snd_pcm_hw_rule_func_t func, void *private,
1110 int dep, ...)
1111{
Takashi Iwai877211f2005-11-17 13:59:38 +01001112 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1113 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 unsigned int k;
1115 va_list args;
1116 va_start(args, dep);
1117 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001118 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 unsigned int new_rules = constrs->rules_all + 16;
1120 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1121 if (!new)
1122 return -ENOMEM;
1123 if (constrs->rules) {
1124 memcpy(new, constrs->rules,
1125 constrs->rules_num * sizeof(*c));
1126 kfree(constrs->rules);
1127 }
1128 constrs->rules = new;
1129 constrs->rules_all = new_rules;
1130 }
1131 c = &constrs->rules[constrs->rules_num];
1132 c->cond = cond;
1133 c->func = func;
1134 c->var = var;
1135 c->private = private;
1136 k = 0;
1137 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001138 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1139 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 c->deps[k++] = dep;
1141 if (dep < 0)
1142 break;
1143 dep = va_arg(args, int);
1144 }
1145 constrs->rules_num++;
1146 va_end(args);
1147 return 0;
1148}
1149
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001150EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001153 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001154 * @runtime: PCM runtime instance
1155 * @var: hw_params variable to apply the mask
1156 * @mask: the bitmap mask
1157 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001158 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001160int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 u_int32_t mask)
1162{
Takashi Iwai877211f2005-11-17 13:59:38 +01001163 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1164 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *maskp->bits &= mask;
1166 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1167 if (*maskp->bits == 0)
1168 return -EINVAL;
1169 return 0;
1170}
1171
1172/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001173 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001174 * @runtime: PCM runtime instance
1175 * @var: hw_params variable to apply the mask
1176 * @mask: the 64bit bitmap mask
1177 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001178 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001180int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 u_int64_t mask)
1182{
Takashi Iwai877211f2005-11-17 13:59:38 +01001183 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1184 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 maskp->bits[0] &= (u_int32_t)mask;
1186 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1187 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1188 if (! maskp->bits[0] && ! maskp->bits[1])
1189 return -EINVAL;
1190 return 0;
1191}
1192
1193/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001194 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001195 * @runtime: PCM runtime instance
1196 * @var: hw_params variable to apply the integer constraint
1197 *
1198 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001200int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwai877211f2005-11-17 13:59:38 +01001202 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return snd_interval_setinteger(constrs_interval(constrs, var));
1204}
1205
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001206EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001209 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001210 * @runtime: PCM runtime instance
1211 * @var: hw_params variable to apply the range
1212 * @min: the minimal value
1213 * @max: the maximal value
1214 *
1215 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001217int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 unsigned int min, unsigned int max)
1219{
Takashi Iwai877211f2005-11-17 13:59:38 +01001220 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1221 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 t.min = min;
1223 t.max = max;
1224 t.openmin = t.openmax = 0;
1225 t.integer = 0;
1226 return snd_interval_refine(constrs_interval(constrs, var), &t);
1227}
1228
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001229EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1230
Takashi Iwai877211f2005-11-17 13:59:38 +01001231static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1232 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwai877211f2005-11-17 13:59:38 +01001234 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1236}
1237
1238
1239/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001240 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001241 * @runtime: PCM runtime instance
1242 * @cond: condition bits
1243 * @var: hw_params variable to apply the list constraint
1244 * @l: list
1245 *
1246 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001248int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned int cond,
1250 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001251 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 return snd_pcm_hw_rule_add(runtime, cond, var,
1254 snd_pcm_hw_rule_list, l,
1255 var, -1);
1256}
1257
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001258EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1259
Takashi Iwai877211f2005-11-17 13:59:38 +01001260static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1261 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai877211f2005-11-17 13:59:38 +01001263 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned int num = 0, den = 0;
1265 int err;
1266 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1267 r->nrats, r->rats, &num, &den);
1268 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1269 params->rate_num = num;
1270 params->rate_den = den;
1271 }
1272 return err;
1273}
1274
1275/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001276 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001277 * @runtime: PCM runtime instance
1278 * @cond: condition bits
1279 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001280 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001282int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 unsigned int cond,
1284 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001285 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 return snd_pcm_hw_rule_add(runtime, cond, var,
1288 snd_pcm_hw_rule_ratnums, r,
1289 var, -1);
1290}
1291
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001292EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1293
Takashi Iwai877211f2005-11-17 13:59:38 +01001294static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1295 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Takashi Iwai877211f2005-11-17 13:59:38 +01001297 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 unsigned int num = 0, den = 0;
1299 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1300 r->nrats, r->rats, &num, &den);
1301 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1302 params->rate_num = num;
1303 params->rate_den = den;
1304 }
1305 return err;
1306}
1307
1308/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001309 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001310 * @runtime: PCM runtime instance
1311 * @cond: condition bits
1312 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001313 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001315int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 unsigned int cond,
1317 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001318 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 return snd_pcm_hw_rule_add(runtime, cond, var,
1321 snd_pcm_hw_rule_ratdens, r,
1322 var, -1);
1323}
1324
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001325EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1326
Takashi Iwai877211f2005-11-17 13:59:38 +01001327static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1328 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 unsigned int l = (unsigned long) rule->private;
1331 int width = l & 0xffff;
1332 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001333 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (snd_interval_single(i) && snd_interval_value(i) == width)
1335 params->msbits = msbits;
1336 return 0;
1337}
1338
1339/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001340 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001341 * @runtime: PCM runtime instance
1342 * @cond: condition bits
1343 * @width: sample bits width
1344 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001346int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 unsigned int cond,
1348 unsigned int width,
1349 unsigned int msbits)
1350{
1351 unsigned long l = (msbits << 16) | width;
1352 return snd_pcm_hw_rule_add(runtime, cond, -1,
1353 snd_pcm_hw_rule_msbits,
1354 (void*) l,
1355 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1356}
1357
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001358EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1359
Takashi Iwai877211f2005-11-17 13:59:38 +01001360static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1361 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 unsigned long step = (unsigned long) rule->private;
1364 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1365}
1366
1367/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001368 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001369 * @runtime: PCM runtime instance
1370 * @cond: condition bits
1371 * @var: hw_params variable to apply the step constraint
1372 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001374int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 unsigned int cond,
1376 snd_pcm_hw_param_t var,
1377 unsigned long step)
1378{
1379 return snd_pcm_hw_rule_add(runtime, cond, var,
1380 snd_pcm_hw_rule_step, (void *) step,
1381 var, -1);
1382}
1383
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001384EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1385
Takashi Iwai877211f2005-11-17 13:59:38 +01001386static 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 -07001387{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001388 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1390 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1391 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1392 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1393 };
1394 return snd_interval_list(hw_param_interval(params, rule->var),
1395 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1396}
1397
1398/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001399 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001400 * @runtime: PCM runtime instance
1401 * @cond: condition bits
1402 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001404int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 unsigned int cond,
1406 snd_pcm_hw_param_t var)
1407{
1408 return snd_pcm_hw_rule_add(runtime, cond, var,
1409 snd_pcm_hw_rule_pow2, NULL,
1410 var, -1);
1411}
1412
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001413EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1414
Takashi Iwai877211f2005-11-17 13:59:38 +01001415static void _snd_pcm_hw_param_any(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 if (hw_is_mask(var)) {
1419 snd_mask_any(hw_param_mask(params, var));
1420 params->cmask |= 1 << var;
1421 params->rmask |= 1 << var;
1422 return;
1423 }
1424 if (hw_is_interval(var)) {
1425 snd_interval_any(hw_param_interval(params, var));
1426 params->cmask |= 1 << var;
1427 params->rmask |= 1 << var;
1428 return;
1429 }
1430 snd_BUG();
1431}
1432
Takashi Iwai877211f2005-11-17 13:59:38 +01001433void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 unsigned int k;
1436 memset(params, 0, sizeof(*params));
1437 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1438 _snd_pcm_hw_param_any(params, k);
1439 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1440 _snd_pcm_hw_param_any(params, k);
1441 params->info = ~0U;
1442}
1443
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001444EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001447 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001448 * @params: the hw_params instance
1449 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001450 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001452 * Return the value for field @var if it's fixed in configuration space
1453 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001455int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1456 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001459 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!snd_mask_single(mask))
1461 return -EINVAL;
1462 if (dir)
1463 *dir = 0;
1464 return snd_mask_value(mask);
1465 }
1466 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001467 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!snd_interval_single(i))
1469 return -EINVAL;
1470 if (dir)
1471 *dir = i->openmin;
1472 return snd_interval_value(i);
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EINVAL;
1475}
1476
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001477EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Takashi Iwai877211f2005-11-17 13:59:38 +01001479void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 snd_pcm_hw_param_t var)
1481{
1482 if (hw_is_mask(var)) {
1483 snd_mask_none(hw_param_mask(params, var));
1484 params->cmask |= 1 << var;
1485 params->rmask |= 1 << var;
1486 } else if (hw_is_interval(var)) {
1487 snd_interval_none(hw_param_interval(params, var));
1488 params->cmask |= 1 << var;
1489 params->rmask |= 1 << var;
1490 } else {
1491 snd_BUG();
1492 }
1493}
1494
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001495EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Takashi Iwai877211f2005-11-17 13:59:38 +01001497static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001498 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 int changed;
1501 if (hw_is_mask(var))
1502 changed = snd_mask_refine_first(hw_param_mask(params, var));
1503 else if (hw_is_interval(var))
1504 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001505 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (changed) {
1508 params->cmask |= 1 << var;
1509 params->rmask |= 1 << var;
1510 }
1511 return changed;
1512}
1513
1514
1515/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001516 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001517 * @pcm: PCM instance
1518 * @params: the hw_params instance
1519 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001520 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001522 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * values > minimum. Reduce configuration space accordingly.
1524 * Return the minimum.
1525 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001526int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1527 struct snd_pcm_hw_params *params,
1528 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int changed = _snd_pcm_hw_param_first(params, var);
1531 if (changed < 0)
1532 return changed;
1533 if (params->rmask) {
1534 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001535 if (snd_BUG_ON(err < 0))
1536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 return snd_pcm_hw_param_value(params, var, dir);
1539}
1540
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001541EXPORT_SYMBOL(snd_pcm_hw_param_first);
1542
Takashi Iwai877211f2005-11-17 13:59:38 +01001543static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001544 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 int changed;
1547 if (hw_is_mask(var))
1548 changed = snd_mask_refine_last(hw_param_mask(params, var));
1549 else if (hw_is_interval(var))
1550 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001551 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (changed) {
1554 params->cmask |= 1 << var;
1555 params->rmask |= 1 << var;
1556 }
1557 return changed;
1558}
1559
1560
1561/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001562 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001563 * @pcm: PCM instance
1564 * @params: the hw_params instance
1565 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001566 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001568 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * values < maximum. Reduce configuration space accordingly.
1570 * Return the maximum.
1571 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001572int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1573 struct snd_pcm_hw_params *params,
1574 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 int changed = _snd_pcm_hw_param_last(params, var);
1577 if (changed < 0)
1578 return changed;
1579 if (params->rmask) {
1580 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001581 if (snd_BUG_ON(err < 0))
1582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 return snd_pcm_hw_param_value(params, var, dir);
1585}
1586
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001587EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001590 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001591 * @pcm: PCM instance
1592 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001594 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * The configuration chosen is that obtained fixing in this order:
1596 * first access, first format, first subformat, min channels,
1597 * min rate, min period time, max buffer size, min tick time
1598 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001599int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1600 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001602 static int vars[] = {
1603 SNDRV_PCM_HW_PARAM_ACCESS,
1604 SNDRV_PCM_HW_PARAM_FORMAT,
1605 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1606 SNDRV_PCM_HW_PARAM_CHANNELS,
1607 SNDRV_PCM_HW_PARAM_RATE,
1608 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1609 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1610 SNDRV_PCM_HW_PARAM_TICK_TIME,
1611 -1
1612 };
1613 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001615 for (v = vars; *v != -1; v++) {
1616 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1617 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1618 else
1619 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001620 if (snd_BUG_ON(err < 0))
1621 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
1625
Takashi Iwai877211f2005-11-17 13:59:38 +01001626static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 void *arg)
1628{
Takashi Iwai877211f2005-11-17 13:59:38 +01001629 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 unsigned long flags;
1631 snd_pcm_stream_lock_irqsave(substream, flags);
1632 if (snd_pcm_running(substream) &&
1633 snd_pcm_update_hw_ptr(substream) >= 0)
1634 runtime->status->hw_ptr %= runtime->buffer_size;
1635 else
1636 runtime->status->hw_ptr = 0;
1637 snd_pcm_stream_unlock_irqrestore(substream, flags);
1638 return 0;
1639}
1640
Takashi Iwai877211f2005-11-17 13:59:38 +01001641static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 void *arg)
1643{
Takashi Iwai877211f2005-11-17 13:59:38 +01001644 struct snd_pcm_channel_info *info = arg;
1645 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int width;
1647 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1648 info->offset = -1;
1649 return 0;
1650 }
1651 width = snd_pcm_format_physical_width(runtime->format);
1652 if (width < 0)
1653 return width;
1654 info->offset = 0;
1655 switch (runtime->access) {
1656 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1657 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1658 info->first = info->channel * width;
1659 info->step = runtime->channels * width;
1660 break;
1661 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1662 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1663 {
1664 size_t size = runtime->dma_bytes / runtime->channels;
1665 info->first = info->channel * size * 8;
1666 info->step = width;
1667 break;
1668 }
1669 default:
1670 snd_BUG();
1671 break;
1672 }
1673 return 0;
1674}
1675
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001676static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1677 void *arg)
1678{
1679 struct snd_pcm_hw_params *params = arg;
1680 snd_pcm_format_t format;
1681 int channels, width;
1682
1683 params->fifo_size = substream->runtime->hw.fifo_size;
1684 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1685 format = params_format(params);
1686 channels = params_channels(params);
1687 width = snd_pcm_format_physical_width(format);
1688 params->fifo_size /= width * channels;
1689 }
1690 return 0;
1691}
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693/**
1694 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1695 * @substream: the pcm substream instance
1696 * @cmd: ioctl command
1697 * @arg: ioctl argument
1698 *
1699 * Processes the generic ioctl commands for PCM.
1700 * Can be passed as the ioctl callback for PCM ops.
1701 *
1702 * Returns zero if successful, or a negative error code on failure.
1703 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001704int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 unsigned int cmd, void *arg)
1706{
1707 switch (cmd) {
1708 case SNDRV_PCM_IOCTL1_INFO:
1709 return 0;
1710 case SNDRV_PCM_IOCTL1_RESET:
1711 return snd_pcm_lib_ioctl_reset(substream, arg);
1712 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1713 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001714 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1715 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 return -ENXIO;
1718}
1719
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001720EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/**
1723 * snd_pcm_period_elapsed - update the pcm status for the next period
1724 * @substream: the pcm substream instance
1725 *
1726 * This function is called from the interrupt handler when the
1727 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001728 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
1730 * Even if more than one periods have elapsed since the last call, you
1731 * have to call this only once.
1732 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001733void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Takashi Iwai877211f2005-11-17 13:59:38 +01001735 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 unsigned long flags;
1737
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001738 if (PCM_RUNTIME_CHECK(substream))
1739 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (runtime->transfer_ack_begin)
1743 runtime->transfer_ack_begin(substream);
1744
1745 snd_pcm_stream_lock_irqsave(substream, flags);
1746 if (!snd_pcm_running(substream) ||
1747 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1748 goto _end;
1749
1750 if (substream->timer_running)
1751 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 _end:
1753 snd_pcm_stream_unlock_irqrestore(substream, flags);
1754 if (runtime->transfer_ack_end)
1755 runtime->transfer_ack_end(substream);
1756 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1757}
1758
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001759EXPORT_SYMBOL(snd_pcm_period_elapsed);
1760
Takashi Iwai13075512008-01-08 18:08:14 +01001761/*
1762 * Wait until avail_min data becomes available
1763 * Returns a negative error code if any error occurs during operation.
1764 * The available space is stored on availp. When err = 0 and avail = 0
1765 * on the capture stream, it indicates the stream is in DRAINING state.
1766 */
1767static int wait_for_avail_min(struct snd_pcm_substream *substream,
1768 snd_pcm_uframes_t *availp)
1769{
1770 struct snd_pcm_runtime *runtime = substream->runtime;
1771 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1772 wait_queue_t wait;
1773 int err = 0;
1774 snd_pcm_uframes_t avail = 0;
1775 long tout;
1776
1777 init_waitqueue_entry(&wait, current);
1778 add_wait_queue(&runtime->sleep, &wait);
1779 for (;;) {
1780 if (signal_pending(current)) {
1781 err = -ERESTARTSYS;
1782 break;
1783 }
1784 set_current_state(TASK_INTERRUPTIBLE);
1785 snd_pcm_stream_unlock_irq(substream);
1786 tout = schedule_timeout(msecs_to_jiffies(10000));
1787 snd_pcm_stream_lock_irq(substream);
1788 switch (runtime->status->state) {
1789 case SNDRV_PCM_STATE_SUSPENDED:
1790 err = -ESTRPIPE;
1791 goto _endloop;
1792 case SNDRV_PCM_STATE_XRUN:
1793 err = -EPIPE;
1794 goto _endloop;
1795 case SNDRV_PCM_STATE_DRAINING:
1796 if (is_playback)
1797 err = -EPIPE;
1798 else
1799 avail = 0; /* indicate draining */
1800 goto _endloop;
1801 case SNDRV_PCM_STATE_OPEN:
1802 case SNDRV_PCM_STATE_SETUP:
1803 case SNDRV_PCM_STATE_DISCONNECTED:
1804 err = -EBADFD;
1805 goto _endloop;
1806 }
1807 if (!tout) {
1808 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1809 is_playback ? "playback" : "capture");
1810 err = -EIO;
1811 break;
1812 }
1813 if (is_playback)
1814 avail = snd_pcm_playback_avail(runtime);
1815 else
1816 avail = snd_pcm_capture_avail(runtime);
1817 if (avail >= runtime->control->avail_min)
1818 break;
1819 }
1820 _endloop:
1821 remove_wait_queue(&runtime->sleep, &wait);
1822 *availp = avail;
1823 return err;
1824}
1825
Takashi Iwai877211f2005-11-17 13:59:38 +01001826static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 unsigned int hwoff,
1828 unsigned long data, unsigned int off,
1829 snd_pcm_uframes_t frames)
1830{
Takashi Iwai877211f2005-11-17 13:59:38 +01001831 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 int err;
1833 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1834 if (substream->ops->copy) {
1835 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1836 return err;
1837 } else {
1838 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1840 return -EFAULT;
1841 }
1842 return 0;
1843}
1844
Takashi Iwai877211f2005-11-17 13:59:38 +01001845typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 unsigned long data, unsigned int off,
1847 snd_pcm_uframes_t size);
1848
Takashi Iwai877211f2005-11-17 13:59:38 +01001849static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 unsigned long data,
1851 snd_pcm_uframes_t size,
1852 int nonblock,
1853 transfer_f transfer)
1854{
Takashi Iwai877211f2005-11-17 13:59:38 +01001855 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 snd_pcm_uframes_t xfer = 0;
1857 snd_pcm_uframes_t offset = 0;
1858 int err = 0;
1859
1860 if (size == 0)
1861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 snd_pcm_stream_lock_irq(substream);
1864 switch (runtime->status->state) {
1865 case SNDRV_PCM_STATE_PREPARED:
1866 case SNDRV_PCM_STATE_RUNNING:
1867 case SNDRV_PCM_STATE_PAUSED:
1868 break;
1869 case SNDRV_PCM_STATE_XRUN:
1870 err = -EPIPE;
1871 goto _end_unlock;
1872 case SNDRV_PCM_STATE_SUSPENDED:
1873 err = -ESTRPIPE;
1874 goto _end_unlock;
1875 default:
1876 err = -EBADFD;
1877 goto _end_unlock;
1878 }
1879
1880 while (size > 0) {
1881 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1882 snd_pcm_uframes_t avail;
1883 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001884 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 snd_pcm_update_hw_ptr(substream);
1886 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001887 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (nonblock) {
1889 err = -EAGAIN;
1890 goto _end_unlock;
1891 }
Takashi Iwai13075512008-01-08 18:08:14 +01001892 err = wait_for_avail_min(substream, &avail);
1893 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 frames = size > avail ? avail : size;
1897 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1898 if (frames > cont)
1899 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001900 if (snd_BUG_ON(!frames)) {
1901 snd_pcm_stream_unlock_irq(substream);
1902 return -EINVAL;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 appl_ptr = runtime->control->appl_ptr;
1905 appl_ofs = appl_ptr % runtime->buffer_size;
1906 snd_pcm_stream_unlock_irq(substream);
1907 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1908 goto _end;
1909 snd_pcm_stream_lock_irq(substream);
1910 switch (runtime->status->state) {
1911 case SNDRV_PCM_STATE_XRUN:
1912 err = -EPIPE;
1913 goto _end_unlock;
1914 case SNDRV_PCM_STATE_SUSPENDED:
1915 err = -ESTRPIPE;
1916 goto _end_unlock;
1917 default:
1918 break;
1919 }
1920 appl_ptr += frames;
1921 if (appl_ptr >= runtime->boundary)
1922 appl_ptr -= runtime->boundary;
1923 runtime->control->appl_ptr = appl_ptr;
1924 if (substream->ops->ack)
1925 substream->ops->ack(substream);
1926
1927 offset += frames;
1928 size -= frames;
1929 xfer += frames;
1930 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1931 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1932 err = snd_pcm_start(substream);
1933 if (err < 0)
1934 goto _end_unlock;
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 _end_unlock:
1938 snd_pcm_stream_unlock_irq(substream);
1939 _end:
1940 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1941}
1942
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001943/* sanity-check for read/write methods */
1944static int pcm_sanity_check(struct snd_pcm_substream *substream)
1945{
1946 struct snd_pcm_runtime *runtime;
1947 if (PCM_RUNTIME_CHECK(substream))
1948 return -ENXIO;
1949 runtime = substream->runtime;
1950 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1951 return -EINVAL;
1952 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1953 return -EBADFD;
1954 return 0;
1955}
1956
Takashi Iwai877211f2005-11-17 13:59:38 +01001957snd_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 -07001958{
Takashi Iwai877211f2005-11-17 13:59:38 +01001959 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001961 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001963 err = pcm_sanity_check(substream);
1964 if (err < 0)
1965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001967 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1970 runtime->channels > 1)
1971 return -EINVAL;
1972 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1973 snd_pcm_lib_write_transfer);
1974}
1975
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001976EXPORT_SYMBOL(snd_pcm_lib_write);
1977
Takashi Iwai877211f2005-11-17 13:59:38 +01001978static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 unsigned int hwoff,
1980 unsigned long data, unsigned int off,
1981 snd_pcm_uframes_t frames)
1982{
Takashi Iwai877211f2005-11-17 13:59:38 +01001983 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 int err;
1985 void __user **bufs = (void __user **)data;
1986 int channels = runtime->channels;
1987 int c;
1988 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001989 if (snd_BUG_ON(!substream->ops->silence))
1990 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 for (c = 0; c < channels; ++c, ++bufs) {
1992 if (*bufs == NULL) {
1993 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1994 return err;
1995 } else {
1996 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1997 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1998 return err;
1999 }
2000 }
2001 } else {
2002 /* default transfer behaviour */
2003 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 for (c = 0; c < channels; ++c, ++bufs) {
2005 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2006 if (*bufs == NULL) {
2007 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2008 } else {
2009 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2010 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2011 return -EFAULT;
2012 }
2013 }
2014 }
2015 return 0;
2016}
2017
Takashi Iwai877211f2005-11-17 13:59:38 +01002018snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 void __user **bufs,
2020 snd_pcm_uframes_t frames)
2021{
Takashi Iwai877211f2005-11-17 13:59:38 +01002022 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002026 err = pcm_sanity_check(substream);
2027 if (err < 0)
2028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002030 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2033 return -EINVAL;
2034 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2035 nonblock, snd_pcm_lib_writev_transfer);
2036}
2037
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002038EXPORT_SYMBOL(snd_pcm_lib_writev);
2039
Takashi Iwai877211f2005-11-17 13:59:38 +01002040static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 unsigned int hwoff,
2042 unsigned long data, unsigned int off,
2043 snd_pcm_uframes_t frames)
2044{
Takashi Iwai877211f2005-11-17 13:59:38 +01002045 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int err;
2047 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2048 if (substream->ops->copy) {
2049 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2050 return err;
2051 } else {
2052 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2054 return -EFAULT;
2055 }
2056 return 0;
2057}
2058
Takashi Iwai877211f2005-11-17 13:59:38 +01002059static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 unsigned long data,
2061 snd_pcm_uframes_t size,
2062 int nonblock,
2063 transfer_f transfer)
2064{
Takashi Iwai877211f2005-11-17 13:59:38 +01002065 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 snd_pcm_uframes_t xfer = 0;
2067 snd_pcm_uframes_t offset = 0;
2068 int err = 0;
2069
2070 if (size == 0)
2071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 snd_pcm_stream_lock_irq(substream);
2074 switch (runtime->status->state) {
2075 case SNDRV_PCM_STATE_PREPARED:
2076 if (size >= runtime->start_threshold) {
2077 err = snd_pcm_start(substream);
2078 if (err < 0)
2079 goto _end_unlock;
2080 }
2081 break;
2082 case SNDRV_PCM_STATE_DRAINING:
2083 case SNDRV_PCM_STATE_RUNNING:
2084 case SNDRV_PCM_STATE_PAUSED:
2085 break;
2086 case SNDRV_PCM_STATE_XRUN:
2087 err = -EPIPE;
2088 goto _end_unlock;
2089 case SNDRV_PCM_STATE_SUSPENDED:
2090 err = -ESTRPIPE;
2091 goto _end_unlock;
2092 default:
2093 err = -EBADFD;
2094 goto _end_unlock;
2095 }
2096
2097 while (size > 0) {
2098 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2099 snd_pcm_uframes_t avail;
2100 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002101 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002104 if (!avail) {
2105 if (runtime->status->state ==
2106 SNDRV_PCM_STATE_DRAINING) {
2107 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 goto _end_unlock;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (nonblock) {
2111 err = -EAGAIN;
2112 goto _end_unlock;
2113 }
Takashi Iwai13075512008-01-08 18:08:14 +01002114 err = wait_for_avail_min(substream, &avail);
2115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002117 if (!avail)
2118 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 frames = size > avail ? avail : size;
2121 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2122 if (frames > cont)
2123 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002124 if (snd_BUG_ON(!frames)) {
2125 snd_pcm_stream_unlock_irq(substream);
2126 return -EINVAL;
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 appl_ptr = runtime->control->appl_ptr;
2129 appl_ofs = appl_ptr % runtime->buffer_size;
2130 snd_pcm_stream_unlock_irq(substream);
2131 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2132 goto _end;
2133 snd_pcm_stream_lock_irq(substream);
2134 switch (runtime->status->state) {
2135 case SNDRV_PCM_STATE_XRUN:
2136 err = -EPIPE;
2137 goto _end_unlock;
2138 case SNDRV_PCM_STATE_SUSPENDED:
2139 err = -ESTRPIPE;
2140 goto _end_unlock;
2141 default:
2142 break;
2143 }
2144 appl_ptr += frames;
2145 if (appl_ptr >= runtime->boundary)
2146 appl_ptr -= runtime->boundary;
2147 runtime->control->appl_ptr = appl_ptr;
2148 if (substream->ops->ack)
2149 substream->ops->ack(substream);
2150
2151 offset += frames;
2152 size -= frames;
2153 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 _end_unlock:
2156 snd_pcm_stream_unlock_irq(substream);
2157 _end:
2158 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2159}
2160
Takashi Iwai877211f2005-11-17 13:59:38 +01002161snd_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 -07002162{
Takashi Iwai877211f2005-11-17 13:59:38 +01002163 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002165 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002167 err = pcm_sanity_check(substream);
2168 if (err < 0)
2169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002171 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2173 return -EINVAL;
2174 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2175}
2176
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002177EXPORT_SYMBOL(snd_pcm_lib_read);
2178
Takashi Iwai877211f2005-11-17 13:59:38 +01002179static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 unsigned int hwoff,
2181 unsigned long data, unsigned int off,
2182 snd_pcm_uframes_t frames)
2183{
Takashi Iwai877211f2005-11-17 13:59:38 +01002184 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int err;
2186 void __user **bufs = (void __user **)data;
2187 int channels = runtime->channels;
2188 int c;
2189 if (substream->ops->copy) {
2190 for (c = 0; c < channels; ++c, ++bufs) {
2191 char __user *buf;
2192 if (*bufs == NULL)
2193 continue;
2194 buf = *bufs + samples_to_bytes(runtime, off);
2195 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2196 return err;
2197 }
2198 } else {
2199 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 for (c = 0; c < channels; ++c, ++bufs) {
2201 char *hwbuf;
2202 char __user *buf;
2203 if (*bufs == NULL)
2204 continue;
2205
2206 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2207 buf = *bufs + samples_to_bytes(runtime, off);
2208 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2209 return -EFAULT;
2210 }
2211 }
2212 return 0;
2213}
2214
Takashi Iwai877211f2005-11-17 13:59:38 +01002215snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 void __user **bufs,
2217 snd_pcm_uframes_t frames)
2218{
Takashi Iwai877211f2005-11-17 13:59:38 +01002219 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002221 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002223 err = pcm_sanity_check(substream);
2224 if (err < 0)
2225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2228 return -EBADFD;
2229
Takashi Iwai0df63e42006-04-28 15:13:41 +02002230 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2232 return -EINVAL;
2233 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236EXPORT_SYMBOL(snd_pcm_lib_readv);