blob: abfeff1611ce271eabe0bd98a1b75f613d626f96 [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);
Jaroslav Kysela9e216e82010-07-19 16:37:39 +020070 if (avail > runtime->buffer_size)
71 avail = runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 runtime->silence_filled = avail > 0 ? avail : 0;
73 runtime->silence_start = (runtime->status->hw_ptr +
74 runtime->silence_filled) %
75 runtime->boundary;
76 } else {
77 ofs = runtime->status->hw_ptr;
78 frames = new_hw_ptr - ofs;
79 if ((snd_pcm_sframes_t)frames < 0)
80 frames += runtime->boundary;
81 runtime->silence_filled -= frames;
82 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
83 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020084 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020086 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 frames = runtime->buffer_size - runtime->silence_filled;
90 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020091 if (snd_BUG_ON(frames > runtime->buffer_size))
92 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (frames == 0)
94 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020095 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 while (frames > 0) {
97 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
98 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
99 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
100 if (substream->ops->silence) {
101 int err;
102 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200103 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else {
105 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
106 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 }
108 } else {
109 unsigned int c;
110 unsigned int channels = runtime->channels;
111 if (substream->ops->silence) {
112 for (c = 0; c < channels; ++c) {
113 int err;
114 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200115 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 } else {
118 size_t dma_csize = runtime->dma_bytes / channels;
119 for (c = 0; c < channels; ++c) {
120 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
121 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 }
123 }
124 }
125 runtime->silence_filled += transfer;
126 frames -= transfer;
127 ofs = 0;
128 }
129}
130
Takashi Iwaic0070112009-06-08 15:58:48 +0200131static void pcm_debug_name(struct snd_pcm_substream *substream,
132 char *name, size_t len)
133{
134 snprintf(name, len, "pcmC%dD%d%c:%d",
135 substream->pcm->card->number,
136 substream->pcm->device,
137 substream->stream ? 'c' : 'p',
138 substream->number);
139}
140
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100141#define XRUN_DEBUG_BASIC (1<<0)
142#define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
143#define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
144#define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
145#define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
146#define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
147#define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
148
149#ifdef CONFIG_SND_PCM_XRUN_DEBUG
150
151#define xrun_debug(substream, mask) \
152 ((substream)->pstr->xrun_debug & (mask))
Jarkko Nikula0f170142010-03-26 16:07:25 +0200153#else
154#define xrun_debug(substream, mask) 0
155#endif
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100156
157#define dump_stack_on_xrun(substream) do { \
158 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
159 dump_stack(); \
160 } while (0)
161
Takashi Iwai877211f2005-11-17 13:59:38 +0100162static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200164 struct snd_pcm_runtime *runtime = substream->runtime;
165
166 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
167 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100169 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
Takashi Iwaic0070112009-06-08 15:58:48 +0200170 char name[16];
171 pcm_debug_name(substream, name, sizeof(name));
172 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100173 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jarkko Nikula0f170142010-03-26 16:07:25 +0200177#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100178#define hw_ptr_error(substream, fmt, args...) \
179 do { \
180 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100181 xrun_log_show(substream); \
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100182 if (printk_ratelimit()) { \
183 snd_printd("PCM: " fmt, ##args); \
184 } \
185 dump_stack_on_xrun(substream); \
186 } \
187 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100189#define XRUN_LOG_CNT 10
190
191struct hwptr_log_entry {
Ben Gardinerec08b142011-05-18 10:03:34 -0400192 unsigned int in_interrupt;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100193 unsigned long jiffies;
194 snd_pcm_uframes_t pos;
195 snd_pcm_uframes_t period_size;
196 snd_pcm_uframes_t buffer_size;
197 snd_pcm_uframes_t old_hw_ptr;
198 snd_pcm_uframes_t hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100199};
200
201struct snd_pcm_hwptr_log {
202 unsigned int idx;
203 unsigned int hit: 1;
204 struct hwptr_log_entry entries[XRUN_LOG_CNT];
205};
206
207static void xrun_log(struct snd_pcm_substream *substream,
Ben Gardinerec08b142011-05-18 10:03:34 -0400208 snd_pcm_uframes_t pos, int in_interrupt)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100209{
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
212 struct hwptr_log_entry *entry;
213
214 if (log == NULL) {
215 log = kzalloc(sizeof(*log), GFP_ATOMIC);
216 if (log == NULL)
217 return;
218 runtime->hwptr_log = log;
219 } else {
220 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
221 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200222 }
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100223 entry = &log->entries[log->idx];
Ben Gardinerec08b142011-05-18 10:03:34 -0400224 entry->in_interrupt = in_interrupt;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100225 entry->jiffies = jiffies;
226 entry->pos = pos;
227 entry->period_size = runtime->period_size;
Joe Perchesc80c1d52010-11-14 19:05:02 -0800228 entry->buffer_size = runtime->buffer_size;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100229 entry->old_hw_ptr = runtime->status->hw_ptr;
230 entry->hw_ptr_base = runtime->hw_ptr_base;
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100231 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100234static void xrun_log_show(struct snd_pcm_substream *substream)
235{
236 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
237 struct hwptr_log_entry *entry;
238 char name[16];
239 unsigned int idx;
240 int cnt;
241
242 if (log == NULL)
243 return;
244 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
245 return;
246 pcm_debug_name(substream, name, sizeof(name));
247 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
248 entry = &log->entries[idx];
249 if (entry->period_size == 0)
250 break;
Ben Gardinerec08b142011-05-18 10:03:34 -0400251 snd_printd("hwptr log: %s: %sj=%lu, pos=%ld/%ld/%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100252 "hwptr=%ld/%ld\n",
Ben Gardinerec08b142011-05-18 10:03:34 -0400253 name, entry->in_interrupt ? "[Q] " : "",
254 entry->jiffies,
255 (unsigned long)entry->pos,
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100256 (unsigned long)entry->period_size,
257 (unsigned long)entry->buffer_size,
258 (unsigned long)entry->old_hw_ptr,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100259 (unsigned long)entry->hw_ptr_base);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100260 idx++;
261 idx %= XRUN_LOG_CNT;
262 }
263 log->hit = 1;
264}
265
266#else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
267
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100268#define hw_ptr_error(substream, fmt, args...) do { } while (0)
Ben Gardiner217658f2011-05-18 23:52:38 -0400269#define xrun_log(substream, pos, in_interrupt) do { } while (0)
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +0100270#define xrun_log_show(substream) do { } while (0)
271
272#endif
273
Jaroslav Kysela12509322010-01-07 15:36:31 +0100274int snd_pcm_update_state(struct snd_pcm_substream *substream,
275 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 snd_pcm_uframes_t avail;
278
279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
280 avail = snd_pcm_playback_avail(runtime);
281 else
282 avail = snd_pcm_capture_avail(runtime);
283 if (avail > runtime->avail_max)
284 runtime->avail_max = avail;
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200285 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
286 if (avail >= runtime->buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 snd_pcm_drain_done(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200288 return -EPIPE;
289 }
290 } else {
291 if (avail >= runtime->stop_threshold) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xrun(substream);
Takashi Iwai4cdc1152009-08-20 16:40:16 +0200293 return -EPIPE;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
David Dillow5daeba32010-06-27 00:13:20 +0200296 if (runtime->twake) {
297 if (avail >= runtime->twake)
298 wake_up(&runtime->tsleep);
299 } else if (avail >= runtime->control->avail_min)
300 wake_up(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100304static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
305 unsigned int in_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Takashi Iwai877211f2005-11-17 13:59:38 +0100307 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 snd_pcm_uframes_t pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100309 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200310 snd_pcm_sframes_t hdelta, delta;
311 unsigned long jdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200313 old_hw_ptr = runtime->status->hw_ptr;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100314 pos = substream->ops->pointer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (pos == SNDRV_PCM_POS_XRUN) {
316 xrun(substream);
317 return -EPIPE;
318 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100319 if (pos >= runtime->buffer_size) {
320 if (printk_ratelimit()) {
321 char name[16];
322 pcm_debug_name(substream, name, sizeof(name));
323 xrun_log_show(substream);
324 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
325 "buffer size = %ld, period size = %ld\n",
326 name, pos, runtime->buffer_size,
327 runtime->period_size);
328 }
329 pos = 0;
Takashi Iwaicedb8112009-07-23 11:04:13 +0200330 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100331 pos -= pos % runtime->min_align;
332 if (xrun_debug(substream, XRUN_DEBUG_LOG))
Ben Gardinerec08b142011-05-18 10:03:34 -0400333 xrun_log(substream, pos, in_interrupt);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100334 hw_base = runtime->hw_ptr_base;
335 new_hw_ptr = hw_base + pos;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100336 if (in_interrupt) {
337 /* we know that one period was processed */
338 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100339 delta = runtime->hw_ptr_interrupt + runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100340 if (delta > new_hw_ptr) {
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200341 /* check for double acknowledged interrupts */
342 hdelta = jiffies - runtime->hw_ptr_jiffies;
343 if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
344 hw_base += runtime->buffer_size;
345 if (hw_base >= runtime->boundary)
346 hw_base = 0;
347 new_hw_ptr = hw_base + pos;
348 goto __delta;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100352 /* new_hw_ptr might be lower than old_hw_ptr in case when */
353 /* pointer crosses the end of the ring buffer */
354 if (new_hw_ptr < old_hw_ptr) {
355 hw_base += runtime->buffer_size;
356 if (hw_base >= runtime->boundary)
357 hw_base = 0;
358 new_hw_ptr = hw_base + pos;
359 }
360 __delta:
Clemens Ladischb406e612010-05-25 09:01:46 +0200361 delta = new_hw_ptr - old_hw_ptr;
362 if (delta < 0)
363 delta += runtime->boundary;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100364 if (xrun_debug(substream, in_interrupt ?
365 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
366 char name[16];
367 pcm_debug_name(substream, name, sizeof(name));
368 snd_printd("%s_update: %s: pos=%u/%u/%u, "
369 "hwptr=%ld/%ld/%ld/%ld\n",
370 in_interrupt ? "period" : "hwptr",
371 name,
372 (unsigned int)pos,
373 (unsigned int)runtime->period_size,
374 (unsigned int)runtime->buffer_size,
375 (unsigned long)delta,
376 (unsigned long)old_hw_ptr,
377 (unsigned long)new_hw_ptr,
378 (unsigned long)runtime->hw_ptr_base);
379 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100380
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100381 if (runtime->no_period_wakeup) {
Kelly Anderson12ff4142011-04-01 11:58:25 +0200382 snd_pcm_sframes_t xrun_threshold;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100383 /*
384 * Without regular period interrupts, we have to check
385 * the elapsed time to detect xruns.
386 */
387 jdelta = jiffies - runtime->hw_ptr_jiffies;
Clemens Ladisch47228e42010-11-18 09:53:07 +0100388 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
389 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100390 hdelta = jdelta - delta * HZ / runtime->rate;
Kelly Anderson12ff4142011-04-01 11:58:25 +0200391 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
392 while (hdelta > xrun_threshold) {
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100393 delta += runtime->buffer_size;
394 hw_base += runtime->buffer_size;
395 if (hw_base >= runtime->boundary)
396 hw_base = 0;
397 new_hw_ptr = hw_base + pos;
398 hdelta -= runtime->hw_ptr_buffer_jiffies;
399 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100400 goto no_delta_check;
Clemens Ladisch59ff8782010-11-18 09:43:52 +0100401 }
Clemens Ladischab69a492010-11-15 10:46:23 +0100402
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100403 /* something must be really wrong */
Jaroslav Kysela7b3a1772010-01-08 08:43:01 +0100404 if (delta >= runtime->buffer_size + runtime->period_size) {
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100405 hw_ptr_error(substream,
406 "Unexpected hw_pointer value %s"
407 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
408 "old_hw_ptr=%ld)\n",
409 in_interrupt ? "[Q] " : "[P]",
410 substream->stream, (long)pos,
411 (long)new_hw_ptr, (long)old_hw_ptr);
412 return 0;
413 }
Takashi Iwaic87d9732009-05-27 10:53:33 +0200414
415 /* Do jiffies check only in xrun_debug mode */
Jaroslav Kysela741b20c2009-12-17 17:34:39 +0100416 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
Takashi Iwaic87d9732009-05-27 10:53:33 +0200417 goto no_jiffies_check;
418
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200419 /* Skip the jiffies check for hardwares with BATCH flag.
420 * Such hardware usually just increases the position at each IRQ,
421 * thus it can't give any strange position.
422 */
423 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
424 goto no_jiffies_check;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100425 hdelta = delta;
Jaroslav Kyselaa4444da2009-05-28 12:31:56 +0200426 if (hdelta < runtime->delay)
427 goto no_jiffies_check;
428 hdelta -= runtime->delay;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200429 jdelta = jiffies - runtime->hw_ptr_jiffies;
430 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
431 delta = jdelta /
432 (((runtime->period_size * HZ) / runtime->rate)
433 + HZ/100);
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100434 /* move new_hw_ptr according jiffies not pos variable */
435 new_hw_ptr = old_hw_ptr;
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100436 hw_base = delta;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100437 /* use loop to avoid checks for delta overflows */
438 /* the delta value is small or zero in most cases */
439 while (delta > 0) {
440 new_hw_ptr += runtime->period_size;
441 if (new_hw_ptr >= runtime->boundary)
442 new_hw_ptr -= runtime->boundary;
443 delta--;
444 }
445 /* align hw_base to buffer_size */
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200446 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100447 "hw_ptr skipping! %s"
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200448 "(pos=%ld, delta=%ld, period=%ld, "
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100449 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
450 in_interrupt ? "[Q] " : "",
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200451 (long)pos, (long)hdelta,
452 (long)runtime->period_size, jdelta,
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100453 ((hdelta * HZ) / runtime->rate), hw_base,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100454 (unsigned long)old_hw_ptr,
455 (unsigned long)new_hw_ptr);
Jaroslav Kyselaed69c6a2010-01-13 08:12:31 +0100456 /* reset values to proper state */
457 delta = 0;
458 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200459 }
Takashi Iwai3e5b5012009-04-28 12:07:08 +0200460 no_jiffies_check:
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200461 if (delta > runtime->period_size + runtime->period_size / 2) {
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100462 hw_ptr_error(substream,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100463 "Lost interrupts? %s"
464 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
465 "old_hw_ptr=%ld)\n",
466 in_interrupt ? "[Q] " : "",
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100467 substream->stream, (long)delta,
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100468 (long)new_hw_ptr,
469 (long)old_hw_ptr);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100470 }
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100471
Clemens Ladischab69a492010-11-15 10:46:23 +0100472 no_delta_check:
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100473 if (runtime->status->hw_ptr == new_hw_ptr)
474 return 0;
Takashi Iwaiab1863f2009-06-07 12:09:17 +0200475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
477 runtime->silence_size > 0)
478 snd_pcm_playback_silence(substream, new_hw_ptr);
479
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100480 if (in_interrupt) {
Clemens Ladischead40462010-05-21 09:15:59 +0200481 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
482 if (delta < 0)
483 delta += runtime->boundary;
484 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
485 runtime->hw_ptr_interrupt += delta;
486 if (runtime->hw_ptr_interrupt >= runtime->boundary)
487 runtime->hw_ptr_interrupt -= runtime->boundary;
Jaroslav Kyselae7636922010-01-26 17:08:24 +0100488 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100489 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 runtime->status->hw_ptr = new_hw_ptr;
Jaroslav Kyselabbf6ad12009-04-10 12:28:58 +0200491 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kysela13f040f2009-05-28 11:31:20 +0200492 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
493 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jaroslav Kysela12509322010-01-07 15:36:31 +0100495 return snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100499int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100501 return snd_pcm_update_hw_ptr0(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504/**
505 * snd_pcm_set_ops - set the PCM operators
506 * @pcm: the pcm instance
507 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
508 * @ops: the operator table
509 *
510 * Sets the given PCM operators to the pcm instance.
511 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100512void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Takashi Iwai877211f2005-11-17 13:59:38 +0100514 struct snd_pcm_str *stream = &pcm->streams[direction];
515 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 for (substream = stream->substream; substream != NULL; substream = substream->next)
518 substream->ops = ops;
519}
520
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200521EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523/**
524 * snd_pcm_sync - set the PCM sync id
525 * @substream: the pcm substream
526 *
527 * Sets the PCM sync identifier for the card.
528 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100529void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Takashi Iwai877211f2005-11-17 13:59:38 +0100531 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 runtime->sync.id32[0] = substream->pcm->card->number;
534 runtime->sync.id32[1] = -1;
535 runtime->sync.id32[2] = -1;
536 runtime->sync.id32[3] = -1;
537}
538
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200539EXPORT_SYMBOL(snd_pcm_set_sync);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * Standard ioctl routine
543 */
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static inline unsigned int div32(unsigned int a, unsigned int b,
546 unsigned int *r)
547{
548 if (b == 0) {
549 *r = 0;
550 return UINT_MAX;
551 }
552 *r = a % b;
553 return a / b;
554}
555
556static inline unsigned int div_down(unsigned int a, unsigned int b)
557{
558 if (b == 0)
559 return UINT_MAX;
560 return a / b;
561}
562
563static inline unsigned int div_up(unsigned int a, unsigned int b)
564{
565 unsigned int r;
566 unsigned int q;
567 if (b == 0)
568 return UINT_MAX;
569 q = div32(a, b, &r);
570 if (r)
571 ++q;
572 return q;
573}
574
575static inline unsigned int mul(unsigned int a, unsigned int b)
576{
577 if (a == 0)
578 return 0;
579 if (div_down(UINT_MAX, a) < b)
580 return UINT_MAX;
581 return a * b;
582}
583
584static inline unsigned int muldiv32(unsigned int a, unsigned int b,
585 unsigned int c, unsigned int *r)
586{
587 u_int64_t n = (u_int64_t) a * b;
588 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200589 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *r = 0;
591 return UINT_MAX;
592 }
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200593 n = div_u64_rem(n, c, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (n >= UINT_MAX) {
595 *r = 0;
596 return UINT_MAX;
597 }
598 return n;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
602 * snd_interval_refine - refine the interval value of configurator
603 * @i: the interval value to refine
604 * @v: the interval value to refer to
605 *
606 * Refines the interval value with the reference value.
607 * The interval is changed to the range satisfying both intervals.
608 * The interval status (min, max, integer, etc.) are evaluated.
609 *
610 * Returns non-zero if the value is changed, zero if not changed.
611 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100612int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200615 if (snd_BUG_ON(snd_interval_empty(i)))
616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (i->min < v->min) {
618 i->min = v->min;
619 i->openmin = v->openmin;
620 changed = 1;
621 } else if (i->min == v->min && !i->openmin && v->openmin) {
622 i->openmin = 1;
623 changed = 1;
624 }
625 if (i->max > v->max) {
626 i->max = v->max;
627 i->openmax = v->openmax;
628 changed = 1;
629 } else if (i->max == v->max && !i->openmax && v->openmax) {
630 i->openmax = 1;
631 changed = 1;
632 }
633 if (!i->integer && v->integer) {
634 i->integer = 1;
635 changed = 1;
636 }
637 if (i->integer) {
638 if (i->openmin) {
639 i->min++;
640 i->openmin = 0;
641 }
642 if (i->openmax) {
643 i->max--;
644 i->openmax = 0;
645 }
646 } else if (!i->openmin && !i->openmax && i->min == i->max)
647 i->integer = 1;
648 if (snd_interval_checkempty(i)) {
649 snd_interval_none(i);
650 return -EINVAL;
651 }
652 return changed;
653}
654
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200655EXPORT_SYMBOL(snd_interval_refine);
656
Takashi Iwai877211f2005-11-17 13:59:38 +0100657static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200659 if (snd_BUG_ON(snd_interval_empty(i)))
660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (snd_interval_single(i))
662 return 0;
663 i->max = i->min;
664 i->openmax = i->openmin;
665 if (i->openmax)
666 i->max++;
667 return 1;
668}
669
Takashi Iwai877211f2005-11-17 13:59:38 +0100670static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200672 if (snd_BUG_ON(snd_interval_empty(i)))
673 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (snd_interval_single(i))
675 return 0;
676 i->min = i->max;
677 i->openmin = i->openmax;
678 if (i->openmin)
679 i->min--;
680 return 1;
681}
682
Takashi Iwai877211f2005-11-17 13:59:38 +0100683void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 if (a->empty || b->empty) {
686 snd_interval_none(c);
687 return;
688 }
689 c->empty = 0;
690 c->min = mul(a->min, b->min);
691 c->openmin = (a->openmin || b->openmin);
692 c->max = mul(a->max, b->max);
693 c->openmax = (a->openmax || b->openmax);
694 c->integer = (a->integer && b->integer);
695}
696
697/**
698 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200699 * @a: dividend
700 * @b: divisor
701 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
703 * c = a / b
704 *
705 * Returns non-zero if the value is changed, zero if not changed.
706 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100707void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 unsigned int r;
710 if (a->empty || b->empty) {
711 snd_interval_none(c);
712 return;
713 }
714 c->empty = 0;
715 c->min = div32(a->min, b->max, &r);
716 c->openmin = (r || a->openmin || b->openmax);
717 if (b->min > 0) {
718 c->max = div32(a->max, b->min, &r);
719 if (r) {
720 c->max++;
721 c->openmax = 1;
722 } else
723 c->openmax = (a->openmax || b->openmin);
724 } else {
725 c->max = UINT_MAX;
726 c->openmax = 0;
727 }
728 c->integer = 0;
729}
730
731/**
732 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200733 * @a: dividend 1
734 * @b: dividend 2
735 * @k: divisor (as integer)
736 * @c: result
737 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * c = a * b / k
739 *
740 * Returns non-zero if the value is changed, zero if not changed.
741 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100742void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
743 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 unsigned int r;
746 if (a->empty || b->empty) {
747 snd_interval_none(c);
748 return;
749 }
750 c->empty = 0;
751 c->min = muldiv32(a->min, b->min, k, &r);
752 c->openmin = (r || a->openmin || b->openmin);
753 c->max = muldiv32(a->max, b->max, k, &r);
754 if (r) {
755 c->max++;
756 c->openmax = 1;
757 } else
758 c->openmax = (a->openmax || b->openmax);
759 c->integer = 0;
760}
761
762/**
763 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200764 * @a: dividend 1
765 * @k: dividend 2 (as integer)
766 * @b: divisor
767 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
769 * c = a * k / b
770 *
771 * Returns non-zero if the value is changed, zero if not changed.
772 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100773void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
774 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 unsigned int r;
777 if (a->empty || b->empty) {
778 snd_interval_none(c);
779 return;
780 }
781 c->empty = 0;
782 c->min = muldiv32(a->min, k, b->max, &r);
783 c->openmin = (r || a->openmin || b->openmax);
784 if (b->min > 0) {
785 c->max = muldiv32(a->max, k, b->min, &r);
786 if (r) {
787 c->max++;
788 c->openmax = 1;
789 } else
790 c->openmax = (a->openmax || b->openmin);
791 } else {
792 c->max = UINT_MAX;
793 c->openmax = 0;
794 }
795 c->integer = 0;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/* ---- */
799
800
801/**
802 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200803 * @i: interval to refine
804 * @rats_count: number of ratnum_t
805 * @rats: ratnum_t array
806 * @nump: pointer to store the resultant numerator
807 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
809 * Returns non-zero if the value is changed, zero if not changed.
810 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100811int snd_interval_ratnum(struct snd_interval *i,
812 unsigned int rats_count, struct snd_ratnum *rats,
813 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Krzysztof Helt8374e242009-12-21 17:07:08 +0100815 unsigned int best_num, best_den;
816 int best_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100818 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int err;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100820 unsigned int result_num, result_den;
821 int result_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 best_num = best_den = best_diff = 0;
824 for (k = 0; k < rats_count; ++k) {
825 unsigned int num = rats[k].num;
826 unsigned int den;
827 unsigned int q = i->min;
828 int diff;
829 if (q == 0)
830 q = 1;
Krzysztof Helt40962d72009-12-19 18:31:04 +0100831 den = div_up(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (den < rats[k].den_min)
833 continue;
834 if (den > rats[k].den_max)
835 den = rats[k].den_max;
836 else {
837 unsigned int r;
838 r = (den - rats[k].den_min) % rats[k].den_step;
839 if (r != 0)
840 den -= r;
841 }
842 diff = num - q * den;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100843 if (diff < 0)
844 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (best_num == 0 ||
846 diff * best_den < best_diff * den) {
847 best_diff = diff;
848 best_den = den;
849 best_num = num;
850 }
851 }
852 if (best_den == 0) {
853 i->empty = 1;
854 return -EINVAL;
855 }
856 t.min = div_down(best_num, best_den);
857 t.openmin = !!(best_num % best_den);
858
Krzysztof Helt8374e242009-12-21 17:07:08 +0100859 result_num = best_num;
860 result_diff = best_diff;
861 result_den = best_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 best_num = best_den = best_diff = 0;
863 for (k = 0; k < rats_count; ++k) {
864 unsigned int num = rats[k].num;
865 unsigned int den;
866 unsigned int q = i->max;
867 int diff;
868 if (q == 0) {
869 i->empty = 1;
870 return -EINVAL;
871 }
Krzysztof Helt40962d72009-12-19 18:31:04 +0100872 den = div_down(num, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (den > rats[k].den_max)
874 continue;
875 if (den < rats[k].den_min)
876 den = rats[k].den_min;
877 else {
878 unsigned int r;
879 r = (den - rats[k].den_min) % rats[k].den_step;
880 if (r != 0)
881 den += rats[k].den_step - r;
882 }
883 diff = q * den - num;
Krzysztof Helt8374e242009-12-21 17:07:08 +0100884 if (diff < 0)
885 diff = -diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (best_num == 0 ||
887 diff * best_den < best_diff * den) {
888 best_diff = diff;
889 best_den = den;
890 best_num = num;
891 }
892 }
893 if (best_den == 0) {
894 i->empty = 1;
895 return -EINVAL;
896 }
897 t.max = div_up(best_num, best_den);
898 t.openmax = !!(best_num % best_den);
899 t.integer = 0;
900 err = snd_interval_refine(i, &t);
901 if (err < 0)
902 return err;
903
904 if (snd_interval_single(i)) {
Krzysztof Helt8374e242009-12-21 17:07:08 +0100905 if (best_diff * result_den < result_diff * best_den) {
906 result_num = best_num;
907 result_den = best_den;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (nump)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100910 *nump = result_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (denp)
Krzysztof Helt8374e242009-12-21 17:07:08 +0100912 *denp = result_den;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 return err;
915}
916
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200917EXPORT_SYMBOL(snd_interval_ratnum);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/**
920 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200921 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100922 * @rats_count: number of struct ratden
923 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200924 * @nump: pointer to store the resultant numerator
925 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * Returns non-zero if the value is changed, zero if not changed.
928 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100929static int snd_interval_ratden(struct snd_interval *i,
930 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int *nump, unsigned int *denp)
932{
933 unsigned int best_num, best_diff, best_den;
934 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100935 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int err;
937
938 best_num = best_den = best_diff = 0;
939 for (k = 0; k < rats_count; ++k) {
940 unsigned int num;
941 unsigned int den = rats[k].den;
942 unsigned int q = i->min;
943 int diff;
944 num = mul(q, den);
945 if (num > rats[k].num_max)
946 continue;
947 if (num < rats[k].num_min)
948 num = rats[k].num_max;
949 else {
950 unsigned int r;
951 r = (num - rats[k].num_min) % rats[k].num_step;
952 if (r != 0)
953 num += rats[k].num_step - r;
954 }
955 diff = num - q * den;
956 if (best_num == 0 ||
957 diff * best_den < best_diff * den) {
958 best_diff = diff;
959 best_den = den;
960 best_num = num;
961 }
962 }
963 if (best_den == 0) {
964 i->empty = 1;
965 return -EINVAL;
966 }
967 t.min = div_down(best_num, best_den);
968 t.openmin = !!(best_num % best_den);
969
970 best_num = best_den = best_diff = 0;
971 for (k = 0; k < rats_count; ++k) {
972 unsigned int num;
973 unsigned int den = rats[k].den;
974 unsigned int q = i->max;
975 int diff;
976 num = mul(q, den);
977 if (num < rats[k].num_min)
978 continue;
979 if (num > rats[k].num_max)
980 num = rats[k].num_max;
981 else {
982 unsigned int r;
983 r = (num - rats[k].num_min) % rats[k].num_step;
984 if (r != 0)
985 num -= r;
986 }
987 diff = q * den - num;
988 if (best_num == 0 ||
989 diff * best_den < best_diff * den) {
990 best_diff = diff;
991 best_den = den;
992 best_num = num;
993 }
994 }
995 if (best_den == 0) {
996 i->empty = 1;
997 return -EINVAL;
998 }
999 t.max = div_up(best_num, best_den);
1000 t.openmax = !!(best_num % best_den);
1001 t.integer = 0;
1002 err = snd_interval_refine(i, &t);
1003 if (err < 0)
1004 return err;
1005
1006 if (snd_interval_single(i)) {
1007 if (nump)
1008 *nump = best_num;
1009 if (denp)
1010 *denp = best_den;
1011 }
1012 return err;
1013}
1014
1015/**
1016 * snd_interval_list - refine the interval value from the list
1017 * @i: the interval value to refine
1018 * @count: the number of elements in the list
1019 * @list: the value list
1020 * @mask: the bit-mask to evaluate
1021 *
1022 * Refines the interval value from the list.
1023 * When mask is non-zero, only the elements corresponding to bit 1 are
1024 * evaluated.
1025 *
1026 * Returns non-zero if the value is changed, zero if not changed.
1027 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001028int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 unsigned int k;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001031 struct snd_interval list_range;
Takashi Iwai0981a262007-02-01 14:53:49 +01001032
1033 if (!count) {
1034 i->empty = 1;
1035 return -EINVAL;
1036 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001037 snd_interval_any(&list_range);
1038 list_range.min = UINT_MAX;
1039 list_range.max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for (k = 0; k < count; k++) {
1041 if (mask && !(mask & (1 << k)))
1042 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001043 if (!snd_interval_test(i, list[k]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 continue;
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001045 list_range.min = min(list_range.min, list[k]);
1046 list_range.max = max(list_range.max, list[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Clemens Ladischb1ddaf62009-08-25 08:15:41 +02001048 return snd_interval_refine(i, &list_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001051EXPORT_SYMBOL(snd_interval_list);
1052
Takashi Iwai877211f2005-11-17 13:59:38 +01001053static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 unsigned int n;
1056 int changed = 0;
1057 n = (i->min - min) % step;
1058 if (n != 0 || i->openmin) {
1059 i->min += step - n;
1060 changed = 1;
1061 }
1062 n = (i->max - min) % step;
1063 if (n != 0 || i->openmax) {
1064 i->max -= n;
1065 changed = 1;
1066 }
1067 if (snd_interval_checkempty(i)) {
1068 i->empty = 1;
1069 return -EINVAL;
1070 }
1071 return changed;
1072}
1073
1074/* Info constraints helpers */
1075
1076/**
1077 * snd_pcm_hw_rule_add - add the hw-constraint rule
1078 * @runtime: the pcm runtime instance
1079 * @cond: condition bits
1080 * @var: the variable to evaluate
1081 * @func: the evaluation function
1082 * @private: the private data pointer passed to function
1083 * @dep: the dependent variables
1084 *
1085 * Returns zero if successful, or a negative error code on failure.
1086 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001087int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int var,
1089 snd_pcm_hw_rule_func_t func, void *private,
1090 int dep, ...)
1091{
Takashi Iwai877211f2005-11-17 13:59:38 +01001092 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1093 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 unsigned int k;
1095 va_list args;
1096 va_start(args, dep);
1097 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001098 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 unsigned int new_rules = constrs->rules_all + 16;
1100 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001101 if (!new) {
1102 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (constrs->rules) {
1106 memcpy(new, constrs->rules,
1107 constrs->rules_num * sizeof(*c));
1108 kfree(constrs->rules);
1109 }
1110 constrs->rules = new;
1111 constrs->rules_all = new_rules;
1112 }
1113 c = &constrs->rules[constrs->rules_num];
1114 c->cond = cond;
1115 c->func = func;
1116 c->var = var;
1117 c->private = private;
1118 k = 0;
1119 while (1) {
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001120 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1121 va_end(args);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001122 return -EINVAL;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 c->deps[k++] = dep;
1125 if (dep < 0)
1126 break;
1127 dep = va_arg(args, int);
1128 }
1129 constrs->rules_num++;
1130 va_end(args);
1131 return 0;
Jesper Juhl87a1c8a2010-12-21 00:03:17 +01001132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001134EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001137 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001138 * @runtime: PCM runtime instance
1139 * @var: hw_params variable to apply the mask
1140 * @mask: the bitmap mask
1141 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001142 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001144int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 u_int32_t mask)
1146{
Takashi Iwai877211f2005-11-17 13:59:38 +01001147 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1148 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 *maskp->bits &= mask;
1150 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1151 if (*maskp->bits == 0)
1152 return -EINVAL;
1153 return 0;
1154}
1155
1156/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001157 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +02001158 * @runtime: PCM runtime instance
1159 * @var: hw_params variable to apply the mask
1160 * @mask: the 64bit bitmap mask
1161 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001162 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001164int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 u_int64_t mask)
1166{
Takashi Iwai877211f2005-11-17 13:59:38 +01001167 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1168 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 maskp->bits[0] &= (u_int32_t)mask;
1170 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1171 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1172 if (! maskp->bits[0] && ! maskp->bits[1])
1173 return -EINVAL;
1174 return 0;
1175}
1176
1177/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001178 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001179 * @runtime: PCM runtime instance
1180 * @var: hw_params variable to apply the integer constraint
1181 *
1182 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001184int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Takashi Iwai877211f2005-11-17 13:59:38 +01001186 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return snd_interval_setinteger(constrs_interval(constrs, var));
1188}
1189
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001190EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001193 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001194 * @runtime: PCM runtime instance
1195 * @var: hw_params variable to apply the range
1196 * @min: the minimal value
1197 * @max: the maximal value
1198 *
1199 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001201int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned int min, unsigned int max)
1203{
Takashi Iwai877211f2005-11-17 13:59:38 +01001204 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1205 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 t.min = min;
1207 t.max = max;
1208 t.openmin = t.openmax = 0;
1209 t.integer = 0;
1210 return snd_interval_refine(constrs_interval(constrs, var), &t);
1211}
1212
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001213EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1214
Takashi Iwai877211f2005-11-17 13:59:38 +01001215static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1216 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Takashi Iwai877211f2005-11-17 13:59:38 +01001218 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1220}
1221
1222
1223/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001224 * snd_pcm_hw_constraint_list - apply a list of constraints 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 list constraint
1228 * @l: list
1229 *
1230 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001232int snd_pcm_hw_constraint_list(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_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 return snd_pcm_hw_rule_add(runtime, cond, var,
1238 snd_pcm_hw_rule_list, l,
1239 var, -1);
1240}
1241
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001242EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1243
Takashi Iwai877211f2005-11-17 13:59:38 +01001244static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1245 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai877211f2005-11-17 13:59:38 +01001247 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 unsigned int num = 0, den = 0;
1249 int err;
1250 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1251 r->nrats, r->rats, &num, &den);
1252 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1253 params->rate_num = num;
1254 params->rate_den = den;
1255 }
1256 return err;
1257}
1258
1259/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001260 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001261 * @runtime: PCM runtime instance
1262 * @cond: condition bits
1263 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001264 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001266int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 unsigned int cond,
1268 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001269 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 return snd_pcm_hw_rule_add(runtime, cond, var,
1272 snd_pcm_hw_rule_ratnums, r,
1273 var, -1);
1274}
1275
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001276EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1277
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1279 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Takashi Iwai877211f2005-11-17 13:59:38 +01001281 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 unsigned int num = 0, den = 0;
1283 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1284 r->nrats, r->rats, &num, &den);
1285 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1286 params->rate_num = num;
1287 params->rate_den = den;
1288 }
1289 return err;
1290}
1291
1292/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001293 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001294 * @runtime: PCM runtime instance
1295 * @cond: condition bits
1296 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001297 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001299int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 unsigned int cond,
1301 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001302 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 return snd_pcm_hw_rule_add(runtime, cond, var,
1305 snd_pcm_hw_rule_ratdens, r,
1306 var, -1);
1307}
1308
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001309EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1310
Takashi Iwai877211f2005-11-17 13:59:38 +01001311static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1312 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 unsigned int l = (unsigned long) rule->private;
1315 int width = l & 0xffff;
1316 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001317 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (snd_interval_single(i) && snd_interval_value(i) == width)
1319 params->msbits = msbits;
1320 return 0;
1321}
1322
1323/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001324 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001325 * @runtime: PCM runtime instance
1326 * @cond: condition bits
1327 * @width: sample bits width
1328 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001330int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 unsigned int cond,
1332 unsigned int width,
1333 unsigned int msbits)
1334{
1335 unsigned long l = (msbits << 16) | width;
1336 return snd_pcm_hw_rule_add(runtime, cond, -1,
1337 snd_pcm_hw_rule_msbits,
1338 (void*) l,
1339 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1340}
1341
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001342EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1345 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 unsigned long step = (unsigned long) rule->private;
1348 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1349}
1350
1351/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001352 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001353 * @runtime: PCM runtime instance
1354 * @cond: condition bits
1355 * @var: hw_params variable to apply the step constraint
1356 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001358int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned int cond,
1360 snd_pcm_hw_param_t var,
1361 unsigned long step)
1362{
1363 return snd_pcm_hw_rule_add(runtime, cond, var,
1364 snd_pcm_hw_rule_step, (void *) step,
1365 var, -1);
1366}
1367
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001368EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1369
Takashi Iwai877211f2005-11-17 13:59:38 +01001370static 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 -07001371{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001372 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1374 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1375 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1376 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1377 };
1378 return snd_interval_list(hw_param_interval(params, rule->var),
1379 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1380}
1381
1382/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001383 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001384 * @runtime: PCM runtime instance
1385 * @cond: condition bits
1386 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001388int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 unsigned int cond,
1390 snd_pcm_hw_param_t var)
1391{
1392 return snd_pcm_hw_rule_add(runtime, cond, var,
1393 snd_pcm_hw_rule_pow2, NULL,
1394 var, -1);
1395}
1396
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001397EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001400 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 if (hw_is_mask(var)) {
1403 snd_mask_any(hw_param_mask(params, var));
1404 params->cmask |= 1 << var;
1405 params->rmask |= 1 << var;
1406 return;
1407 }
1408 if (hw_is_interval(var)) {
1409 snd_interval_any(hw_param_interval(params, var));
1410 params->cmask |= 1 << var;
1411 params->rmask |= 1 << var;
1412 return;
1413 }
1414 snd_BUG();
1415}
1416
Takashi Iwai877211f2005-11-17 13:59:38 +01001417void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 unsigned int k;
1420 memset(params, 0, sizeof(*params));
1421 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1422 _snd_pcm_hw_param_any(params, k);
1423 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1424 _snd_pcm_hw_param_any(params, k);
1425 params->info = ~0U;
1426}
1427
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001428EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001431 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001432 * @params: the hw_params instance
1433 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001436 * Return the value for field @var if it's fixed in configuration space
1437 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001439int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1440 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001443 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!snd_mask_single(mask))
1445 return -EINVAL;
1446 if (dir)
1447 *dir = 0;
1448 return snd_mask_value(mask);
1449 }
1450 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001451 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!snd_interval_single(i))
1453 return -EINVAL;
1454 if (dir)
1455 *dir = i->openmin;
1456 return snd_interval_value(i);
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EINVAL;
1459}
1460
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001461EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Takashi Iwai877211f2005-11-17 13:59:38 +01001463void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 snd_pcm_hw_param_t var)
1465{
1466 if (hw_is_mask(var)) {
1467 snd_mask_none(hw_param_mask(params, var));
1468 params->cmask |= 1 << var;
1469 params->rmask |= 1 << var;
1470 } else if (hw_is_interval(var)) {
1471 snd_interval_none(hw_param_interval(params, var));
1472 params->cmask |= 1 << var;
1473 params->rmask |= 1 << var;
1474 } else {
1475 snd_BUG();
1476 }
1477}
1478
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001479EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwai877211f2005-11-17 13:59:38 +01001481static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001482 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 int changed;
1485 if (hw_is_mask(var))
1486 changed = snd_mask_refine_first(hw_param_mask(params, var));
1487 else if (hw_is_interval(var))
1488 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001489 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (changed) {
1492 params->cmask |= 1 << var;
1493 params->rmask |= 1 << var;
1494 }
1495 return changed;
1496}
1497
1498
1499/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001500 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001501 * @pcm: PCM instance
1502 * @params: the hw_params instance
1503 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001504 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001506 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * values > minimum. Reduce configuration space accordingly.
1508 * Return the minimum.
1509 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001510int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1511 struct snd_pcm_hw_params *params,
1512 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 int changed = _snd_pcm_hw_param_first(params, var);
1515 if (changed < 0)
1516 return changed;
1517 if (params->rmask) {
1518 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001519 if (snd_BUG_ON(err < 0))
1520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 return snd_pcm_hw_param_value(params, var, dir);
1523}
1524
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001525EXPORT_SYMBOL(snd_pcm_hw_param_first);
1526
Takashi Iwai877211f2005-11-17 13:59:38 +01001527static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001528 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int changed;
1531 if (hw_is_mask(var))
1532 changed = snd_mask_refine_last(hw_param_mask(params, var));
1533 else if (hw_is_interval(var))
1534 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001535 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (changed) {
1538 params->cmask |= 1 << var;
1539 params->rmask |= 1 << var;
1540 }
1541 return changed;
1542}
1543
1544
1545/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001546 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001547 * @pcm: PCM instance
1548 * @params: the hw_params instance
1549 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001550 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001552 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * values < maximum. Reduce configuration space accordingly.
1554 * Return the maximum.
1555 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001556int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1557 struct snd_pcm_hw_params *params,
1558 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 int changed = _snd_pcm_hw_param_last(params, var);
1561 if (changed < 0)
1562 return changed;
1563 if (params->rmask) {
1564 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001565 if (snd_BUG_ON(err < 0))
1566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 return snd_pcm_hw_param_value(params, var, dir);
1569}
1570
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001571EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001574 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001575 * @pcm: PCM instance
1576 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001578 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * The configuration chosen is that obtained fixing in this order:
1580 * first access, first format, first subformat, min channels,
1581 * min rate, min period time, max buffer size, min tick time
1582 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001583int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1584 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001586 static int vars[] = {
1587 SNDRV_PCM_HW_PARAM_ACCESS,
1588 SNDRV_PCM_HW_PARAM_FORMAT,
1589 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1590 SNDRV_PCM_HW_PARAM_CHANNELS,
1591 SNDRV_PCM_HW_PARAM_RATE,
1592 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1593 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1594 SNDRV_PCM_HW_PARAM_TICK_TIME,
1595 -1
1596 };
1597 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001599 for (v = vars; *v != -1; v++) {
1600 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1601 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1602 else
1603 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001604 if (snd_BUG_ON(err < 0))
1605 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return 0;
1608}
1609
Takashi Iwai877211f2005-11-17 13:59:38 +01001610static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 void *arg)
1612{
Takashi Iwai877211f2005-11-17 13:59:38 +01001613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 unsigned long flags;
1615 snd_pcm_stream_lock_irqsave(substream, flags);
1616 if (snd_pcm_running(substream) &&
1617 snd_pcm_update_hw_ptr(substream) >= 0)
1618 runtime->status->hw_ptr %= runtime->buffer_size;
1619 else
1620 runtime->status->hw_ptr = 0;
1621 snd_pcm_stream_unlock_irqrestore(substream, flags);
1622 return 0;
1623}
1624
Takashi Iwai877211f2005-11-17 13:59:38 +01001625static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 void *arg)
1627{
Takashi Iwai877211f2005-11-17 13:59:38 +01001628 struct snd_pcm_channel_info *info = arg;
1629 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int width;
1631 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1632 info->offset = -1;
1633 return 0;
1634 }
1635 width = snd_pcm_format_physical_width(runtime->format);
1636 if (width < 0)
1637 return width;
1638 info->offset = 0;
1639 switch (runtime->access) {
1640 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1641 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1642 info->first = info->channel * width;
1643 info->step = runtime->channels * width;
1644 break;
1645 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1646 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1647 {
1648 size_t size = runtime->dma_bytes / runtime->channels;
1649 info->first = info->channel * size * 8;
1650 info->step = width;
1651 break;
1652 }
1653 default:
1654 snd_BUG();
1655 break;
1656 }
1657 return 0;
1658}
1659
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001660static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1661 void *arg)
1662{
1663 struct snd_pcm_hw_params *params = arg;
1664 snd_pcm_format_t format;
1665 int channels, width;
1666
1667 params->fifo_size = substream->runtime->hw.fifo_size;
1668 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1669 format = params_format(params);
1670 channels = params_channels(params);
1671 width = snd_pcm_format_physical_width(format);
1672 params->fifo_size /= width * channels;
1673 }
1674 return 0;
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/**
1678 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1679 * @substream: the pcm substream instance
1680 * @cmd: ioctl command
1681 * @arg: ioctl argument
1682 *
1683 * Processes the generic ioctl commands for PCM.
1684 * Can be passed as the ioctl callback for PCM ops.
1685 *
1686 * Returns zero if successful, or a negative error code on failure.
1687 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001688int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 unsigned int cmd, void *arg)
1690{
1691 switch (cmd) {
1692 case SNDRV_PCM_IOCTL1_INFO:
1693 return 0;
1694 case SNDRV_PCM_IOCTL1_RESET:
1695 return snd_pcm_lib_ioctl_reset(substream, arg);
1696 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1697 return snd_pcm_lib_ioctl_channel_info(substream, arg);
Jaroslav Kysela8bea8692009-04-27 09:44:40 +02001698 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1699 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 return -ENXIO;
1702}
1703
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001704EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/**
1707 * snd_pcm_period_elapsed - update the pcm status for the next period
1708 * @substream: the pcm substream instance
1709 *
1710 * This function is called from the interrupt handler when the
1711 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001712 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *
1714 * Even if more than one periods have elapsed since the last call, you
1715 * have to call this only once.
1716 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001717void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Takashi Iwai877211f2005-11-17 13:59:38 +01001719 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned long flags;
1721
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001722 if (PCM_RUNTIME_CHECK(substream))
1723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (runtime->transfer_ack_begin)
1727 runtime->transfer_ack_begin(substream);
1728
1729 snd_pcm_stream_lock_irqsave(substream, flags);
1730 if (!snd_pcm_running(substream) ||
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001731 snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto _end;
1733
1734 if (substream->timer_running)
1735 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 _end:
1737 snd_pcm_stream_unlock_irqrestore(substream, flags);
1738 if (runtime->transfer_ack_end)
1739 runtime->transfer_ack_end(substream);
1740 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1741}
1742
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001743EXPORT_SYMBOL(snd_pcm_period_elapsed);
1744
Takashi Iwai13075512008-01-08 18:08:14 +01001745/*
1746 * Wait until avail_min data becomes available
1747 * Returns a negative error code if any error occurs during operation.
1748 * The available space is stored on availp. When err = 0 and avail = 0
1749 * on the capture stream, it indicates the stream is in DRAINING state.
1750 */
David Dillow5daeba32010-06-27 00:13:20 +02001751static int wait_for_avail(struct snd_pcm_substream *substream,
Takashi Iwai13075512008-01-08 18:08:14 +01001752 snd_pcm_uframes_t *availp)
1753{
1754 struct snd_pcm_runtime *runtime = substream->runtime;
1755 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1756 wait_queue_t wait;
1757 int err = 0;
1758 snd_pcm_uframes_t avail = 0;
1759 long tout;
1760
1761 init_waitqueue_entry(&wait, current);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001762 add_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001763 for (;;) {
1764 if (signal_pending(current)) {
1765 err = -ERESTARTSYS;
1766 break;
1767 }
1768 set_current_state(TASK_INTERRUPTIBLE);
1769 snd_pcm_stream_unlock_irq(substream);
1770 tout = schedule_timeout(msecs_to_jiffies(10000));
1771 snd_pcm_stream_lock_irq(substream);
1772 switch (runtime->status->state) {
1773 case SNDRV_PCM_STATE_SUSPENDED:
1774 err = -ESTRPIPE;
1775 goto _endloop;
1776 case SNDRV_PCM_STATE_XRUN:
1777 err = -EPIPE;
1778 goto _endloop;
1779 case SNDRV_PCM_STATE_DRAINING:
1780 if (is_playback)
1781 err = -EPIPE;
1782 else
1783 avail = 0; /* indicate draining */
1784 goto _endloop;
1785 case SNDRV_PCM_STATE_OPEN:
1786 case SNDRV_PCM_STATE_SETUP:
1787 case SNDRV_PCM_STATE_DISCONNECTED:
1788 err = -EBADFD;
1789 goto _endloop;
1790 }
1791 if (!tout) {
1792 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1793 is_playback ? "playback" : "capture");
1794 err = -EIO;
1795 break;
1796 }
1797 if (is_playback)
1798 avail = snd_pcm_playback_avail(runtime);
1799 else
1800 avail = snd_pcm_capture_avail(runtime);
David Dillow5daeba32010-06-27 00:13:20 +02001801 if (avail >= runtime->twake)
Takashi Iwai13075512008-01-08 18:08:14 +01001802 break;
1803 }
1804 _endloop:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001805 remove_wait_queue(&runtime->tsleep, &wait);
Takashi Iwai13075512008-01-08 18:08:14 +01001806 *availp = avail;
1807 return err;
1808}
1809
Takashi Iwai877211f2005-11-17 13:59:38 +01001810static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned int hwoff,
1812 unsigned long data, unsigned int off,
1813 snd_pcm_uframes_t frames)
1814{
Takashi Iwai877211f2005-11-17 13:59:38 +01001815 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int err;
1817 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1818 if (substream->ops->copy) {
1819 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1820 return err;
1821 } else {
1822 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1824 return -EFAULT;
1825 }
1826 return 0;
1827}
1828
Takashi Iwai877211f2005-11-17 13:59:38 +01001829typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 unsigned long data, unsigned int off,
1831 snd_pcm_uframes_t size);
1832
Takashi Iwai877211f2005-11-17 13:59:38 +01001833static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 unsigned long data,
1835 snd_pcm_uframes_t size,
1836 int nonblock,
1837 transfer_f transfer)
1838{
Takashi Iwai877211f2005-11-17 13:59:38 +01001839 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 snd_pcm_uframes_t xfer = 0;
1841 snd_pcm_uframes_t offset = 0;
1842 int err = 0;
1843
1844 if (size == 0)
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 snd_pcm_stream_lock_irq(substream);
1848 switch (runtime->status->state) {
1849 case SNDRV_PCM_STATE_PREPARED:
1850 case SNDRV_PCM_STATE_RUNNING:
1851 case SNDRV_PCM_STATE_PAUSED:
1852 break;
1853 case SNDRV_PCM_STATE_XRUN:
1854 err = -EPIPE;
1855 goto _end_unlock;
1856 case SNDRV_PCM_STATE_SUSPENDED:
1857 err = -ESTRPIPE;
1858 goto _end_unlock;
1859 default:
1860 err = -EBADFD;
1861 goto _end_unlock;
1862 }
1863
David Dillow5daeba32010-06-27 00:13:20 +02001864 runtime->twake = runtime->control->avail_min ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 while (size > 0) {
1866 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1867 snd_pcm_uframes_t avail;
1868 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001869 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 snd_pcm_update_hw_ptr(substream);
1871 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001872 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (nonblock) {
1874 err = -EAGAIN;
1875 goto _end_unlock;
1876 }
David Dillow5daeba32010-06-27 00:13:20 +02001877 runtime->twake = min_t(snd_pcm_uframes_t, size,
1878 runtime->control->avail_min ? : 1);
1879 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01001880 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 frames = size > avail ? avail : size;
1884 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1885 if (frames > cont)
1886 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001887 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001888 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001889 snd_pcm_stream_unlock_irq(substream);
1890 return -EINVAL;
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 appl_ptr = runtime->control->appl_ptr;
1893 appl_ofs = appl_ptr % runtime->buffer_size;
1894 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001895 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01001897 if (err < 0)
1898 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 switch (runtime->status->state) {
1900 case SNDRV_PCM_STATE_XRUN:
1901 err = -EPIPE;
1902 goto _end_unlock;
1903 case SNDRV_PCM_STATE_SUSPENDED:
1904 err = -ESTRPIPE;
1905 goto _end_unlock;
1906 default:
1907 break;
1908 }
1909 appl_ptr += frames;
1910 if (appl_ptr >= runtime->boundary)
1911 appl_ptr -= runtime->boundary;
1912 runtime->control->appl_ptr = appl_ptr;
1913 if (substream->ops->ack)
1914 substream->ops->ack(substream);
1915
1916 offset += frames;
1917 size -= frames;
1918 xfer += frames;
1919 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1920 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1921 err = snd_pcm_start(substream);
1922 if (err < 0)
1923 goto _end_unlock;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001927 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01001928 if (xfer > 0 && err >= 0)
1929 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1932}
1933
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001934/* sanity-check for read/write methods */
1935static int pcm_sanity_check(struct snd_pcm_substream *substream)
1936{
1937 struct snd_pcm_runtime *runtime;
1938 if (PCM_RUNTIME_CHECK(substream))
1939 return -ENXIO;
1940 runtime = substream->runtime;
1941 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1942 return -EINVAL;
1943 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1944 return -EBADFD;
1945 return 0;
1946}
1947
Takashi Iwai877211f2005-11-17 13:59:38 +01001948snd_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 -07001949{
Takashi Iwai877211f2005-11-17 13:59:38 +01001950 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001954 err = pcm_sanity_check(substream);
1955 if (err < 0)
1956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001958 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1961 runtime->channels > 1)
1962 return -EINVAL;
1963 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1964 snd_pcm_lib_write_transfer);
1965}
1966
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001967EXPORT_SYMBOL(snd_pcm_lib_write);
1968
Takashi Iwai877211f2005-11-17 13:59:38 +01001969static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 unsigned int hwoff,
1971 unsigned long data, unsigned int off,
1972 snd_pcm_uframes_t frames)
1973{
Takashi Iwai877211f2005-11-17 13:59:38 +01001974 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int err;
1976 void __user **bufs = (void __user **)data;
1977 int channels = runtime->channels;
1978 int c;
1979 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001980 if (snd_BUG_ON(!substream->ops->silence))
1981 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 for (c = 0; c < channels; ++c, ++bufs) {
1983 if (*bufs == NULL) {
1984 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1985 return err;
1986 } else {
1987 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1988 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1989 return err;
1990 }
1991 }
1992 } else {
1993 /* default transfer behaviour */
1994 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 for (c = 0; c < channels; ++c, ++bufs) {
1996 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1997 if (*bufs == NULL) {
1998 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1999 } else {
2000 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2001 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2002 return -EFAULT;
2003 }
2004 }
2005 }
2006 return 0;
2007}
2008
Takashi Iwai877211f2005-11-17 13:59:38 +01002009snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 void __user **bufs,
2011 snd_pcm_uframes_t frames)
2012{
Takashi Iwai877211f2005-11-17 13:59:38 +01002013 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002015 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002017 err = pcm_sanity_check(substream);
2018 if (err < 0)
2019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002021 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2024 return -EINVAL;
2025 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2026 nonblock, snd_pcm_lib_writev_transfer);
2027}
2028
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002029EXPORT_SYMBOL(snd_pcm_lib_writev);
2030
Takashi Iwai877211f2005-11-17 13:59:38 +01002031static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 unsigned int hwoff,
2033 unsigned long data, unsigned int off,
2034 snd_pcm_uframes_t frames)
2035{
Takashi Iwai877211f2005-11-17 13:59:38 +01002036 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int err;
2038 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2039 if (substream->ops->copy) {
2040 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2041 return err;
2042 } else {
2043 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2045 return -EFAULT;
2046 }
2047 return 0;
2048}
2049
Takashi Iwai877211f2005-11-17 13:59:38 +01002050static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 unsigned long data,
2052 snd_pcm_uframes_t size,
2053 int nonblock,
2054 transfer_f transfer)
2055{
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 snd_pcm_uframes_t xfer = 0;
2058 snd_pcm_uframes_t offset = 0;
2059 int err = 0;
2060
2061 if (size == 0)
2062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 snd_pcm_stream_lock_irq(substream);
2065 switch (runtime->status->state) {
2066 case SNDRV_PCM_STATE_PREPARED:
2067 if (size >= runtime->start_threshold) {
2068 err = snd_pcm_start(substream);
2069 if (err < 0)
2070 goto _end_unlock;
2071 }
2072 break;
2073 case SNDRV_PCM_STATE_DRAINING:
2074 case SNDRV_PCM_STATE_RUNNING:
2075 case SNDRV_PCM_STATE_PAUSED:
2076 break;
2077 case SNDRV_PCM_STATE_XRUN:
2078 err = -EPIPE;
2079 goto _end_unlock;
2080 case SNDRV_PCM_STATE_SUSPENDED:
2081 err = -ESTRPIPE;
2082 goto _end_unlock;
2083 default:
2084 err = -EBADFD;
2085 goto _end_unlock;
2086 }
2087
David Dillow5daeba32010-06-27 00:13:20 +02002088 runtime->twake = runtime->control->avail_min ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 while (size > 0) {
2090 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2091 snd_pcm_uframes_t avail;
2092 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01002093 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01002096 if (!avail) {
2097 if (runtime->status->state ==
2098 SNDRV_PCM_STATE_DRAINING) {
2099 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto _end_unlock;
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (nonblock) {
2103 err = -EAGAIN;
2104 goto _end_unlock;
2105 }
David Dillow5daeba32010-06-27 00:13:20 +02002106 runtime->twake = min_t(snd_pcm_uframes_t, size,
2107 runtime->control->avail_min ? : 1);
2108 err = wait_for_avail(substream, &avail);
Takashi Iwai13075512008-01-08 18:08:14 +01002109 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01002111 if (!avail)
2112 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 frames = size > avail ? avail : size;
2115 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2116 if (frames > cont)
2117 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002118 if (snd_BUG_ON(!frames)) {
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002119 runtime->twake = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002120 snd_pcm_stream_unlock_irq(substream);
2121 return -EINVAL;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 appl_ptr = runtime->control->appl_ptr;
2124 appl_ofs = appl_ptr % runtime->buffer_size;
2125 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002126 err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 snd_pcm_stream_lock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +01002128 if (err < 0)
2129 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 switch (runtime->status->state) {
2131 case SNDRV_PCM_STATE_XRUN:
2132 err = -EPIPE;
2133 goto _end_unlock;
2134 case SNDRV_PCM_STATE_SUSPENDED:
2135 err = -ESTRPIPE;
2136 goto _end_unlock;
2137 default:
2138 break;
2139 }
2140 appl_ptr += frames;
2141 if (appl_ptr >= runtime->boundary)
2142 appl_ptr -= runtime->boundary;
2143 runtime->control->appl_ptr = appl_ptr;
2144 if (substream->ops->ack)
2145 substream->ops->ack(substream);
2146
2147 offset += frames;
2148 size -= frames;
2149 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151 _end_unlock:
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01002152 runtime->twake = 0;
Jaroslav Kysela12509322010-01-07 15:36:31 +01002153 if (xfer > 0 && err >= 0)
2154 snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2157}
2158
Takashi Iwai877211f2005-11-17 13:59:38 +01002159snd_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 -07002160{
Takashi Iwai877211f2005-11-17 13:59:38 +01002161 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002163 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002165 err = pcm_sanity_check(substream);
2166 if (err < 0)
2167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002169 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2171 return -EINVAL;
2172 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2173}
2174
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002175EXPORT_SYMBOL(snd_pcm_lib_read);
2176
Takashi Iwai877211f2005-11-17 13:59:38 +01002177static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 unsigned int hwoff,
2179 unsigned long data, unsigned int off,
2180 snd_pcm_uframes_t frames)
2181{
Takashi Iwai877211f2005-11-17 13:59:38 +01002182 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int err;
2184 void __user **bufs = (void __user **)data;
2185 int channels = runtime->channels;
2186 int c;
2187 if (substream->ops->copy) {
2188 for (c = 0; c < channels; ++c, ++bufs) {
2189 char __user *buf;
2190 if (*bufs == NULL)
2191 continue;
2192 buf = *bufs + samples_to_bytes(runtime, off);
2193 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2194 return err;
2195 }
2196 } else {
2197 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 for (c = 0; c < channels; ++c, ++bufs) {
2199 char *hwbuf;
2200 char __user *buf;
2201 if (*bufs == NULL)
2202 continue;
2203
2204 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2205 buf = *bufs + samples_to_bytes(runtime, off);
2206 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2207 return -EFAULT;
2208 }
2209 }
2210 return 0;
2211}
2212
Takashi Iwai877211f2005-11-17 13:59:38 +01002213snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 void __user **bufs,
2215 snd_pcm_uframes_t frames)
2216{
Takashi Iwai877211f2005-11-17 13:59:38 +01002217 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002219 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002221 err = pcm_sanity_check(substream);
2222 if (err < 0)
2223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2226 return -EBADFD;
2227
Takashi Iwai0df63e42006-04-28 15:13:41 +02002228 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2230 return -EINVAL;
2231 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234EXPORT_SYMBOL(snd_pcm_lib_readv);