blob: e2a817eac2a9a3a7d4087599a8e6c147120a8a3d [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 Kyselac91a9882010-01-21 10:32:15 +0100288 if (avail >= runtime->control->avail_min)
289 wake_up(runtime->twake ? &runtime->tsleep : &runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 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 */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100365 if (delta >= runtime->buffer_size + runtime->period_size) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100366 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;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100397 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100398 /* use loop to avoid checks for delta overflows */
399 /* the delta value is small or zero in most cases */
400 while (delta > 0) {
401 new_hw_ptr += runtime->period_size;
402 if (new_hw_ptr >= runtime->boundary)
403 new_hw_ptr -= runtime->boundary;
404 delta--;
405 }
406 /* align hw_base to buffer_size */
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200407 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100408 "hw_ptr skipping! %s"
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200409 "(pos=%ld, delta=%ld, period=%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100410 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
411 in_interrupt ? "[Q] " : "",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200412 (long)pos, (long)hdelta,
413 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100414 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100415 (unsigned long)old_hw_ptr,
416 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100417 /* reset values to proper state */
418 delta = 0;
419 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200420 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200421 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200422 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100423 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100424 "Lost interrupts? %s"
425 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
426 "old_hw_ptr=%ld)\n",
427 in_interrupt ? "[Q] " : "",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100428 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100429 (long)new_hw_ptr,
430 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100431 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100432
433 if (runtime->status->hw_ptr == new_hw_ptr)
434 return 0;
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
437 runtime->silence_size > 0)
438 snd_pcm_playback_silence(substream, new_hw_ptr);
439
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100440 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200442 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200443 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
444 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jaroslav Kysela12509322010-01-07 15:36:31 +0100446 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100450int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100452 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/**
456 * snd_pcm_set_ops - set the PCM operators
457 * @pcm: the pcm instance
458 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
459 * @ops: the operator table
460 *
461 * Sets the given PCM operators to the pcm instance.
462 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100463void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Takashi Iwai877211f2005-11-17 13:59:38 +0100465 struct snd_pcm_str *stream = &pcm->streams[direction];
466 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 for (substream = stream->substream; substream != NULL; substream = substream->next)
469 substream->ops = ops;
470}
471
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200472EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/**
475 * snd_pcm_sync - set the PCM sync id
476 * @substream: the pcm substream
477 *
478 * Sets the PCM sync identifier for the card.
479 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100480void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Takashi Iwai877211f2005-11-17 13:59:38 +0100482 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 runtime->sync.id32[0] = substream->pcm->card->number;
485 runtime->sync.id32[1] = -1;
486 runtime->sync.id32[2] = -1;
487 runtime->sync.id32[3] = -1;
488}
489
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200490EXPORT_SYMBOL(snd_pcm_set_sync);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * Standard ioctl routine
494 */
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static inline unsigned int div32(unsigned int a, unsigned int b,
497 unsigned int *r)
498{
499 if (b == 0) {
500 *r = 0;
501 return UINT_MAX;
502 }
503 *r = a % b;
504 return a / b;
505}
506
507static inline unsigned int div_down(unsigned int a, unsigned int b)
508{
509 if (b == 0)
510 return UINT_MAX;
511 return a / b;
512}
513
514static inline unsigned int div_up(unsigned int a, unsigned int b)
515{
516 unsigned int r;
517 unsigned int q;
518 if (b == 0)
519 return UINT_MAX;
520 q = div32(a, b, &r);
521 if (r)
522 ++q;
523 return q;
524}
525
526static inline unsigned int mul(unsigned int a, unsigned int b)
527{
528 if (a == 0)
529 return 0;
530 if (div_down(UINT_MAX, a) < b)
531 return UINT_MAX;
532 return a * b;
533}
534
535static inline unsigned int muldiv32(unsigned int a, unsigned int b,
536 unsigned int c, unsigned int *r)
537{
538 u_int64_t n = (u_int64_t) a * b;
539 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200540 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *r = 0;
542 return UINT_MAX;
543 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200544 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (n >= UINT_MAX) {
546 *r = 0;
547 return UINT_MAX;
548 }
549 return n;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/**
553 * snd_interval_refine - refine the interval value of configurator
554 * @i: the interval value to refine
555 * @v: the interval value to refer to
556 *
557 * Refines the interval value with the reference value.
558 * The interval is changed to the range satisfying both intervals.
559 * The interval status (min, max, integer, etc.) are evaluated.
560 *
561 * Returns non-zero if the value is changed, zero if not changed.
562 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100563int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200566 if (snd_BUG_ON(snd_interval_empty(i)))
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (i->min < v->min) {
569 i->min = v->min;
570 i->openmin = v->openmin;
571 changed = 1;
572 } else if (i->min == v->min && !i->openmin && v->openmin) {
573 i->openmin = 1;
574 changed = 1;
575 }
576 if (i->max > v->max) {
577 i->max = v->max;
578 i->openmax = v->openmax;
579 changed = 1;
580 } else if (i->max == v->max && !i->openmax && v->openmax) {
581 i->openmax = 1;
582 changed = 1;
583 }
584 if (!i->integer && v->integer) {
585 i->integer = 1;
586 changed = 1;
587 }
588 if (i->integer) {
589 if (i->openmin) {
590 i->min++;
591 i->openmin = 0;
592 }
593 if (i->openmax) {
594 i->max--;
595 i->openmax = 0;
596 }
597 } else if (!i->openmin && !i->openmax && i->min == i->max)
598 i->integer = 1;
599 if (snd_interval_checkempty(i)) {
600 snd_interval_none(i);
601 return -EINVAL;
602 }
603 return changed;
604}
605
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200606EXPORT_SYMBOL(snd_interval_refine);
607
Takashi Iwai877211f2005-11-17 13:59:38 +0100608static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200610 if (snd_BUG_ON(snd_interval_empty(i)))
611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (snd_interval_single(i))
613 return 0;
614 i->max = i->min;
615 i->openmax = i->openmin;
616 if (i->openmax)
617 i->max++;
618 return 1;
619}
620
Takashi Iwai877211f2005-11-17 13:59:38 +0100621static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200623 if (snd_BUG_ON(snd_interval_empty(i)))
624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (snd_interval_single(i))
626 return 0;
627 i->min = i->max;
628 i->openmin = i->openmax;
629 if (i->openmin)
630 i->min--;
631 return 1;
632}
633
Takashi Iwai877211f2005-11-17 13:59:38 +0100634void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 if (a->empty || b->empty) {
637 snd_interval_none(c);
638 return;
639 }
640 c->empty = 0;
641 c->min = mul(a->min, b->min);
642 c->openmin = (a->openmin || b->openmin);
643 c->max = mul(a->max, b->max);
644 c->openmax = (a->openmax || b->openmax);
645 c->integer = (a->integer && b->integer);
646}
647
648/**
649 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200650 * @a: dividend
651 * @b: divisor
652 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *
654 * c = a / b
655 *
656 * Returns non-zero if the value is changed, zero if not changed.
657 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100658void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 unsigned int r;
661 if (a->empty || b->empty) {
662 snd_interval_none(c);
663 return;
664 }
665 c->empty = 0;
666 c->min = div32(a->min, b->max, &r);
667 c->openmin = (r || a->openmin || b->openmax);
668 if (b->min > 0) {
669 c->max = div32(a->max, b->min, &r);
670 if (r) {
671 c->max++;
672 c->openmax = 1;
673 } else
674 c->openmax = (a->openmax || b->openmin);
675 } else {
676 c->max = UINT_MAX;
677 c->openmax = 0;
678 }
679 c->integer = 0;
680}
681
682/**
683 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200684 * @a: dividend 1
685 * @b: dividend 2
686 * @k: divisor (as integer)
687 * @c: result
688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * c = a * b / k
690 *
691 * Returns non-zero if the value is changed, zero if not changed.
692 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100693void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
694 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 unsigned int r;
697 if (a->empty || b->empty) {
698 snd_interval_none(c);
699 return;
700 }
701 c->empty = 0;
702 c->min = muldiv32(a->min, b->min, k, &r);
703 c->openmin = (r || a->openmin || b->openmin);
704 c->max = muldiv32(a->max, b->max, k, &r);
705 if (r) {
706 c->max++;
707 c->openmax = 1;
708 } else
709 c->openmax = (a->openmax || b->openmax);
710 c->integer = 0;
711}
712
713/**
714 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200715 * @a: dividend 1
716 * @k: dividend 2 (as integer)
717 * @b: divisor
718 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *
720 * c = a * k / b
721 *
722 * Returns non-zero if the value is changed, zero if not changed.
723 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100724void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
725 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 unsigned int r;
728 if (a->empty || b->empty) {
729 snd_interval_none(c);
730 return;
731 }
732 c->empty = 0;
733 c->min = muldiv32(a->min, k, b->max, &r);
734 c->openmin = (r || a->openmin || b->openmax);
735 if (b->min > 0) {
736 c->max = muldiv32(a->max, k, b->min, &r);
737 if (r) {
738 c->max++;
739 c->openmax = 1;
740 } else
741 c->openmax = (a->openmax || b->openmin);
742 } else {
743 c->max = UINT_MAX;
744 c->openmax = 0;
745 }
746 c->integer = 0;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/* ---- */
750
751
752/**
753 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200754 * @i: interval to refine
755 * @rats_count: number of ratnum_t
756 * @rats: ratnum_t array
757 * @nump: pointer to store the resultant numerator
758 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
760 * Returns non-zero if the value is changed, zero if not changed.
761 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100762int snd_interval_ratnum(struct snd_interval *i,
763 unsigned int rats_count, struct snd_ratnum *rats,
764 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 unsigned int best_num, best_diff, best_den;
767 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100768 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int err;
770
771 best_num = best_den = best_diff = 0;
772 for (k = 0; k < rats_count; ++k) {
773 unsigned int num = rats[k].num;
774 unsigned int den;
775 unsigned int q = i->min;
776 int diff;
777 if (q == 0)
778 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100779 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (den < rats[k].den_min)
781 continue;
782 if (den > rats[k].den_max)
783 den = rats[k].den_max;
784 else {
785 unsigned int r;
786 r = (den - rats[k].den_min) % rats[k].den_step;
787 if (r != 0)
788 den -= r;
789 }
790 diff = num - q * den;
791 if (best_num == 0 ||
792 diff * best_den < best_diff * den) {
793 best_diff = diff;
794 best_den = den;
795 best_num = num;
796 }
797 }
798 if (best_den == 0) {
799 i->empty = 1;
800 return -EINVAL;
801 }
802 t.min = div_down(best_num, best_den);
803 t.openmin = !!(best_num % best_den);
804
805 best_num = best_den = best_diff = 0;
806 for (k = 0; k < rats_count; ++k) {
807 unsigned int num = rats[k].num;
808 unsigned int den;
809 unsigned int q = i->max;
810 int diff;
811 if (q == 0) {
812 i->empty = 1;
813 return -EINVAL;
814 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100815 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (den > rats[k].den_max)
817 continue;
818 if (den < rats[k].den_min)
819 den = rats[k].den_min;
820 else {
821 unsigned int r;
822 r = (den - rats[k].den_min) % rats[k].den_step;
823 if (r != 0)
824 den += rats[k].den_step - r;
825 }
826 diff = q * den - num;
827 if (best_num == 0 ||
828 diff * best_den < best_diff * den) {
829 best_diff = diff;
830 best_den = den;
831 best_num = num;
832 }
833 }
834 if (best_den == 0) {
835 i->empty = 1;
836 return -EINVAL;
837 }
838 t.max = div_up(best_num, best_den);
839 t.openmax = !!(best_num % best_den);
840 t.integer = 0;
841 err = snd_interval_refine(i, &t);
842 if (err < 0)
843 return err;
844
845 if (snd_interval_single(i)) {
846 if (nump)
847 *nump = best_num;
848 if (denp)
849 *denp = best_den;
850 }
851 return err;
852}
853
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200854EXPORT_SYMBOL(snd_interval_ratnum);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/**
857 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200858 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100859 * @rats_count: number of struct ratden
860 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200861 * @nump: pointer to store the resultant numerator
862 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
864 * Returns non-zero if the value is changed, zero if not changed.
865 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100866static int snd_interval_ratden(struct snd_interval *i,
867 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int *nump, unsigned int *denp)
869{
870 unsigned int best_num, best_diff, best_den;
871 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int err;
874
875 best_num = best_den = best_diff = 0;
876 for (k = 0; k < rats_count; ++k) {
877 unsigned int num;
878 unsigned int den = rats[k].den;
879 unsigned int q = i->min;
880 int diff;
881 num = mul(q, den);
882 if (num > rats[k].num_max)
883 continue;
884 if (num < rats[k].num_min)
885 num = rats[k].num_max;
886 else {
887 unsigned int r;
888 r = (num - rats[k].num_min) % rats[k].num_step;
889 if (r != 0)
890 num += rats[k].num_step - r;
891 }
892 diff = num - q * den;
893 if (best_num == 0 ||
894 diff * best_den < best_diff * den) {
895 best_diff = diff;
896 best_den = den;
897 best_num = num;
898 }
899 }
900 if (best_den == 0) {
901 i->empty = 1;
902 return -EINVAL;
903 }
904 t.min = div_down(best_num, best_den);
905 t.openmin = !!(best_num % best_den);
906
907 best_num = best_den = best_diff = 0;
908 for (k = 0; k < rats_count; ++k) {
909 unsigned int num;
910 unsigned int den = rats[k].den;
911 unsigned int q = i->max;
912 int diff;
913 num = mul(q, den);
914 if (num < rats[k].num_min)
915 continue;
916 if (num > rats[k].num_max)
917 num = rats[k].num_max;
918 else {
919 unsigned int r;
920 r = (num - rats[k].num_min) % rats[k].num_step;
921 if (r != 0)
922 num -= r;
923 }
924 diff = q * den - num;
925 if (best_num == 0 ||
926 diff * best_den < best_diff * den) {
927 best_diff = diff;
928 best_den = den;
929 best_num = num;
930 }
931 }
932 if (best_den == 0) {
933 i->empty = 1;
934 return -EINVAL;
935 }
936 t.max = div_up(best_num, best_den);
937 t.openmax = !!(best_num % best_den);
938 t.integer = 0;
939 err = snd_interval_refine(i, &t);
940 if (err < 0)
941 return err;
942
943 if (snd_interval_single(i)) {
944 if (nump)
945 *nump = best_num;
946 if (denp)
947 *denp = best_den;
948 }
949 return err;
950}
951
952/**
953 * snd_interval_list - refine the interval value from the list
954 * @i: the interval value to refine
955 * @count: the number of elements in the list
956 * @list: the value list
957 * @mask: the bit-mask to evaluate
958 *
959 * Refines the interval value from the list.
960 * When mask is non-zero, only the elements corresponding to bit 1 are
961 * evaluated.
962 *
963 * Returns non-zero if the value is changed, zero if not changed.
964 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100965int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200968 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +0100969
970 if (!count) {
971 i->empty = 1;
972 return -EINVAL;
973 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200974 snd_interval_any(&list_range);
975 list_range.min = UINT_MAX;
976 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 for (k = 0; k < count; k++) {
978 if (mask && !(mask & (1 << k)))
979 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200980 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200982 list_range.min = min(list_range.min, list[k]);
983 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +0200985 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200988EXPORT_SYMBOL(snd_interval_list);
989
Takashi Iwai877211f2005-11-17 13:59:38 +0100990static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 unsigned int n;
993 int changed = 0;
994 n = (i->min - min) % step;
995 if (n != 0 || i->openmin) {
996 i->min += step - n;
997 changed = 1;
998 }
999 n = (i->max - min) % step;
1000 if (n != 0 || i->openmax) {
1001 i->max -= n;
1002 changed = 1;
1003 }
1004 if (snd_interval_checkempty(i)) {
1005 i->empty = 1;
1006 return -EINVAL;
1007 }
1008 return changed;
1009}
1010
1011/* Info constraints helpers */
1012
1013/**
1014 * snd_pcm_hw_rule_add - add the hw-constraint rule
1015 * @runtime: the pcm runtime instance
1016 * @cond: condition bits
1017 * @var: the variable to evaluate
1018 * @func: the evaluation function
1019 * @private: the private data pointer passed to function
1020 * @dep: the dependent variables
1021 *
1022 * Returns zero if successful, or a negative error code on failure.
1023 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001024int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int var,
1026 snd_pcm_hw_rule_func_t func, void *private,
1027 int dep, ...)
1028{
Takashi Iwai877211f2005-11-17 13:59:38 +01001029 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1030 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 unsigned int k;
1032 va_list args;
1033 va_start(args, dep);
1034 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001035 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned int new_rules = constrs->rules_all + 16;
1037 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1038 if (!new)
1039 return -ENOMEM;
1040 if (constrs->rules) {
1041 memcpy(new, constrs->rules,
1042 constrs->rules_num * sizeof(*c));
1043 kfree(constrs->rules);
1044 }
1045 constrs->rules = new;
1046 constrs->rules_all = new_rules;
1047 }
1048 c = &constrs->rules[constrs->rules_num];
1049 c->cond = cond;
1050 c->func = func;
1051 c->var = var;
1052 c->private = private;
1053 k = 0;
1054 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001055 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1056 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 c->deps[k++] = dep;
1058 if (dep < 0)
1059 break;
1060 dep = va_arg(args, int);
1061 }
1062 constrs->rules_num++;
1063 va_end(args);
1064 return 0;
1065}
1066
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001067EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001070 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001071 * @runtime: PCM runtime instance
1072 * @var: hw_params variable to apply the mask
1073 * @mask: the bitmap mask
1074 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001075 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001077int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 u_int32_t mask)
1079{
Takashi Iwai877211f2005-11-17 13:59:38 +01001080 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1081 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *maskp->bits &= mask;
1083 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1084 if (*maskp->bits == 0)
1085 return -EINVAL;
1086 return 0;
1087}
1088
1089/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001090 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001091 * @runtime: PCM runtime instance
1092 * @var: hw_params variable to apply the mask
1093 * @mask: the 64bit bitmap mask
1094 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001095 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001097int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 u_int64_t mask)
1099{
Takashi Iwai877211f2005-11-17 13:59:38 +01001100 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1101 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 maskp->bits[0] &= (u_int32_t)mask;
1103 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1104 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1105 if (! maskp->bits[0] && ! maskp->bits[1])
1106 return -EINVAL;
1107 return 0;
1108}
1109
1110/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001111 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001112 * @runtime: PCM runtime instance
1113 * @var: hw_params variable to apply the integer constraint
1114 *
1115 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001117int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return snd_interval_setinteger(constrs_interval(constrs, var));
1121}
1122
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001123EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001126 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001127 * @runtime: PCM runtime instance
1128 * @var: hw_params variable to apply the range
1129 * @min: the minimal value
1130 * @max: the maximal value
1131 *
1132 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001134int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned int min, unsigned int max)
1136{
Takashi Iwai877211f2005-11-17 13:59:38 +01001137 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1138 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 t.min = min;
1140 t.max = max;
1141 t.openmin = t.openmax = 0;
1142 t.integer = 0;
1143 return snd_interval_refine(constrs_interval(constrs, var), &t);
1144}
1145
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001146EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1147
Takashi Iwai877211f2005-11-17 13:59:38 +01001148static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1149 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwai877211f2005-11-17 13:59:38 +01001151 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1153}
1154
1155
1156/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001157 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001158 * @runtime: PCM runtime instance
1159 * @cond: condition bits
1160 * @var: hw_params variable to apply the list constraint
1161 * @l: list
1162 *
1163 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001165int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned int cond,
1167 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001168 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 return snd_pcm_hw_rule_add(runtime, cond, var,
1171 snd_pcm_hw_rule_list, l,
1172 var, -1);
1173}
1174
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001175EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1176
Takashi Iwai877211f2005-11-17 13:59:38 +01001177static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1178 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Takashi Iwai877211f2005-11-17 13:59:38 +01001180 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned int num = 0, den = 0;
1182 int err;
1183 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1184 r->nrats, r->rats, &num, &den);
1185 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1186 params->rate_num = num;
1187 params->rate_den = den;
1188 }
1189 return err;
1190}
1191
1192/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001193 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001194 * @runtime: PCM runtime instance
1195 * @cond: condition bits
1196 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001197 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001199int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 unsigned int cond,
1201 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001202 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 return snd_pcm_hw_rule_add(runtime, cond, var,
1205 snd_pcm_hw_rule_ratnums, r,
1206 var, -1);
1207}
1208
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001209EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1210
Takashi Iwai877211f2005-11-17 13:59:38 +01001211static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1212 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai877211f2005-11-17 13:59:38 +01001214 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 unsigned int num = 0, den = 0;
1216 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1217 r->nrats, r->rats, &num, &den);
1218 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1219 params->rate_num = num;
1220 params->rate_den = den;
1221 }
1222 return err;
1223}
1224
1225/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001226 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001227 * @runtime: PCM runtime instance
1228 * @cond: condition bits
1229 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001230 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001232int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned int cond,
1234 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001235 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 return snd_pcm_hw_rule_add(runtime, cond, var,
1238 snd_pcm_hw_rule_ratdens, r,
1239 var, -1);
1240}
1241
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001242EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1243
Takashi Iwai877211f2005-11-17 13:59:38 +01001244static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1245 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 unsigned int l = (unsigned long) rule->private;
1248 int width = l & 0xffff;
1249 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (snd_interval_single(i) && snd_interval_value(i) == width)
1252 params->msbits = msbits;
1253 return 0;
1254}
1255
1256/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001257 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001258 * @runtime: PCM runtime instance
1259 * @cond: condition bits
1260 * @width: sample bits width
1261 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001263int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned int cond,
1265 unsigned int width,
1266 unsigned int msbits)
1267{
1268 unsigned long l = (msbits << 16) | width;
1269 return snd_pcm_hw_rule_add(runtime, cond, -1,
1270 snd_pcm_hw_rule_msbits,
1271 (void*) l,
1272 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1273}
1274
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001275EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1276
Takashi Iwai877211f2005-11-17 13:59:38 +01001277static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1278 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 unsigned long step = (unsigned long) rule->private;
1281 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1282}
1283
1284/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001285 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001286 * @runtime: PCM runtime instance
1287 * @cond: condition bits
1288 * @var: hw_params variable to apply the step constraint
1289 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001291int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 unsigned int cond,
1293 snd_pcm_hw_param_t var,
1294 unsigned long step)
1295{
1296 return snd_pcm_hw_rule_add(runtime, cond, var,
1297 snd_pcm_hw_rule_step, (void *) step,
1298 var, -1);
1299}
1300
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001301EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1302
Takashi Iwai877211f2005-11-17 13:59:38 +01001303static 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 -07001304{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001305 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1307 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1308 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1309 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1310 };
1311 return snd_interval_list(hw_param_interval(params, rule->var),
1312 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1313}
1314
1315/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001316 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001317 * @runtime: PCM runtime instance
1318 * @cond: condition bits
1319 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001321int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 unsigned int cond,
1323 snd_pcm_hw_param_t var)
1324{
1325 return snd_pcm_hw_rule_add(runtime, cond, var,
1326 snd_pcm_hw_rule_pow2, NULL,
1327 var, -1);
1328}
1329
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001330EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1331
Takashi Iwai877211f2005-11-17 13:59:38 +01001332static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001333 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 if (hw_is_mask(var)) {
1336 snd_mask_any(hw_param_mask(params, var));
1337 params->cmask |= 1 << var;
1338 params->rmask |= 1 << var;
1339 return;
1340 }
1341 if (hw_is_interval(var)) {
1342 snd_interval_any(hw_param_interval(params, var));
1343 params->cmask |= 1 << var;
1344 params->rmask |= 1 << var;
1345 return;
1346 }
1347 snd_BUG();
1348}
1349
Takashi Iwai877211f2005-11-17 13:59:38 +01001350void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 unsigned int k;
1353 memset(params, 0, sizeof(*params));
1354 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1355 _snd_pcm_hw_param_any(params, k);
1356 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1357 _snd_pcm_hw_param_any(params, k);
1358 params->info = ~0U;
1359}
1360
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001361EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001364 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001365 * @params: the hw_params instance
1366 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001367 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001369 * Return the value for field @var if it's fixed in configuration space
1370 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001372int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1373 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001376 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (!snd_mask_single(mask))
1378 return -EINVAL;
1379 if (dir)
1380 *dir = 0;
1381 return snd_mask_value(mask);
1382 }
1383 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001384 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!snd_interval_single(i))
1386 return -EINVAL;
1387 if (dir)
1388 *dir = i->openmin;
1389 return snd_interval_value(i);
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EINVAL;
1392}
1393
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001394EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Takashi Iwai877211f2005-11-17 13:59:38 +01001396void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 snd_pcm_hw_param_t var)
1398{
1399 if (hw_is_mask(var)) {
1400 snd_mask_none(hw_param_mask(params, var));
1401 params->cmask |= 1 << var;
1402 params->rmask |= 1 << var;
1403 } else if (hw_is_interval(var)) {
1404 snd_interval_none(hw_param_interval(params, var));
1405 params->cmask |= 1 << var;
1406 params->rmask |= 1 << var;
1407 } else {
1408 snd_BUG();
1409 }
1410}
1411
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001412EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Takashi Iwai877211f2005-11-17 13:59:38 +01001414static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001415 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 int changed;
1418 if (hw_is_mask(var))
1419 changed = snd_mask_refine_first(hw_param_mask(params, var));
1420 else if (hw_is_interval(var))
1421 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001422 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (changed) {
1425 params->cmask |= 1 << var;
1426 params->rmask |= 1 << var;
1427 }
1428 return changed;
1429}
1430
1431
1432/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001433 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001434 * @pcm: PCM instance
1435 * @params: the hw_params instance
1436 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001437 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001439 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * values > minimum. Reduce configuration space accordingly.
1441 * Return the minimum.
1442 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001443int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1444 struct snd_pcm_hw_params *params,
1445 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 int changed = _snd_pcm_hw_param_first(params, var);
1448 if (changed < 0)
1449 return changed;
1450 if (params->rmask) {
1451 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001452 if (snd_BUG_ON(err < 0))
1453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 return snd_pcm_hw_param_value(params, var, dir);
1456}
1457
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001458EXPORT_SYMBOL(snd_pcm_hw_param_first);
1459
Takashi Iwai877211f2005-11-17 13:59:38 +01001460static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001461 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 int changed;
1464 if (hw_is_mask(var))
1465 changed = snd_mask_refine_last(hw_param_mask(params, var));
1466 else if (hw_is_interval(var))
1467 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001468 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (changed) {
1471 params->cmask |= 1 << var;
1472 params->rmask |= 1 << var;
1473 }
1474 return changed;
1475}
1476
1477
1478/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001479 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001480 * @pcm: PCM instance
1481 * @params: the hw_params instance
1482 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001483 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001485 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * values < maximum. Reduce configuration space accordingly.
1487 * Return the maximum.
1488 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001489int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1490 struct snd_pcm_hw_params *params,
1491 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int changed = _snd_pcm_hw_param_last(params, var);
1494 if (changed < 0)
1495 return changed;
1496 if (params->rmask) {
1497 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001498 if (snd_BUG_ON(err < 0))
1499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 return snd_pcm_hw_param_value(params, var, dir);
1502}
1503
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001504EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001507 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001508 * @pcm: PCM instance
1509 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001511 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * The configuration chosen is that obtained fixing in this order:
1513 * first access, first format, first subformat, min channels,
1514 * min rate, min period time, max buffer size, min tick time
1515 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001516int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1517 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001519 static int vars[] = {
1520 SNDRV_PCM_HW_PARAM_ACCESS,
1521 SNDRV_PCM_HW_PARAM_FORMAT,
1522 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1523 SNDRV_PCM_HW_PARAM_CHANNELS,
1524 SNDRV_PCM_HW_PARAM_RATE,
1525 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1526 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1527 SNDRV_PCM_HW_PARAM_TICK_TIME,
1528 -1
1529 };
1530 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001532 for (v = vars; *v != -1; v++) {
1533 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1534 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1535 else
1536 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001537 if (snd_BUG_ON(err < 0))
1538 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 0;
1541}
1542
Takashi Iwai877211f2005-11-17 13:59:38 +01001543static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 void *arg)
1545{
Takashi Iwai877211f2005-11-17 13:59:38 +01001546 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 unsigned long flags;
1548 snd_pcm_stream_lock_irqsave(substream, flags);
1549 if (snd_pcm_running(substream) &&
1550 snd_pcm_update_hw_ptr(substream) >= 0)
1551 runtime->status->hw_ptr %= runtime->buffer_size;
1552 else
1553 runtime->status->hw_ptr = 0;
1554 snd_pcm_stream_unlock_irqrestore(substream, flags);
1555 return 0;
1556}
1557
Takashi Iwai877211f2005-11-17 13:59:38 +01001558static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 void *arg)
1560{
Takashi Iwai877211f2005-11-17 13:59:38 +01001561 struct snd_pcm_channel_info *info = arg;
1562 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int width;
1564 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1565 info->offset = -1;
1566 return 0;
1567 }
1568 width = snd_pcm_format_physical_width(runtime->format);
1569 if (width < 0)
1570 return width;
1571 info->offset = 0;
1572 switch (runtime->access) {
1573 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1574 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1575 info->first = info->channel * width;
1576 info->step = runtime->channels * width;
1577 break;
1578 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1579 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1580 {
1581 size_t size = runtime->dma_bytes / runtime->channels;
1582 info->first = info->channel * size * 8;
1583 info->step = width;
1584 break;
1585 }
1586 default:
1587 snd_BUG();
1588 break;
1589 }
1590 return 0;
1591}
1592
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001593static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1594 void *arg)
1595{
1596 struct snd_pcm_hw_params *params = arg;
1597 snd_pcm_format_t format;
1598 int channels, width;
1599
1600 params->fifo_size = substream->runtime->hw.fifo_size;
1601 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1602 format = params_format(params);
1603 channels = params_channels(params);
1604 width = snd_pcm_format_physical_width(format);
1605 params->fifo_size /= width * channels;
1606 }
1607 return 0;
1608}
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/**
1611 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1612 * @substream: the pcm substream instance
1613 * @cmd: ioctl command
1614 * @arg: ioctl argument
1615 *
1616 * Processes the generic ioctl commands for PCM.
1617 * Can be passed as the ioctl callback for PCM ops.
1618 *
1619 * Returns zero if successful, or a negative error code on failure.
1620 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001621int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned int cmd, void *arg)
1623{
1624 switch (cmd) {
1625 case SNDRV_PCM_IOCTL1_INFO:
1626 return 0;
1627 case SNDRV_PCM_IOCTL1_RESET:
1628 return snd_pcm_lib_ioctl_reset(substream, arg);
1629 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1630 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001631 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1632 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 return -ENXIO;
1635}
1636
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001637EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/**
1640 * snd_pcm_period_elapsed - update the pcm status for the next period
1641 * @substream: the pcm substream instance
1642 *
1643 * This function is called from the interrupt handler when the
1644 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001645 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 *
1647 * Even if more than one periods have elapsed since the last call, you
1648 * have to call this only once.
1649 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001650void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Takashi Iwai877211f2005-11-17 13:59:38 +01001652 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 unsigned long flags;
1654
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001655 if (PCM_RUNTIME_CHECK(substream))
1656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (runtime->transfer_ack_begin)
1660 runtime->transfer_ack_begin(substream);
1661
1662 snd_pcm_stream_lock_irqsave(substream, flags);
1663 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001664 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto _end;
1666
1667 if (substream->timer_running)
1668 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 _end:
1670 snd_pcm_stream_unlock_irqrestore(substream, flags);
1671 if (runtime->transfer_ack_end)
1672 runtime->transfer_ack_end(substream);
1673 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1674}
1675
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001676EXPORT_SYMBOL(snd_pcm_period_elapsed);
1677
Takashi Iwai13075512008-01-08 18:08:14 +01001678/*
1679 * Wait until avail_min data becomes available
1680 * Returns a negative error code if any error occurs during operation.
1681 * The available space is stored on availp. When err = 0 and avail = 0
1682 * on the capture stream, it indicates the stream is in DRAINING state.
1683 */
1684static int wait_for_avail_min(struct snd_pcm_substream *substream,
1685 snd_pcm_uframes_t *availp)
1686{
1687 struct snd_pcm_runtime *runtime = substream->runtime;
1688 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1689 wait_queue_t wait;
1690 int err = 0;
1691 snd_pcm_uframes_t avail = 0;
1692 long tout;
1693
1694 init_waitqueue_entry(&wait, current);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001695 add_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001696 for (;;) {
1697 if (signal_pending(current)) {
1698 err = -ERESTARTSYS;
1699 break;
1700 }
1701 set_current_state(TASK_INTERRUPTIBLE);
1702 snd_pcm_stream_unlock_irq(substream);
1703 tout = schedule_timeout(msecs_to_jiffies(10000));
1704 snd_pcm_stream_lock_irq(substream);
1705 switch (runtime->status->state) {
1706 case SNDRV_PCM_STATE_SUSPENDED:
1707 err = -ESTRPIPE;
1708 goto _endloop;
1709 case SNDRV_PCM_STATE_XRUN:
1710 err = -EPIPE;
1711 goto _endloop;
1712 case SNDRV_PCM_STATE_DRAINING:
1713 if (is_playback)
1714 err = -EPIPE;
1715 else
1716 avail = 0; /* indicate draining */
1717 goto _endloop;
1718 case SNDRV_PCM_STATE_OPEN:
1719 case SNDRV_PCM_STATE_SETUP:
1720 case SNDRV_PCM_STATE_DISCONNECTED:
1721 err = -EBADFD;
1722 goto _endloop;
1723 }
1724 if (!tout) {
1725 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1726 is_playback ? "playback" : "capture");
1727 err = -EIO;
1728 break;
1729 }
1730 if (is_playback)
1731 avail = snd_pcm_playback_avail(runtime);
1732 else
1733 avail = snd_pcm_capture_avail(runtime);
1734 if (avail >= runtime->control->avail_min)
1735 break;
1736 }
1737 _endloop:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001738 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001739 *availp = avail;
1740 return err;
1741}
1742
Takashi Iwai877211f2005-11-17 13:59:38 +01001743static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 unsigned int hwoff,
1745 unsigned long data, unsigned int off,
1746 snd_pcm_uframes_t frames)
1747{
Takashi Iwai877211f2005-11-17 13:59:38 +01001748 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int err;
1750 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1751 if (substream->ops->copy) {
1752 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1753 return err;
1754 } else {
1755 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1757 return -EFAULT;
1758 }
1759 return 0;
1760}
1761
Takashi Iwai877211f2005-11-17 13:59:38 +01001762typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 unsigned long data, unsigned int off,
1764 snd_pcm_uframes_t size);
1765
Takashi Iwai877211f2005-11-17 13:59:38 +01001766static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 unsigned long data,
1768 snd_pcm_uframes_t size,
1769 int nonblock,
1770 transfer_f transfer)
1771{
Takashi Iwai877211f2005-11-17 13:59:38 +01001772 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 snd_pcm_uframes_t xfer = 0;
1774 snd_pcm_uframes_t offset = 0;
1775 int err = 0;
1776
1777 if (size == 0)
1778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 snd_pcm_stream_lock_irq(substream);
1781 switch (runtime->status->state) {
1782 case SNDRV_PCM_STATE_PREPARED:
1783 case SNDRV_PCM_STATE_RUNNING:
1784 case SNDRV_PCM_STATE_PAUSED:
1785 break;
1786 case SNDRV_PCM_STATE_XRUN:
1787 err = -EPIPE;
1788 goto _end_unlock;
1789 case SNDRV_PCM_STATE_SUSPENDED:
1790 err = -ESTRPIPE;
1791 goto _end_unlock;
1792 default:
1793 err = -EBADFD;
1794 goto _end_unlock;
1795 }
1796
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001797 runtime->twake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 while (size > 0) {
1799 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1800 snd_pcm_uframes_t avail;
1801 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001802 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 snd_pcm_update_hw_ptr(substream);
1804 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001805 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (nonblock) {
1807 err = -EAGAIN;
1808 goto _end_unlock;
1809 }
Takashi Iwai13075512008-01-08 18:08:14 +01001810 err = wait_for_avail_min(substream, &avail);
1811 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 frames = size > avail ? avail : size;
1815 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1816 if (frames > cont)
1817 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001818 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001819 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001820 snd_pcm_stream_unlock_irq(substream);
1821 return -EINVAL;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 appl_ptr = runtime->control->appl_ptr;
1824 appl_ofs = appl_ptr % runtime->buffer_size;
1825 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001826 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001828 if (err < 0)
1829 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 switch (runtime->status->state) {
1831 case SNDRV_PCM_STATE_XRUN:
1832 err = -EPIPE;
1833 goto _end_unlock;
1834 case SNDRV_PCM_STATE_SUSPENDED:
1835 err = -ESTRPIPE;
1836 goto _end_unlock;
1837 default:
1838 break;
1839 }
1840 appl_ptr += frames;
1841 if (appl_ptr >= runtime->boundary)
1842 appl_ptr -= runtime->boundary;
1843 runtime->control->appl_ptr = appl_ptr;
1844 if (substream->ops->ack)
1845 substream->ops->ack(substream);
1846
1847 offset += frames;
1848 size -= frames;
1849 xfer += frames;
1850 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1851 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1852 err = snd_pcm_start(substream);
1853 if (err < 0)
1854 goto _end_unlock;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001858 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01001859 if (xfer > 0 && err >= 0)
1860 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1863}
1864
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001865/* sanity-check for read/write methods */
1866static int pcm_sanity_check(struct snd_pcm_substream *substream)
1867{
1868 struct snd_pcm_runtime *runtime;
1869 if (PCM_RUNTIME_CHECK(substream))
1870 return -ENXIO;
1871 runtime = substream->runtime;
1872 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1873 return -EINVAL;
1874 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1875 return -EBADFD;
1876 return 0;
1877}
1878
Takashi Iwai877211f2005-11-17 13:59:38 +01001879snd_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 -07001880{
Takashi Iwai877211f2005-11-17 13:59:38 +01001881 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001883 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001885 err = pcm_sanity_check(substream);
1886 if (err < 0)
1887 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001889 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1892 runtime->channels > 1)
1893 return -EINVAL;
1894 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1895 snd_pcm_lib_write_transfer);
1896}
1897
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001898EXPORT_SYMBOL(snd_pcm_lib_write);
1899
Takashi Iwai877211f2005-11-17 13:59:38 +01001900static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 unsigned int hwoff,
1902 unsigned long data, unsigned int off,
1903 snd_pcm_uframes_t frames)
1904{
Takashi Iwai877211f2005-11-17 13:59:38 +01001905 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int err;
1907 void __user **bufs = (void __user **)data;
1908 int channels = runtime->channels;
1909 int c;
1910 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001911 if (snd_BUG_ON(!substream->ops->silence))
1912 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 for (c = 0; c < channels; ++c, ++bufs) {
1914 if (*bufs == NULL) {
1915 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1916 return err;
1917 } else {
1918 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1919 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1920 return err;
1921 }
1922 }
1923 } else {
1924 /* default transfer behaviour */
1925 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 for (c = 0; c < channels; ++c, ++bufs) {
1927 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1928 if (*bufs == NULL) {
1929 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1930 } else {
1931 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1932 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1933 return -EFAULT;
1934 }
1935 }
1936 }
1937 return 0;
1938}
1939
Takashi Iwai877211f2005-11-17 13:59:38 +01001940snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 void __user **bufs,
1942 snd_pcm_uframes_t frames)
1943{
Takashi Iwai877211f2005-11-17 13:59:38 +01001944 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001946 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001948 err = pcm_sanity_check(substream);
1949 if (err < 0)
1950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001952 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1955 return -EINVAL;
1956 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1957 nonblock, snd_pcm_lib_writev_transfer);
1958}
1959
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001960EXPORT_SYMBOL(snd_pcm_lib_writev);
1961
Takashi Iwai877211f2005-11-17 13:59:38 +01001962static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 unsigned int hwoff,
1964 unsigned long data, unsigned int off,
1965 snd_pcm_uframes_t frames)
1966{
Takashi Iwai877211f2005-11-17 13:59:38 +01001967 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int err;
1969 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1970 if (substream->ops->copy) {
1971 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1972 return err;
1973 } else {
1974 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1976 return -EFAULT;
1977 }
1978 return 0;
1979}
1980
Takashi Iwai877211f2005-11-17 13:59:38 +01001981static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 unsigned long data,
1983 snd_pcm_uframes_t size,
1984 int nonblock,
1985 transfer_f transfer)
1986{
Takashi Iwai877211f2005-11-17 13:59:38 +01001987 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 snd_pcm_uframes_t xfer = 0;
1989 snd_pcm_uframes_t offset = 0;
1990 int err = 0;
1991
1992 if (size == 0)
1993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 snd_pcm_stream_lock_irq(substream);
1996 switch (runtime->status->state) {
1997 case SNDRV_PCM_STATE_PREPARED:
1998 if (size >= runtime->start_threshold) {
1999 err = snd_pcm_start(substream);
2000 if (err < 0)
2001 goto _end_unlock;
2002 }
2003 break;
2004 case SNDRV_PCM_STATE_DRAINING:
2005 case SNDRV_PCM_STATE_RUNNING:
2006 case SNDRV_PCM_STATE_PAUSED:
2007 break;
2008 case SNDRV_PCM_STATE_XRUN:
2009 err = -EPIPE;
2010 goto _end_unlock;
2011 case SNDRV_PCM_STATE_SUSPENDED:
2012 err = -ESTRPIPE;
2013 goto _end_unlock;
2014 default:
2015 err = -EBADFD;
2016 goto _end_unlock;
2017 }
2018
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002019 runtime->twake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 while (size > 0) {
2021 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2022 snd_pcm_uframes_t avail;
2023 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002024 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002027 if (!avail) {
2028 if (runtime->status->state ==
2029 SNDRV_PCM_STATE_DRAINING) {
2030 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto _end_unlock;
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (nonblock) {
2034 err = -EAGAIN;
2035 goto _end_unlock;
2036 }
Takashi Iwai13075512008-01-08 18:08:14 +01002037 err = wait_for_avail_min(substream, &avail);
2038 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002040 if (!avail)
2041 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 frames = size > avail ? avail : size;
2044 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2045 if (frames > cont)
2046 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002047 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002048 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002049 snd_pcm_stream_unlock_irq(substream);
2050 return -EINVAL;
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 appl_ptr = runtime->control->appl_ptr;
2053 appl_ofs = appl_ptr % runtime->buffer_size;
2054 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002055 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002057 if (err < 0)
2058 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 switch (runtime->status->state) {
2060 case SNDRV_PCM_STATE_XRUN:
2061 err = -EPIPE;
2062 goto _end_unlock;
2063 case SNDRV_PCM_STATE_SUSPENDED:
2064 err = -ESTRPIPE;
2065 goto _end_unlock;
2066 default:
2067 break;
2068 }
2069 appl_ptr += frames;
2070 if (appl_ptr >= runtime->boundary)
2071 appl_ptr -= runtime->boundary;
2072 runtime->control->appl_ptr = appl_ptr;
2073 if (substream->ops->ack)
2074 substream->ops->ack(substream);
2075
2076 offset += frames;
2077 size -= frames;
2078 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002081 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002082 if (xfer > 0 && err >= 0)
2083 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2086}
2087
Takashi Iwai877211f2005-11-17 13:59:38 +01002088snd_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 -07002089{
Takashi Iwai877211f2005-11-17 13:59:38 +01002090 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002092 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002094 err = pcm_sanity_check(substream);
2095 if (err < 0)
2096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002098 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2100 return -EINVAL;
2101 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2102}
2103
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002104EXPORT_SYMBOL(snd_pcm_lib_read);
2105
Takashi Iwai877211f2005-11-17 13:59:38 +01002106static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 unsigned int hwoff,
2108 unsigned long data, unsigned int off,
2109 snd_pcm_uframes_t frames)
2110{
Takashi Iwai877211f2005-11-17 13:59:38 +01002111 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int err;
2113 void __user **bufs = (void __user **)data;
2114 int channels = runtime->channels;
2115 int c;
2116 if (substream->ops->copy) {
2117 for (c = 0; c < channels; ++c, ++bufs) {
2118 char __user *buf;
2119 if (*bufs == NULL)
2120 continue;
2121 buf = *bufs + samples_to_bytes(runtime, off);
2122 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2123 return err;
2124 }
2125 } else {
2126 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 for (c = 0; c < channels; ++c, ++bufs) {
2128 char *hwbuf;
2129 char __user *buf;
2130 if (*bufs == NULL)
2131 continue;
2132
2133 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2134 buf = *bufs + samples_to_bytes(runtime, off);
2135 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2136 return -EFAULT;
2137 }
2138 }
2139 return 0;
2140}
2141
Takashi Iwai877211f2005-11-17 13:59:38 +01002142snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 void __user **bufs,
2144 snd_pcm_uframes_t frames)
2145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002148 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002150 err = pcm_sanity_check(substream);
2151 if (err < 0)
2152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2155 return -EBADFD;
2156
Takashi Iwai0df63e42006-04-28 15:13:41 +02002157 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2159 return -EINVAL;
2160 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163EXPORT_SYMBOL(snd_pcm_lib_readv);