blob: a63226232ef4e60988ae129f2c65080328055c8a [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)) { \
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100175 xrun_log_show(substream); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100176 if (printk_ratelimit()) { \
177 snd_printd("PCM: " fmt, ##args); \
178 } \
179 dump_stack_on_xrun(substream); \
180 } \
181 } while (0)
182
183#define XRUN_LOG_CNT 10
184
185struct hwptr_log_entry {
186 unsigned long jiffies;
187 snd_pcm_uframes_t pos;
188 snd_pcm_uframes_t period_size;
189 snd_pcm_uframes_t buffer_size;
190 snd_pcm_uframes_t old_hw_ptr;
191 snd_pcm_uframes_t hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100192};
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;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100223 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
224}
225
226static void xrun_log_show(struct snd_pcm_substream *substream)
227{
228 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
229 struct hwptr_log_entry *entry;
230 char name[16];
231 unsigned int idx;
232 int cnt;
233
234 if (log == NULL)
235 return;
236 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
237 return;
238 pcm_debug_name(substream, name, sizeof(name));
239 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
240 entry = &log->entries[idx];
241 if (entry->period_size == 0)
242 break;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100243 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
244 "hwptr=%ld/%ld\n",
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100245 name, entry->jiffies, (unsigned long)entry->pos,
246 (unsigned long)entry->period_size,
247 (unsigned long)entry->buffer_size,
248 (unsigned long)entry->old_hw_ptr,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100249 (unsigned long)entry->hw_ptr_base);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100250 idx++;
251 idx %= XRUN_LOG_CNT;
252 }
253 log->hit = 1;
254}
255
256#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
257
258#define xrun_debug(substream, mask) 0
259#define xrun(substream) do { } while (0)
260#define hw_ptr_error(substream, fmt, args...) do { } while (0)
261#define xrun_log(substream, pos) do { } while (0)
262#define xrun_log_show(substream) do { } while (0)
263
264#endif
265
Jaroslav Kysela12509322010-01-07 15:36:31 +0100266int snd_pcm_update_state(struct snd_pcm_substream *substream,
267 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 snd_pcm_uframes_t avail;
270
271 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
272 avail = snd_pcm_playback_avail(runtime);
273 else
274 avail = snd_pcm_capture_avail(runtime);
275 if (avail > runtime->avail_max)
276 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200277 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
278 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200280 return -EPIPE;
281 }
282 } else {
283 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200285 return -EPIPE;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100288 if (!runtime->nowake && avail >= runtime->control->avail_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 wake_up(&runtime->sleep);
290 return 0;
291}
292
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100293static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
294 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Takashi Iwai877211f2005-11-17 13:59:38 +0100296 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100298 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200299 snd_pcm_sframes_t hdelta, delta;
300 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200302 old_hw_ptr = runtime->status->hw_ptr;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100303 pos = substream->ops->pointer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (pos == SNDRV_PCM_POS_XRUN) {
305 xrun(substream);
306 return -EPIPE;
307 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100308 if (pos >= runtime->buffer_size) {
309 if (printk_ratelimit()) {
310 char name[16];
311 pcm_debug_name(substream, name, sizeof(name));
312 xrun_log_show(substream);
313 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
314 "buffer size = %ld, period size = %ld\n",
315 name, pos, runtime->buffer_size,
316 runtime->period_size);
317 }
318 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200319 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100320 pos -= pos % runtime->min_align;
321 if (xrun_debug(substream, XRUN_DEBUG_LOG))
322 xrun_log(substream, pos);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100323 hw_base = runtime->hw_ptr_base;
324 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100325 if (in_interrupt) {
326 /* we know that one period was processed */
327 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
328 delta = old_hw_ptr - (old_hw_ptr % runtime->period_size)
329 + runtime->period_size;
330 if (delta > new_hw_ptr) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100331 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100332 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100333 hw_base = 0;
334 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100335 goto __delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100338 /* new_hw_ptr might be lower than old_hw_ptr in case when */
339 /* pointer crosses the end of the ring buffer */
340 if (new_hw_ptr < old_hw_ptr) {
341 hw_base += runtime->buffer_size;
342 if (hw_base >= runtime->boundary)
343 hw_base = 0;
344 new_hw_ptr = hw_base + pos;
345 }
346 __delta:
347 delta = (new_hw_ptr - old_hw_ptr) % runtime->boundary;
348 if (xrun_debug(substream, in_interrupt ?
349 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
350 char name[16];
351 pcm_debug_name(substream, name, sizeof(name));
352 snd_printd("%s_update: %s: pos=%u/%u/%u, "
353 "hwptr=%ld/%ld/%ld/%ld\n",
354 in_interrupt ? "period" : "hwptr",
355 name,
356 (unsigned int)pos,
357 (unsigned int)runtime->period_size,
358 (unsigned int)runtime->buffer_size,
359 (unsigned long)delta,
360 (unsigned long)old_hw_ptr,
361 (unsigned long)new_hw_ptr,
362 (unsigned long)runtime->hw_ptr_base);
363 }
364 /* something must be really wrong */
365 if (delta >= runtime->buffer_size) {
366 hw_ptr_error(substream,
367 "Unexpected hw_pointer value %s"
368 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
369 "old_hw_ptr=%ld)\n",
370 in_interrupt ? "[Q] " : "[P]",
371 substream->stream, (long)pos,
372 (long)new_hw_ptr, (long)old_hw_ptr);
373 return 0;
374 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200375
376 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100377 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200378 goto no_jiffies_check;
379
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200380 /* Skip the jiffies check for hardwares with BATCH flag.
381 * Such hardware usually just increases the position at each IRQ,
382 * thus it can't give any strange position.
383 */
384 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
385 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100386 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200387 if (hdelta < runtime->delay)
388 goto no_jiffies_check;
389 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200390 jdelta = jiffies - runtime->hw_ptr_jiffies;
391 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
392 delta = jdelta /
393 (((runtime->period_size * HZ) / runtime->rate)
394 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100395 /* move new_hw_ptr according jiffies not pos variable */
396 new_hw_ptr = old_hw_ptr;
397 /* use loop to avoid checks for delta overflows */
398 /* the delta value is small or zero in most cases */
399 while (delta > 0) {
400 new_hw_ptr += runtime->period_size;
401 if (new_hw_ptr >= runtime->boundary)
402 new_hw_ptr -= runtime->boundary;
403 delta--;
404 }
405 /* align hw_base to buffer_size */
406 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
407 delta = 0;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200408 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100409 "hw_ptr skipping! %s"
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200410 "(pos=%ld, delta=%ld, period=%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100411 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
412 in_interrupt ? "[Q] " : "",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200413 (long)pos, (long)hdelta,
414 (long)runtime->period_size, jdelta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100415 ((hdelta * HZ) / runtime->rate), delta,
416 (unsigned long)old_hw_ptr,
417 (unsigned long)new_hw_ptr);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200418 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200419 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200420 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100421 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100422 "Lost interrupts? %s"
423 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
424 "old_hw_ptr=%ld)\n",
425 in_interrupt ? "[Q] " : "",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100426 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100427 (long)new_hw_ptr,
428 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100429 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100430
431 if (runtime->status->hw_ptr == new_hw_ptr)
432 return 0;
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
435 runtime->silence_size > 0)
436 snd_pcm_playback_silence(substream, new_hw_ptr);
437
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100438 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200440 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200441 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
442 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jaroslav Kysela12509322010-01-07 15:36:31 +0100444 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100448int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100450 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/**
454 * snd_pcm_set_ops - set the PCM operators
455 * @pcm: the pcm instance
456 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
457 * @ops: the operator table
458 *
459 * Sets the given PCM operators to the pcm instance.
460 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100461void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Takashi Iwai877211f2005-11-17 13:59:38 +0100463 struct snd_pcm_str *stream = &pcm->streams[direction];
464 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 for (substream = stream->substream; substream != NULL; substream = substream->next)
467 substream->ops = ops;
468}
469
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200470EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472/**
473 * snd_pcm_sync - set the PCM sync id
474 * @substream: the pcm substream
475 *
476 * Sets the PCM sync identifier for the card.
477 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100478void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Takashi Iwai877211f2005-11-17 13:59:38 +0100480 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 runtime->sync.id32[0] = substream->pcm->card->number;
483 runtime->sync.id32[1] = -1;
484 runtime->sync.id32[2] = -1;
485 runtime->sync.id32[3] = -1;
486}
487
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200488EXPORT_SYMBOL(snd_pcm_set_sync);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * Standard ioctl routine
492 */
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static inline unsigned int div32(unsigned int a, unsigned int b,
495 unsigned int *r)
496{
497 if (b == 0) {
498 *r = 0;
499 return UINT_MAX;
500 }
501 *r = a % b;
502 return a / b;
503}
504
505static inline unsigned int div_down(unsigned int a, unsigned int b)
506{
507 if (b == 0)
508 return UINT_MAX;
509 return a / b;
510}
511
512static inline unsigned int div_up(unsigned int a, unsigned int b)
513{
514 unsigned int r;
515 unsigned int q;
516 if (b == 0)
517 return UINT_MAX;
518 q = div32(a, b, &r);
519 if (r)
520 ++q;
521 return q;
522}
523
524static inline unsigned int mul(unsigned int a, unsigned int b)
525{
526 if (a == 0)
527 return 0;
528 if (div_down(UINT_MAX, a) < b)
529 return UINT_MAX;
530 return a * b;
531}
532
533static inline unsigned int muldiv32(unsigned int a, unsigned int b,
534 unsigned int c, unsigned int *r)
535{
536 u_int64_t n = (u_int64_t) a * b;
537 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200538 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *r = 0;
540 return UINT_MAX;
541 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200542 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (n >= UINT_MAX) {
544 *r = 0;
545 return UINT_MAX;
546 }
547 return n;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/**
551 * snd_interval_refine - refine the interval value of configurator
552 * @i: the interval value to refine
553 * @v: the interval value to refer to
554 *
555 * Refines the interval value with the reference value.
556 * The interval is changed to the range satisfying both intervals.
557 * The interval status (min, max, integer, etc.) are evaluated.
558 *
559 * Returns non-zero if the value is changed, zero if not changed.
560 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100561int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200564 if (snd_BUG_ON(snd_interval_empty(i)))
565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (i->min < v->min) {
567 i->min = v->min;
568 i->openmin = v->openmin;
569 changed = 1;
570 } else if (i->min == v->min && !i->openmin && v->openmin) {
571 i->openmin = 1;
572 changed = 1;
573 }
574 if (i->max > v->max) {
575 i->max = v->max;
576 i->openmax = v->openmax;
577 changed = 1;
578 } else if (i->max == v->max && !i->openmax && v->openmax) {
579 i->openmax = 1;
580 changed = 1;
581 }
582 if (!i->integer && v->integer) {
583 i->integer = 1;
584 changed = 1;
585 }
586 if (i->integer) {
587 if (i->openmin) {
588 i->min++;
589 i->openmin = 0;
590 }
591 if (i->openmax) {
592 i->max--;
593 i->openmax = 0;
594 }
595 } else if (!i->openmin && !i->openmax && i->min == i->max)
596 i->integer = 1;
597 if (snd_interval_checkempty(i)) {
598 snd_interval_none(i);
599 return -EINVAL;
600 }
601 return changed;
602}
603
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200604EXPORT_SYMBOL(snd_interval_refine);
605
Takashi Iwai877211f2005-11-17 13:59:38 +0100606static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200608 if (snd_BUG_ON(snd_interval_empty(i)))
609 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (snd_interval_single(i))
611 return 0;
612 i->max = i->min;
613 i->openmax = i->openmin;
614 if (i->openmax)
615 i->max++;
616 return 1;
617}
618
Takashi Iwai877211f2005-11-17 13:59:38 +0100619static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200621 if (snd_BUG_ON(snd_interval_empty(i)))
622 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (snd_interval_single(i))
624 return 0;
625 i->min = i->max;
626 i->openmin = i->openmax;
627 if (i->openmin)
628 i->min--;
629 return 1;
630}
631
Takashi Iwai877211f2005-11-17 13:59:38 +0100632void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 if (a->empty || b->empty) {
635 snd_interval_none(c);
636 return;
637 }
638 c->empty = 0;
639 c->min = mul(a->min, b->min);
640 c->openmin = (a->openmin || b->openmin);
641 c->max = mul(a->max, b->max);
642 c->openmax = (a->openmax || b->openmax);
643 c->integer = (a->integer && b->integer);
644}
645
646/**
647 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200648 * @a: dividend
649 * @b: divisor
650 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
652 * c = a / b
653 *
654 * Returns non-zero if the value is changed, zero if not changed.
655 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100656void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 unsigned int r;
659 if (a->empty || b->empty) {
660 snd_interval_none(c);
661 return;
662 }
663 c->empty = 0;
664 c->min = div32(a->min, b->max, &r);
665 c->openmin = (r || a->openmin || b->openmax);
666 if (b->min > 0) {
667 c->max = div32(a->max, b->min, &r);
668 if (r) {
669 c->max++;
670 c->openmax = 1;
671 } else
672 c->openmax = (a->openmax || b->openmin);
673 } else {
674 c->max = UINT_MAX;
675 c->openmax = 0;
676 }
677 c->integer = 0;
678}
679
680/**
681 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200682 * @a: dividend 1
683 * @b: dividend 2
684 * @k: divisor (as integer)
685 * @c: result
686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * c = a * b / k
688 *
689 * Returns non-zero if the value is changed, zero if not changed.
690 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100691void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
692 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 unsigned int r;
695 if (a->empty || b->empty) {
696 snd_interval_none(c);
697 return;
698 }
699 c->empty = 0;
700 c->min = muldiv32(a->min, b->min, k, &r);
701 c->openmin = (r || a->openmin || b->openmin);
702 c->max = muldiv32(a->max, b->max, k, &r);
703 if (r) {
704 c->max++;
705 c->openmax = 1;
706 } else
707 c->openmax = (a->openmax || b->openmax);
708 c->integer = 0;
709}
710
711/**
712 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200713 * @a: dividend 1
714 * @k: dividend 2 (as integer)
715 * @b: divisor
716 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *
718 * c = a * k / b
719 *
720 * Returns non-zero if the value is changed, zero if not changed.
721 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100722void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
723 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 unsigned int r;
726 if (a->empty || b->empty) {
727 snd_interval_none(c);
728 return;
729 }
730 c->empty = 0;
731 c->min = muldiv32(a->min, k, b->max, &r);
732 c->openmin = (r || a->openmin || b->openmax);
733 if (b->min > 0) {
734 c->max = muldiv32(a->max, k, b->min, &r);
735 if (r) {
736 c->max++;
737 c->openmax = 1;
738 } else
739 c->openmax = (a->openmax || b->openmin);
740 } else {
741 c->max = UINT_MAX;
742 c->openmax = 0;
743 }
744 c->integer = 0;
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/* ---- */
748
749
750/**
751 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200752 * @i: interval to refine
753 * @rats_count: number of ratnum_t
754 * @rats: ratnum_t array
755 * @nump: pointer to store the resultant numerator
756 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
758 * Returns non-zero if the value is changed, zero if not changed.
759 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100760int snd_interval_ratnum(struct snd_interval *i,
761 unsigned int rats_count, struct snd_ratnum *rats,
762 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 unsigned int best_num, best_diff, best_den;
765 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100766 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int err;
768
769 best_num = best_den = best_diff = 0;
770 for (k = 0; k < rats_count; ++k) {
771 unsigned int num = rats[k].num;
772 unsigned int den;
773 unsigned int q = i->min;
774 int diff;
775 if (q == 0)
776 q = 1;
777 den = div_down(num, q);
778 if (den < rats[k].den_min)
779 continue;
780 if (den > rats[k].den_max)
781 den = rats[k].den_max;
782 else {
783 unsigned int r;
784 r = (den - rats[k].den_min) % rats[k].den_step;
785 if (r != 0)
786 den -= r;
787 }
788 diff = num - q * den;
789 if (best_num == 0 ||
790 diff * best_den < best_diff * den) {
791 best_diff = diff;
792 best_den = den;
793 best_num = num;
794 }
795 }
796 if (best_den == 0) {
797 i->empty = 1;
798 return -EINVAL;
799 }
800 t.min = div_down(best_num, best_den);
801 t.openmin = !!(best_num % best_den);
802
803 best_num = best_den = best_diff = 0;
804 for (k = 0; k < rats_count; ++k) {
805 unsigned int num = rats[k].num;
806 unsigned int den;
807 unsigned int q = i->max;
808 int diff;
809 if (q == 0) {
810 i->empty = 1;
811 return -EINVAL;
812 }
813 den = div_up(num, q);
814 if (den > rats[k].den_max)
815 continue;
816 if (den < rats[k].den_min)
817 den = rats[k].den_min;
818 else {
819 unsigned int r;
820 r = (den - rats[k].den_min) % rats[k].den_step;
821 if (r != 0)
822 den += rats[k].den_step - r;
823 }
824 diff = q * den - num;
825 if (best_num == 0 ||
826 diff * best_den < best_diff * den) {
827 best_diff = diff;
828 best_den = den;
829 best_num = num;
830 }
831 }
832 if (best_den == 0) {
833 i->empty = 1;
834 return -EINVAL;
835 }
836 t.max = div_up(best_num, best_den);
837 t.openmax = !!(best_num % best_den);
838 t.integer = 0;
839 err = snd_interval_refine(i, &t);
840 if (err < 0)
841 return err;
842
843 if (snd_interval_single(i)) {
844 if (nump)
845 *nump = best_num;
846 if (denp)
847 *denp = best_den;
848 }
849 return err;
850}
851
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200852EXPORT_SYMBOL(snd_interval_ratnum);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/**
855 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200856 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100857 * @rats_count: number of struct ratden
858 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200859 * @nump: pointer to store the resultant numerator
860 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 *
862 * Returns non-zero if the value is changed, zero if not changed.
863 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100864static int snd_interval_ratden(struct snd_interval *i,
865 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned int *nump, unsigned int *denp)
867{
868 unsigned int best_num, best_diff, best_den;
869 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int err;
872
873 best_num = best_den = best_diff = 0;
874 for (k = 0; k < rats_count; ++k) {
875 unsigned int num;
876 unsigned int den = rats[k].den;
877 unsigned int q = i->min;
878 int diff;
879 num = mul(q, den);
880 if (num > rats[k].num_max)
881 continue;
882 if (num < rats[k].num_min)
883 num = rats[k].num_max;
884 else {
885 unsigned int r;
886 r = (num - rats[k].num_min) % rats[k].num_step;
887 if (r != 0)
888 num += rats[k].num_step - r;
889 }
890 diff = num - q * den;
891 if (best_num == 0 ||
892 diff * best_den < best_diff * den) {
893 best_diff = diff;
894 best_den = den;
895 best_num = num;
896 }
897 }
898 if (best_den == 0) {
899 i->empty = 1;
900 return -EINVAL;
901 }
902 t.min = div_down(best_num, best_den);
903 t.openmin = !!(best_num % best_den);
904
905 best_num = best_den = best_diff = 0;
906 for (k = 0; k < rats_count; ++k) {
907 unsigned int num;
908 unsigned int den = rats[k].den;
909 unsigned int q = i->max;
910 int diff;
911 num = mul(q, den);
912 if (num < rats[k].num_min)
913 continue;
914 if (num > rats[k].num_max)
915 num = rats[k].num_max;
916 else {
917 unsigned int r;
918 r = (num - rats[k].num_min) % rats[k].num_step;
919 if (r != 0)
920 num -= r;
921 }
922 diff = q * den - num;
923 if (best_num == 0 ||
924 diff * best_den < best_diff * den) {
925 best_diff = diff;
926 best_den = den;
927 best_num = num;
928 }
929 }
930 if (best_den == 0) {
931 i->empty = 1;
932 return -EINVAL;
933 }
934 t.max = div_up(best_num, best_den);
935 t.openmax = !!(best_num % best_den);
936 t.integer = 0;
937 err = snd_interval_refine(i, &t);
938 if (err < 0)
939 return err;
940
941 if (snd_interval_single(i)) {
942 if (nump)
943 *nump = best_num;
944 if (denp)
945 *denp = best_den;
946 }
947 return err;
948}
949
950/**
951 * snd_interval_list - refine the interval value from the list
952 * @i: the interval value to refine
953 * @count: the number of elements in the list
954 * @list: the value list
955 * @mask: the bit-mask to evaluate
956 *
957 * Refines the interval value from the list.
958 * When mask is non-zero, only the elements corresponding to bit 1 are
959 * evaluated.
960 *
961 * Returns non-zero if the value is changed, zero if not changed.
962 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100963int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200966 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +0100967
968 if (!count) {
969 i->empty = 1;
970 return -EINVAL;
971 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200972 snd_interval_any(&list_range);
973 list_range.min = UINT_MAX;
974 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 for (k = 0; k < count; k++) {
976 if (mask && !(mask & (1 << k)))
977 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200978 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200980 list_range.min = min(list_range.min, list[k]);
981 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200983 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200986EXPORT_SYMBOL(snd_interval_list);
987
Takashi Iwai877211f2005-11-17 13:59:38 +0100988static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 unsigned int n;
991 int changed = 0;
992 n = (i->min - min) % step;
993 if (n != 0 || i->openmin) {
994 i->min += step - n;
995 changed = 1;
996 }
997 n = (i->max - min) % step;
998 if (n != 0 || i->openmax) {
999 i->max -= n;
1000 changed = 1;
1001 }
1002 if (snd_interval_checkempty(i)) {
1003 i->empty = 1;
1004 return -EINVAL;
1005 }
1006 return changed;
1007}
1008
1009/* Info constraints helpers */
1010
1011/**
1012 * snd_pcm_hw_rule_add - add the hw-constraint rule
1013 * @runtime: the pcm runtime instance
1014 * @cond: condition bits
1015 * @var: the variable to evaluate
1016 * @func: the evaluation function
1017 * @private: the private data pointer passed to function
1018 * @dep: the dependent variables
1019 *
1020 * Returns zero if successful, or a negative error code on failure.
1021 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001022int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int var,
1024 snd_pcm_hw_rule_func_t func, void *private,
1025 int dep, ...)
1026{
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned int k;
1030 va_list args;
1031 va_start(args, dep);
1032 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001033 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned int new_rules = constrs->rules_all + 16;
1035 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1036 if (!new)
1037 return -ENOMEM;
1038 if (constrs->rules) {
1039 memcpy(new, constrs->rules,
1040 constrs->rules_num * sizeof(*c));
1041 kfree(constrs->rules);
1042 }
1043 constrs->rules = new;
1044 constrs->rules_all = new_rules;
1045 }
1046 c = &constrs->rules[constrs->rules_num];
1047 c->cond = cond;
1048 c->func = func;
1049 c->var = var;
1050 c->private = private;
1051 k = 0;
1052 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001053 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 c->deps[k++] = dep;
1056 if (dep < 0)
1057 break;
1058 dep = va_arg(args, int);
1059 }
1060 constrs->rules_num++;
1061 va_end(args);
1062 return 0;
1063}
1064
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001065EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001068 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001069 * @runtime: PCM runtime instance
1070 * @var: hw_params variable to apply the mask
1071 * @mask: the bitmap mask
1072 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001073 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001075int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 u_int32_t mask)
1077{
Takashi Iwai877211f2005-11-17 13:59:38 +01001078 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1079 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *maskp->bits &= mask;
1081 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1082 if (*maskp->bits == 0)
1083 return -EINVAL;
1084 return 0;
1085}
1086
1087/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001088 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001089 * @runtime: PCM runtime instance
1090 * @var: hw_params variable to apply the mask
1091 * @mask: the 64bit bitmap mask
1092 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001093 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001095int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 u_int64_t mask)
1097{
Takashi Iwai877211f2005-11-17 13:59:38 +01001098 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1099 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 maskp->bits[0] &= (u_int32_t)mask;
1101 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1102 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1103 if (! maskp->bits[0] && ! maskp->bits[1])
1104 return -EINVAL;
1105 return 0;
1106}
1107
1108/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001109 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001110 * @runtime: PCM runtime instance
1111 * @var: hw_params variable to apply the integer constraint
1112 *
1113 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001115int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai877211f2005-11-17 13:59:38 +01001117 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return snd_interval_setinteger(constrs_interval(constrs, var));
1119}
1120
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001121EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001124 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001125 * @runtime: PCM runtime instance
1126 * @var: hw_params variable to apply the range
1127 * @min: the minimal value
1128 * @max: the maximal value
1129 *
1130 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001132int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned int min, unsigned int max)
1134{
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1136 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 t.min = min;
1138 t.max = max;
1139 t.openmin = t.openmax = 0;
1140 t.integer = 0;
1141 return snd_interval_refine(constrs_interval(constrs, var), &t);
1142}
1143
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001144EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1145
Takashi Iwai877211f2005-11-17 13:59:38 +01001146static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1147 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwai877211f2005-11-17 13:59:38 +01001149 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1151}
1152
1153
1154/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001155 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001156 * @runtime: PCM runtime instance
1157 * @cond: condition bits
1158 * @var: hw_params variable to apply the list constraint
1159 * @l: list
1160 *
1161 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001163int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned int cond,
1165 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001166 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 return snd_pcm_hw_rule_add(runtime, cond, var,
1169 snd_pcm_hw_rule_list, l,
1170 var, -1);
1171}
1172
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001173EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1174
Takashi Iwai877211f2005-11-17 13:59:38 +01001175static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1176 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwai877211f2005-11-17 13:59:38 +01001178 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned int num = 0, den = 0;
1180 int err;
1181 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1182 r->nrats, r->rats, &num, &den);
1183 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1184 params->rate_num = num;
1185 params->rate_den = den;
1186 }
1187 return err;
1188}
1189
1190/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001191 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001192 * @runtime: PCM runtime instance
1193 * @cond: condition bits
1194 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001195 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001197int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 unsigned int cond,
1199 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001200 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 return snd_pcm_hw_rule_add(runtime, cond, var,
1203 snd_pcm_hw_rule_ratnums, r,
1204 var, -1);
1205}
1206
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001207EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1208
Takashi Iwai877211f2005-11-17 13:59:38 +01001209static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1210 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Takashi Iwai877211f2005-11-17 13:59:38 +01001212 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 unsigned int num = 0, den = 0;
1214 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1215 r->nrats, r->rats, &num, &den);
1216 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1217 params->rate_num = num;
1218 params->rate_den = den;
1219 }
1220 return err;
1221}
1222
1223/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001224 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001225 * @runtime: PCM runtime instance
1226 * @cond: condition bits
1227 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001228 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001230int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int cond,
1232 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001233 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 return snd_pcm_hw_rule_add(runtime, cond, var,
1236 snd_pcm_hw_rule_ratdens, r,
1237 var, -1);
1238}
1239
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001240EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1241
Takashi Iwai877211f2005-11-17 13:59:38 +01001242static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1243 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 unsigned int l = (unsigned long) rule->private;
1246 int width = l & 0xffff;
1247 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (snd_interval_single(i) && snd_interval_value(i) == width)
1250 params->msbits = msbits;
1251 return 0;
1252}
1253
1254/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001255 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001256 * @runtime: PCM runtime instance
1257 * @cond: condition bits
1258 * @width: sample bits width
1259 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001261int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 unsigned int cond,
1263 unsigned int width,
1264 unsigned int msbits)
1265{
1266 unsigned long l = (msbits << 16) | width;
1267 return snd_pcm_hw_rule_add(runtime, cond, -1,
1268 snd_pcm_hw_rule_msbits,
1269 (void*) l,
1270 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1271}
1272
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001273EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1274
Takashi Iwai877211f2005-11-17 13:59:38 +01001275static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1276 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 unsigned long step = (unsigned long) rule->private;
1279 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1280}
1281
1282/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001283 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001284 * @runtime: PCM runtime instance
1285 * @cond: condition bits
1286 * @var: hw_params variable to apply the step constraint
1287 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001289int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned int cond,
1291 snd_pcm_hw_param_t var,
1292 unsigned long step)
1293{
1294 return snd_pcm_hw_rule_add(runtime, cond, var,
1295 snd_pcm_hw_rule_step, (void *) step,
1296 var, -1);
1297}
1298
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001299EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1300
Takashi Iwai877211f2005-11-17 13:59:38 +01001301static 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 -07001302{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001303 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1305 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1306 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1307 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1308 };
1309 return snd_interval_list(hw_param_interval(params, rule->var),
1310 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1311}
1312
1313/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001314 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001315 * @runtime: PCM runtime instance
1316 * @cond: condition bits
1317 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001319int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 unsigned int cond,
1321 snd_pcm_hw_param_t var)
1322{
1323 return snd_pcm_hw_rule_add(runtime, cond, var,
1324 snd_pcm_hw_rule_pow2, NULL,
1325 var, -1);
1326}
1327
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001328EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1329
Takashi Iwai877211f2005-11-17 13:59:38 +01001330static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001331 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 if (hw_is_mask(var)) {
1334 snd_mask_any(hw_param_mask(params, var));
1335 params->cmask |= 1 << var;
1336 params->rmask |= 1 << var;
1337 return;
1338 }
1339 if (hw_is_interval(var)) {
1340 snd_interval_any(hw_param_interval(params, var));
1341 params->cmask |= 1 << var;
1342 params->rmask |= 1 << var;
1343 return;
1344 }
1345 snd_BUG();
1346}
1347
Takashi Iwai877211f2005-11-17 13:59:38 +01001348void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 unsigned int k;
1351 memset(params, 0, sizeof(*params));
1352 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1353 _snd_pcm_hw_param_any(params, k);
1354 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1355 _snd_pcm_hw_param_any(params, k);
1356 params->info = ~0U;
1357}
1358
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001359EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001362 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001363 * @params: the hw_params instance
1364 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001365 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001367 * Return the value for field @var if it's fixed in configuration space
1368 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001370int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1371 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001374 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!snd_mask_single(mask))
1376 return -EINVAL;
1377 if (dir)
1378 *dir = 0;
1379 return snd_mask_value(mask);
1380 }
1381 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001382 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!snd_interval_single(i))
1384 return -EINVAL;
1385 if (dir)
1386 *dir = i->openmin;
1387 return snd_interval_value(i);
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -EINVAL;
1390}
1391
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001392EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwai877211f2005-11-17 13:59:38 +01001394void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 snd_pcm_hw_param_t var)
1396{
1397 if (hw_is_mask(var)) {
1398 snd_mask_none(hw_param_mask(params, var));
1399 params->cmask |= 1 << var;
1400 params->rmask |= 1 << var;
1401 } else if (hw_is_interval(var)) {
1402 snd_interval_none(hw_param_interval(params, var));
1403 params->cmask |= 1 << var;
1404 params->rmask |= 1 << var;
1405 } else {
1406 snd_BUG();
1407 }
1408}
1409
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001410EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Takashi Iwai877211f2005-11-17 13:59:38 +01001412static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001413 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int changed;
1416 if (hw_is_mask(var))
1417 changed = snd_mask_refine_first(hw_param_mask(params, var));
1418 else if (hw_is_interval(var))
1419 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001420 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (changed) {
1423 params->cmask |= 1 << var;
1424 params->rmask |= 1 << var;
1425 }
1426 return changed;
1427}
1428
1429
1430/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001431 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001432 * @pcm: PCM instance
1433 * @params: the hw_params instance
1434 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001435 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001437 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * values > minimum. Reduce configuration space accordingly.
1439 * Return the minimum.
1440 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001441int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1442 struct snd_pcm_hw_params *params,
1443 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 int changed = _snd_pcm_hw_param_first(params, var);
1446 if (changed < 0)
1447 return changed;
1448 if (params->rmask) {
1449 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001450 if (snd_BUG_ON(err < 0))
1451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 return snd_pcm_hw_param_value(params, var, dir);
1454}
1455
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001456EXPORT_SYMBOL(snd_pcm_hw_param_first);
1457
Takashi Iwai877211f2005-11-17 13:59:38 +01001458static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001459 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 int changed;
1462 if (hw_is_mask(var))
1463 changed = snd_mask_refine_last(hw_param_mask(params, var));
1464 else if (hw_is_interval(var))
1465 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001466 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (changed) {
1469 params->cmask |= 1 << var;
1470 params->rmask |= 1 << var;
1471 }
1472 return changed;
1473}
1474
1475
1476/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001477 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001478 * @pcm: PCM instance
1479 * @params: the hw_params instance
1480 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001481 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001483 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * values < maximum. Reduce configuration space accordingly.
1485 * Return the maximum.
1486 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001487int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1488 struct snd_pcm_hw_params *params,
1489 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 int changed = _snd_pcm_hw_param_last(params, var);
1492 if (changed < 0)
1493 return changed;
1494 if (params->rmask) {
1495 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001496 if (snd_BUG_ON(err < 0))
1497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 return snd_pcm_hw_param_value(params, var, dir);
1500}
1501
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001502EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001505 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001506 * @pcm: PCM instance
1507 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001509 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * The configuration chosen is that obtained fixing in this order:
1511 * first access, first format, first subformat, min channels,
1512 * min rate, min period time, max buffer size, min tick time
1513 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001514int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1515 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001517 static int vars[] = {
1518 SNDRV_PCM_HW_PARAM_ACCESS,
1519 SNDRV_PCM_HW_PARAM_FORMAT,
1520 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1521 SNDRV_PCM_HW_PARAM_CHANNELS,
1522 SNDRV_PCM_HW_PARAM_RATE,
1523 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1524 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1525 SNDRV_PCM_HW_PARAM_TICK_TIME,
1526 -1
1527 };
1528 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001530 for (v = vars; *v != -1; v++) {
1531 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1532 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1533 else
1534 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001535 if (snd_BUG_ON(err < 0))
1536 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
1539}
1540
Takashi Iwai877211f2005-11-17 13:59:38 +01001541static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 void *arg)
1543{
Takashi Iwai877211f2005-11-17 13:59:38 +01001544 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 unsigned long flags;
1546 snd_pcm_stream_lock_irqsave(substream, flags);
1547 if (snd_pcm_running(substream) &&
1548 snd_pcm_update_hw_ptr(substream) >= 0)
1549 runtime->status->hw_ptr %= runtime->buffer_size;
1550 else
1551 runtime->status->hw_ptr = 0;
1552 snd_pcm_stream_unlock_irqrestore(substream, flags);
1553 return 0;
1554}
1555
Takashi Iwai877211f2005-11-17 13:59:38 +01001556static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 void *arg)
1558{
Takashi Iwai877211f2005-11-17 13:59:38 +01001559 struct snd_pcm_channel_info *info = arg;
1560 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int width;
1562 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1563 info->offset = -1;
1564 return 0;
1565 }
1566 width = snd_pcm_format_physical_width(runtime->format);
1567 if (width < 0)
1568 return width;
1569 info->offset = 0;
1570 switch (runtime->access) {
1571 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1572 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1573 info->first = info->channel * width;
1574 info->step = runtime->channels * width;
1575 break;
1576 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1577 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1578 {
1579 size_t size = runtime->dma_bytes / runtime->channels;
1580 info->first = info->channel * size * 8;
1581 info->step = width;
1582 break;
1583 }
1584 default:
1585 snd_BUG();
1586 break;
1587 }
1588 return 0;
1589}
1590
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001591static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1592 void *arg)
1593{
1594 struct snd_pcm_hw_params *params = arg;
1595 snd_pcm_format_t format;
1596 int channels, width;
1597
1598 params->fifo_size = substream->runtime->hw.fifo_size;
1599 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1600 format = params_format(params);
1601 channels = params_channels(params);
1602 width = snd_pcm_format_physical_width(format);
1603 params->fifo_size /= width * channels;
1604 }
1605 return 0;
1606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1610 * @substream: the pcm substream instance
1611 * @cmd: ioctl command
1612 * @arg: ioctl argument
1613 *
1614 * Processes the generic ioctl commands for PCM.
1615 * Can be passed as the ioctl callback for PCM ops.
1616 *
1617 * Returns zero if successful, or a negative error code on failure.
1618 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001619int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 unsigned int cmd, void *arg)
1621{
1622 switch (cmd) {
1623 case SNDRV_PCM_IOCTL1_INFO:
1624 return 0;
1625 case SNDRV_PCM_IOCTL1_RESET:
1626 return snd_pcm_lib_ioctl_reset(substream, arg);
1627 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1628 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001629 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1630 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 return -ENXIO;
1633}
1634
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001635EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/**
1638 * snd_pcm_period_elapsed - update the pcm status for the next period
1639 * @substream: the pcm substream instance
1640 *
1641 * This function is called from the interrupt handler when the
1642 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001643 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
1645 * Even if more than one periods have elapsed since the last call, you
1646 * have to call this only once.
1647 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001648void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Takashi Iwai877211f2005-11-17 13:59:38 +01001650 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 unsigned long flags;
1652
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001653 if (PCM_RUNTIME_CHECK(substream))
1654 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (runtime->transfer_ack_begin)
1658 runtime->transfer_ack_begin(substream);
1659
1660 snd_pcm_stream_lock_irqsave(substream, flags);
1661 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001662 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto _end;
1664
1665 if (substream->timer_running)
1666 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 _end:
1668 snd_pcm_stream_unlock_irqrestore(substream, flags);
1669 if (runtime->transfer_ack_end)
1670 runtime->transfer_ack_end(substream);
1671 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1672}
1673
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001674EXPORT_SYMBOL(snd_pcm_period_elapsed);
1675
Takashi Iwai13075512008-01-08 18:08:14 +01001676/*
1677 * Wait until avail_min data becomes available
1678 * Returns a negative error code if any error occurs during operation.
1679 * The available space is stored on availp. When err = 0 and avail = 0
1680 * on the capture stream, it indicates the stream is in DRAINING state.
1681 */
1682static int wait_for_avail_min(struct snd_pcm_substream *substream,
1683 snd_pcm_uframes_t *availp)
1684{
1685 struct snd_pcm_runtime *runtime = substream->runtime;
1686 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1687 wait_queue_t wait;
1688 int err = 0;
1689 snd_pcm_uframes_t avail = 0;
1690 long tout;
1691
1692 init_waitqueue_entry(&wait, current);
1693 add_wait_queue(&runtime->sleep, &wait);
1694 for (;;) {
1695 if (signal_pending(current)) {
1696 err = -ERESTARTSYS;
1697 break;
1698 }
1699 set_current_state(TASK_INTERRUPTIBLE);
1700 snd_pcm_stream_unlock_irq(substream);
1701 tout = schedule_timeout(msecs_to_jiffies(10000));
1702 snd_pcm_stream_lock_irq(substream);
1703 switch (runtime->status->state) {
1704 case SNDRV_PCM_STATE_SUSPENDED:
1705 err = -ESTRPIPE;
1706 goto _endloop;
1707 case SNDRV_PCM_STATE_XRUN:
1708 err = -EPIPE;
1709 goto _endloop;
1710 case SNDRV_PCM_STATE_DRAINING:
1711 if (is_playback)
1712 err = -EPIPE;
1713 else
1714 avail = 0; /* indicate draining */
1715 goto _endloop;
1716 case SNDRV_PCM_STATE_OPEN:
1717 case SNDRV_PCM_STATE_SETUP:
1718 case SNDRV_PCM_STATE_DISCONNECTED:
1719 err = -EBADFD;
1720 goto _endloop;
1721 }
1722 if (!tout) {
1723 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1724 is_playback ? "playback" : "capture");
1725 err = -EIO;
1726 break;
1727 }
1728 if (is_playback)
1729 avail = snd_pcm_playback_avail(runtime);
1730 else
1731 avail = snd_pcm_capture_avail(runtime);
1732 if (avail >= runtime->control->avail_min)
1733 break;
1734 }
1735 _endloop:
1736 remove_wait_queue(&runtime->sleep, &wait);
1737 *availp = avail;
1738 return err;
1739}
1740
Takashi Iwai877211f2005-11-17 13:59:38 +01001741static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int hwoff,
1743 unsigned long data, unsigned int off,
1744 snd_pcm_uframes_t frames)
1745{
Takashi Iwai877211f2005-11-17 13:59:38 +01001746 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int err;
1748 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1749 if (substream->ops->copy) {
1750 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1751 return err;
1752 } else {
1753 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1755 return -EFAULT;
1756 }
1757 return 0;
1758}
1759
Takashi Iwai877211f2005-11-17 13:59:38 +01001760typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned long data, unsigned int off,
1762 snd_pcm_uframes_t size);
1763
Takashi Iwai877211f2005-11-17 13:59:38 +01001764static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned long data,
1766 snd_pcm_uframes_t size,
1767 int nonblock,
1768 transfer_f transfer)
1769{
Takashi Iwai877211f2005-11-17 13:59:38 +01001770 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 snd_pcm_uframes_t xfer = 0;
1772 snd_pcm_uframes_t offset = 0;
1773 int err = 0;
1774
1775 if (size == 0)
1776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 snd_pcm_stream_lock_irq(substream);
1779 switch (runtime->status->state) {
1780 case SNDRV_PCM_STATE_PREPARED:
1781 case SNDRV_PCM_STATE_RUNNING:
1782 case SNDRV_PCM_STATE_PAUSED:
1783 break;
1784 case SNDRV_PCM_STATE_XRUN:
1785 err = -EPIPE;
1786 goto _end_unlock;
1787 case SNDRV_PCM_STATE_SUSPENDED:
1788 err = -ESTRPIPE;
1789 goto _end_unlock;
1790 default:
1791 err = -EBADFD;
1792 goto _end_unlock;
1793 }
1794
Jaroslav Kysela12509322010-01-07 15:36:31 +01001795 runtime->nowake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 while (size > 0) {
1797 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1798 snd_pcm_uframes_t avail;
1799 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001800 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 snd_pcm_update_hw_ptr(substream);
1802 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001803 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (nonblock) {
1805 err = -EAGAIN;
1806 goto _end_unlock;
1807 }
Takashi Iwai13075512008-01-08 18:08:14 +01001808 err = wait_for_avail_min(substream, &avail);
1809 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 frames = size > avail ? avail : size;
1813 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1814 if (frames > cont)
1815 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001816 if (snd_BUG_ON(!frames)) {
Jaroslav Kysela12509322010-01-07 15:36:31 +01001817 runtime->nowake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001818 snd_pcm_stream_unlock_irq(substream);
1819 return -EINVAL;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 appl_ptr = runtime->control->appl_ptr;
1822 appl_ofs = appl_ptr % runtime->buffer_size;
1823 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001824 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001826 if (err < 0)
1827 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 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:
Jaroslav Kysela12509322010-01-07 15:36:31 +01001856 runtime->nowake = 0;
1857 if (xfer > 0 && err >= 0)
1858 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1861}
1862
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001863/* sanity-check for read/write methods */
1864static int pcm_sanity_check(struct snd_pcm_substream *substream)
1865{
1866 struct snd_pcm_runtime *runtime;
1867 if (PCM_RUNTIME_CHECK(substream))
1868 return -ENXIO;
1869 runtime = substream->runtime;
1870 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1871 return -EINVAL;
1872 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1873 return -EBADFD;
1874 return 0;
1875}
1876
Takashi Iwai877211f2005-11-17 13:59:38 +01001877snd_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 -07001878{
Takashi Iwai877211f2005-11-17 13:59:38 +01001879 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001881 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001883 err = pcm_sanity_check(substream);
1884 if (err < 0)
1885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001887 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1890 runtime->channels > 1)
1891 return -EINVAL;
1892 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1893 snd_pcm_lib_write_transfer);
1894}
1895
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001896EXPORT_SYMBOL(snd_pcm_lib_write);
1897
Takashi Iwai877211f2005-11-17 13:59:38 +01001898static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 unsigned int hwoff,
1900 unsigned long data, unsigned int off,
1901 snd_pcm_uframes_t frames)
1902{
Takashi Iwai877211f2005-11-17 13:59:38 +01001903 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 int err;
1905 void __user **bufs = (void __user **)data;
1906 int channels = runtime->channels;
1907 int c;
1908 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001909 if (snd_BUG_ON(!substream->ops->silence))
1910 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 for (c = 0; c < channels; ++c, ++bufs) {
1912 if (*bufs == NULL) {
1913 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1914 return err;
1915 } else {
1916 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1917 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1918 return err;
1919 }
1920 }
1921 } else {
1922 /* default transfer behaviour */
1923 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 for (c = 0; c < channels; ++c, ++bufs) {
1925 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1926 if (*bufs == NULL) {
1927 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1928 } else {
1929 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1930 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1931 return -EFAULT;
1932 }
1933 }
1934 }
1935 return 0;
1936}
1937
Takashi Iwai877211f2005-11-17 13:59:38 +01001938snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 void __user **bufs,
1940 snd_pcm_uframes_t frames)
1941{
Takashi Iwai877211f2005-11-17 13:59:38 +01001942 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001944 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001946 err = pcm_sanity_check(substream);
1947 if (err < 0)
1948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001950 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1953 return -EINVAL;
1954 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1955 nonblock, snd_pcm_lib_writev_transfer);
1956}
1957
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001958EXPORT_SYMBOL(snd_pcm_lib_writev);
1959
Takashi Iwai877211f2005-11-17 13:59:38 +01001960static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 unsigned int hwoff,
1962 unsigned long data, unsigned int off,
1963 snd_pcm_uframes_t frames)
1964{
Takashi Iwai877211f2005-11-17 13:59:38 +01001965 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int err;
1967 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1968 if (substream->ops->copy) {
1969 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1970 return err;
1971 } else {
1972 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1974 return -EFAULT;
1975 }
1976 return 0;
1977}
1978
Takashi Iwai877211f2005-11-17 13:59:38 +01001979static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unsigned long data,
1981 snd_pcm_uframes_t size,
1982 int nonblock,
1983 transfer_f transfer)
1984{
Takashi Iwai877211f2005-11-17 13:59:38 +01001985 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 snd_pcm_uframes_t xfer = 0;
1987 snd_pcm_uframes_t offset = 0;
1988 int err = 0;
1989
1990 if (size == 0)
1991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 snd_pcm_stream_lock_irq(substream);
1994 switch (runtime->status->state) {
1995 case SNDRV_PCM_STATE_PREPARED:
1996 if (size >= runtime->start_threshold) {
1997 err = snd_pcm_start(substream);
1998 if (err < 0)
1999 goto _end_unlock;
2000 }
2001 break;
2002 case SNDRV_PCM_STATE_DRAINING:
2003 case SNDRV_PCM_STATE_RUNNING:
2004 case SNDRV_PCM_STATE_PAUSED:
2005 break;
2006 case SNDRV_PCM_STATE_XRUN:
2007 err = -EPIPE;
2008 goto _end_unlock;
2009 case SNDRV_PCM_STATE_SUSPENDED:
2010 err = -ESTRPIPE;
2011 goto _end_unlock;
2012 default:
2013 err = -EBADFD;
2014 goto _end_unlock;
2015 }
2016
Jaroslav Kysela12509322010-01-07 15:36:31 +01002017 runtime->nowake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 while (size > 0) {
2019 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2020 snd_pcm_uframes_t avail;
2021 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002022 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002025 if (!avail) {
2026 if (runtime->status->state ==
2027 SNDRV_PCM_STATE_DRAINING) {
2028 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 goto _end_unlock;
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (nonblock) {
2032 err = -EAGAIN;
2033 goto _end_unlock;
2034 }
Takashi Iwai13075512008-01-08 18:08:14 +01002035 err = wait_for_avail_min(substream, &avail);
2036 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002038 if (!avail)
2039 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 frames = size > avail ? avail : size;
2042 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2043 if (frames > cont)
2044 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002045 if (snd_BUG_ON(!frames)) {
Jaroslav Kysela12509322010-01-07 15:36:31 +01002046 runtime->nowake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002047 snd_pcm_stream_unlock_irq(substream);
2048 return -EINVAL;
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 appl_ptr = runtime->control->appl_ptr;
2051 appl_ofs = appl_ptr % runtime->buffer_size;
2052 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002053 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002055 if (err < 0)
2056 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 switch (runtime->status->state) {
2058 case SNDRV_PCM_STATE_XRUN:
2059 err = -EPIPE;
2060 goto _end_unlock;
2061 case SNDRV_PCM_STATE_SUSPENDED:
2062 err = -ESTRPIPE;
2063 goto _end_unlock;
2064 default:
2065 break;
2066 }
2067 appl_ptr += frames;
2068 if (appl_ptr >= runtime->boundary)
2069 appl_ptr -= runtime->boundary;
2070 runtime->control->appl_ptr = appl_ptr;
2071 if (substream->ops->ack)
2072 substream->ops->ack(substream);
2073
2074 offset += frames;
2075 size -= frames;
2076 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078 _end_unlock:
Jaroslav Kysela12509322010-01-07 15:36:31 +01002079 runtime->nowake = 0;
2080 if (xfer > 0 && err >= 0)
2081 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2084}
2085
Takashi Iwai877211f2005-11-17 13:59:38 +01002086snd_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 -07002087{
Takashi Iwai877211f2005-11-17 13:59:38 +01002088 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002090 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002092 err = pcm_sanity_check(substream);
2093 if (err < 0)
2094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002096 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2098 return -EINVAL;
2099 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2100}
2101
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002102EXPORT_SYMBOL(snd_pcm_lib_read);
2103
Takashi Iwai877211f2005-11-17 13:59:38 +01002104static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 unsigned int hwoff,
2106 unsigned long data, unsigned int off,
2107 snd_pcm_uframes_t frames)
2108{
Takashi Iwai877211f2005-11-17 13:59:38 +01002109 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 int err;
2111 void __user **bufs = (void __user **)data;
2112 int channels = runtime->channels;
2113 int c;
2114 if (substream->ops->copy) {
2115 for (c = 0; c < channels; ++c, ++bufs) {
2116 char __user *buf;
2117 if (*bufs == NULL)
2118 continue;
2119 buf = *bufs + samples_to_bytes(runtime, off);
2120 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2121 return err;
2122 }
2123 } else {
2124 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 for (c = 0; c < channels; ++c, ++bufs) {
2126 char *hwbuf;
2127 char __user *buf;
2128 if (*bufs == NULL)
2129 continue;
2130
2131 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2132 buf = *bufs + samples_to_bytes(runtime, off);
2133 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2134 return -EFAULT;
2135 }
2136 }
2137 return 0;
2138}
2139
Takashi Iwai877211f2005-11-17 13:59:38 +01002140snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 void __user **bufs,
2142 snd_pcm_uframes_t frames)
2143{
Takashi Iwai877211f2005-11-17 13:59:38 +01002144 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002146 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002148 err = pcm_sanity_check(substream);
2149 if (err < 0)
2150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2153 return -EBADFD;
2154
Takashi Iwai0df63e42006-04-28 15:13:41 +02002155 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2157 return -EINVAL;
2158 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161EXPORT_SYMBOL(snd_pcm_lib_readv);