blob: fbb2e391591ea2c1a5e1933c65f6b946e7a0bd2c [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>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/info.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/timer.h>
31
32/*
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
38 *
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
40 */
Takashi Iwai877211f2005-11-17 13:59:38 +010041void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai877211f2005-11-17 13:59:38 +010043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 snd_pcm_uframes_t frames, ofs, transfer;
45
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
50 if (n < 0)
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
54 else
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
57 }
Takashi Iwai235475c2005-12-07 15:28:07 +010058 if (runtime->silence_filled >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
62 return;
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
66 } else {
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
72 runtime->boundary;
73 } else {
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020081 runtime->silence_start = new_hw_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } else {
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020083 runtime->silence_start = ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 frames = runtime->buffer_size - runtime->silence_filled;
87 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +020088 if (snd_BUG_ON(frames > runtime->buffer_size))
89 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (frames == 0)
91 return;
Clemens Ladisch9a826dd2006-10-23 16:26:57 +020092 ofs = runtime->silence_start % runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 while (frames > 0) {
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
98 int err;
99 err = substream->ops->silence(substream, -1, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200100 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } else {
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
104 }
105 } else {
106 unsigned int c;
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
110 int err;
111 err = substream->ops->silence(substream, c, ofs, transfer);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200112 snd_BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 } else {
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
119 }
120 }
121 }
122 runtime->silence_filled += transfer;
123 frames -= transfer;
124 ofs = 0;
125 }
126}
127
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100128#ifdef CONFIG_SND_PCM_XRUN_DEBUG
129#define xrun_debug(substream) ((substream)->pstr->xrun_debug)
130#else
131#define xrun_debug(substream) 0
132#endif
133
134#define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
136 dump_stack(); \
137 } while (0)
138
Takashi Iwai877211f2005-11-17 13:59:38 +0100139static void xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100142 if (xrun_debug(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100147 dump_stack_on_xrun(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Takashi Iwai98204642009-03-19 09:59:21 +0100151static snd_pcm_uframes_t
152snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 snd_pcm_uframes_t pos;
156
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (pos >= runtime->buffer_size) {
Takashi Iwai5f513e12009-03-19 10:01:47 +0100163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
168 }
169 pos = 0;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 pos -= pos % runtime->min_align;
172 return pos;
173}
174
Takashi Iwai98204642009-03-19 09:59:21 +0100175static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 snd_pcm_uframes_t avail;
179
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
182 else
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
189 else
190 xrun(substream);
191 return -EPIPE;
192 }
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
195 return 0;
196}
197
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100198#define hw_ptr_error(substream, fmt, args...) \
199 do { \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
Takashi Iwaicad377a2009-03-19 09:55:15 +0100202 snd_printd("PCM: " fmt, ##args); \
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100203 } \
204 dump_stack_on_xrun(substream); \
205 } \
206 } while (0)
207
Takashi Iwai98204642009-03-19 09:59:21 +0100208static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Takashi Iwai877211f2005-11-17 13:59:38 +0100210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100212 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 snd_pcm_sframes_t delta;
214
215 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
216 if (pos == SNDRV_PCM_POS_XRUN) {
217 xrun(substream);
218 return -EPIPE;
219 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100220 hw_base = runtime->hw_ptr_base;
221 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100223 delta = new_hw_ptr - hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100224 if (hw_ptr_interrupt >= runtime->boundary) {
Takashi Iwai8b22d942009-03-20 16:26:15 +0100225 hw_ptr_interrupt -= runtime->boundary;
226 if (hw_base < runtime->boundary / 2)
227 /* hw_base was already lapped; recalc delta */
Takashi Iwaided652f2009-03-19 10:08:49 +0100228 delta = new_hw_ptr - hw_ptr_interrupt;
229 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100230 if (delta < 0) {
231 delta += runtime->buffer_size;
232 if (delta < 0) {
233 hw_ptr_error(substream,
234 "Unexpected hw_pointer value "
235 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
236 substream->stream, (long)pos,
237 (long)hw_ptr_interrupt);
238 /* rebase to interrupt position */
239 hw_base = new_hw_ptr = hw_ptr_interrupt;
Takashi Iwaided652f2009-03-19 10:08:49 +0100240 /* align hw_base to buffer_size */
241 hw_base -= hw_base % runtime->buffer_size;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100242 delta = 0;
243 } else {
244 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100245 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100246 hw_base = 0;
247 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100250 if (delta > runtime->period_size) {
251 hw_ptr_error(substream,
252 "Lost interrupts? "
253 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
254 substream->stream, (long)delta,
255 (long)hw_ptr_interrupt);
256 /* rebase hw_ptr_interrupt */
257 hw_ptr_interrupt =
258 new_hw_ptr - new_hw_ptr % runtime->period_size;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
261 runtime->silence_size > 0)
262 snd_pcm_playback_silence(substream, new_hw_ptr);
263
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100264 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 runtime->status->hw_ptr = new_hw_ptr;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100266 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return snd_pcm_update_hw_ptr_post(substream, runtime);
269}
270
271/* CAUTION: call it with irq disabled */
Takashi Iwai877211f2005-11-17 13:59:38 +0100272int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Takashi Iwai877211f2005-11-17 13:59:38 +0100274 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 snd_pcm_uframes_t pos;
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100276 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 snd_pcm_sframes_t delta;
278
279 old_hw_ptr = runtime->status->hw_ptr;
280 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
281 if (pos == SNDRV_PCM_POS_XRUN) {
282 xrun(substream);
283 return -EPIPE;
284 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100285 hw_base = runtime->hw_ptr_base;
286 new_hw_ptr = hw_base + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100288 delta = new_hw_ptr - old_hw_ptr;
289 if (delta < 0) {
290 delta += runtime->buffer_size;
291 if (delta < 0) {
292 hw_ptr_error(substream,
293 "Unexpected hw_pointer value [2] "
294 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
295 substream->stream, (long)pos,
296 (long)old_hw_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298 }
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100299 hw_base += runtime->buffer_size;
Takashi Iwai8b22d942009-03-20 16:26:15 +0100300 if (hw_base >= runtime->boundary)
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100301 hw_base = 0;
302 new_hw_ptr = hw_base + pos;
303 }
304 if (delta > runtime->period_size && runtime->periods > 1) {
305 hw_ptr_error(substream,
306 "hw_ptr skipping! "
307 "(pos=%ld, delta=%ld, period=%ld)\n",
308 (long)pos, (long)delta,
309 (long)runtime->period_size);
310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
313 runtime->silence_size > 0)
314 snd_pcm_playback_silence(substream, new_hw_ptr);
315
Takashi Iwaied3da3d2009-03-03 17:00:15 +0100316 runtime->hw_ptr_base = hw_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 runtime->status->hw_ptr = new_hw_ptr;
318
319 return snd_pcm_update_hw_ptr_post(substream, runtime);
320}
321
322/**
323 * snd_pcm_set_ops - set the PCM operators
324 * @pcm: the pcm instance
325 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
326 * @ops: the operator table
327 *
328 * Sets the given PCM operators to the pcm instance.
329 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100330void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Takashi Iwai877211f2005-11-17 13:59:38 +0100332 struct snd_pcm_str *stream = &pcm->streams[direction];
333 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 for (substream = stream->substream; substream != NULL; substream = substream->next)
336 substream->ops = ops;
337}
338
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200339EXPORT_SYMBOL(snd_pcm_set_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/**
342 * snd_pcm_sync - set the PCM sync id
343 * @substream: the pcm substream
344 *
345 * Sets the PCM sync identifier for the card.
346 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100347void snd_pcm_set_sync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Takashi Iwai877211f2005-11-17 13:59:38 +0100349 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 runtime->sync.id32[0] = substream->pcm->card->number;
352 runtime->sync.id32[1] = -1;
353 runtime->sync.id32[2] = -1;
354 runtime->sync.id32[3] = -1;
355}
356
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200357EXPORT_SYMBOL(snd_pcm_set_sync);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*
360 * Standard ioctl routine
361 */
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static inline unsigned int div32(unsigned int a, unsigned int b,
364 unsigned int *r)
365{
366 if (b == 0) {
367 *r = 0;
368 return UINT_MAX;
369 }
370 *r = a % b;
371 return a / b;
372}
373
374static inline unsigned int div_down(unsigned int a, unsigned int b)
375{
376 if (b == 0)
377 return UINT_MAX;
378 return a / b;
379}
380
381static inline unsigned int div_up(unsigned int a, unsigned int b)
382{
383 unsigned int r;
384 unsigned int q;
385 if (b == 0)
386 return UINT_MAX;
387 q = div32(a, b, &r);
388 if (r)
389 ++q;
390 return q;
391}
392
393static inline unsigned int mul(unsigned int a, unsigned int b)
394{
395 if (a == 0)
396 return 0;
397 if (div_down(UINT_MAX, a) < b)
398 return UINT_MAX;
399 return a * b;
400}
401
402static inline unsigned int muldiv32(unsigned int a, unsigned int b,
403 unsigned int c, unsigned int *r)
404{
405 u_int64_t n = (u_int64_t) a * b;
406 if (c == 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200407 snd_BUG_ON(!n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *r = 0;
409 return UINT_MAX;
410 }
411 div64_32(&n, c, r);
412 if (n >= UINT_MAX) {
413 *r = 0;
414 return UINT_MAX;
415 }
416 return n;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 * snd_interval_refine - refine the interval value of configurator
421 * @i: the interval value to refine
422 * @v: the interval value to refer to
423 *
424 * Refines the interval value with the reference value.
425 * The interval is changed to the range satisfying both intervals.
426 * The interval status (min, max, integer, etc.) are evaluated.
427 *
428 * Returns non-zero if the value is changed, zero if not changed.
429 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100430int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 int changed = 0;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200433 if (snd_BUG_ON(snd_interval_empty(i)))
434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (i->min < v->min) {
436 i->min = v->min;
437 i->openmin = v->openmin;
438 changed = 1;
439 } else if (i->min == v->min && !i->openmin && v->openmin) {
440 i->openmin = 1;
441 changed = 1;
442 }
443 if (i->max > v->max) {
444 i->max = v->max;
445 i->openmax = v->openmax;
446 changed = 1;
447 } else if (i->max == v->max && !i->openmax && v->openmax) {
448 i->openmax = 1;
449 changed = 1;
450 }
451 if (!i->integer && v->integer) {
452 i->integer = 1;
453 changed = 1;
454 }
455 if (i->integer) {
456 if (i->openmin) {
457 i->min++;
458 i->openmin = 0;
459 }
460 if (i->openmax) {
461 i->max--;
462 i->openmax = 0;
463 }
464 } else if (!i->openmin && !i->openmax && i->min == i->max)
465 i->integer = 1;
466 if (snd_interval_checkempty(i)) {
467 snd_interval_none(i);
468 return -EINVAL;
469 }
470 return changed;
471}
472
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200473EXPORT_SYMBOL(snd_interval_refine);
474
Takashi Iwai877211f2005-11-17 13:59:38 +0100475static int snd_interval_refine_first(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200477 if (snd_BUG_ON(snd_interval_empty(i)))
478 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (snd_interval_single(i))
480 return 0;
481 i->max = i->min;
482 i->openmax = i->openmin;
483 if (i->openmax)
484 i->max++;
485 return 1;
486}
487
Takashi Iwai877211f2005-11-17 13:59:38 +0100488static int snd_interval_refine_last(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200490 if (snd_BUG_ON(snd_interval_empty(i)))
491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (snd_interval_single(i))
493 return 0;
494 i->min = i->max;
495 i->openmin = i->openmax;
496 if (i->openmin)
497 i->min--;
498 return 1;
499}
500
Takashi Iwai877211f2005-11-17 13:59:38 +0100501void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 if (a->empty || b->empty) {
504 snd_interval_none(c);
505 return;
506 }
507 c->empty = 0;
508 c->min = mul(a->min, b->min);
509 c->openmin = (a->openmin || b->openmin);
510 c->max = mul(a->max, b->max);
511 c->openmax = (a->openmax || b->openmax);
512 c->integer = (a->integer && b->integer);
513}
514
515/**
516 * snd_interval_div - refine the interval value with division
Takashi Iwaidf8db932005-09-07 13:38:19 +0200517 * @a: dividend
518 * @b: divisor
519 * @c: quotient
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 *
521 * c = a / b
522 *
523 * Returns non-zero if the value is changed, zero if not changed.
524 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100525void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 unsigned int r;
528 if (a->empty || b->empty) {
529 snd_interval_none(c);
530 return;
531 }
532 c->empty = 0;
533 c->min = div32(a->min, b->max, &r);
534 c->openmin = (r || a->openmin || b->openmax);
535 if (b->min > 0) {
536 c->max = div32(a->max, b->min, &r);
537 if (r) {
538 c->max++;
539 c->openmax = 1;
540 } else
541 c->openmax = (a->openmax || b->openmin);
542 } else {
543 c->max = UINT_MAX;
544 c->openmax = 0;
545 }
546 c->integer = 0;
547}
548
549/**
550 * snd_interval_muldivk - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200551 * @a: dividend 1
552 * @b: dividend 2
553 * @k: divisor (as integer)
554 * @c: result
555 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * c = a * b / k
557 *
558 * Returns non-zero if the value is changed, zero if not changed.
559 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100560void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
561 unsigned int k, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 unsigned int r;
564 if (a->empty || b->empty) {
565 snd_interval_none(c);
566 return;
567 }
568 c->empty = 0;
569 c->min = muldiv32(a->min, b->min, k, &r);
570 c->openmin = (r || a->openmin || b->openmin);
571 c->max = muldiv32(a->max, b->max, k, &r);
572 if (r) {
573 c->max++;
574 c->openmax = 1;
575 } else
576 c->openmax = (a->openmax || b->openmax);
577 c->integer = 0;
578}
579
580/**
581 * snd_interval_mulkdiv - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200582 * @a: dividend 1
583 * @k: dividend 2 (as integer)
584 * @b: divisor
585 * @c: result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * c = a * k / b
588 *
589 * Returns non-zero if the value is changed, zero if not changed.
590 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100591void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
592 const struct snd_interval *b, struct snd_interval *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 unsigned int r;
595 if (a->empty || b->empty) {
596 snd_interval_none(c);
597 return;
598 }
599 c->empty = 0;
600 c->min = muldiv32(a->min, k, b->max, &r);
601 c->openmin = (r || a->openmin || b->openmax);
602 if (b->min > 0) {
603 c->max = muldiv32(a->max, k, b->min, &r);
604 if (r) {
605 c->max++;
606 c->openmax = 1;
607 } else
608 c->openmax = (a->openmax || b->openmin);
609 } else {
610 c->max = UINT_MAX;
611 c->openmax = 0;
612 }
613 c->integer = 0;
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* ---- */
617
618
619/**
620 * snd_interval_ratnum - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200621 * @i: interval to refine
622 * @rats_count: number of ratnum_t
623 * @rats: ratnum_t array
624 * @nump: pointer to store the resultant numerator
625 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
627 * Returns non-zero if the value is changed, zero if not changed.
628 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100629int snd_interval_ratnum(struct snd_interval *i,
630 unsigned int rats_count, struct snd_ratnum *rats,
631 unsigned int *nump, unsigned int *denp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 unsigned int best_num, best_diff, best_den;
634 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100635 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int err;
637
638 best_num = best_den = best_diff = 0;
639 for (k = 0; k < rats_count; ++k) {
640 unsigned int num = rats[k].num;
641 unsigned int den;
642 unsigned int q = i->min;
643 int diff;
644 if (q == 0)
645 q = 1;
646 den = div_down(num, q);
647 if (den < rats[k].den_min)
648 continue;
649 if (den > rats[k].den_max)
650 den = rats[k].den_max;
651 else {
652 unsigned int r;
653 r = (den - rats[k].den_min) % rats[k].den_step;
654 if (r != 0)
655 den -= r;
656 }
657 diff = num - q * den;
658 if (best_num == 0 ||
659 diff * best_den < best_diff * den) {
660 best_diff = diff;
661 best_den = den;
662 best_num = num;
663 }
664 }
665 if (best_den == 0) {
666 i->empty = 1;
667 return -EINVAL;
668 }
669 t.min = div_down(best_num, best_den);
670 t.openmin = !!(best_num % best_den);
671
672 best_num = best_den = best_diff = 0;
673 for (k = 0; k < rats_count; ++k) {
674 unsigned int num = rats[k].num;
675 unsigned int den;
676 unsigned int q = i->max;
677 int diff;
678 if (q == 0) {
679 i->empty = 1;
680 return -EINVAL;
681 }
682 den = div_up(num, q);
683 if (den > rats[k].den_max)
684 continue;
685 if (den < rats[k].den_min)
686 den = rats[k].den_min;
687 else {
688 unsigned int r;
689 r = (den - rats[k].den_min) % rats[k].den_step;
690 if (r != 0)
691 den += rats[k].den_step - r;
692 }
693 diff = q * den - num;
694 if (best_num == 0 ||
695 diff * best_den < best_diff * den) {
696 best_diff = diff;
697 best_den = den;
698 best_num = num;
699 }
700 }
701 if (best_den == 0) {
702 i->empty = 1;
703 return -EINVAL;
704 }
705 t.max = div_up(best_num, best_den);
706 t.openmax = !!(best_num % best_den);
707 t.integer = 0;
708 err = snd_interval_refine(i, &t);
709 if (err < 0)
710 return err;
711
712 if (snd_interval_single(i)) {
713 if (nump)
714 *nump = best_num;
715 if (denp)
716 *denp = best_den;
717 }
718 return err;
719}
720
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200721EXPORT_SYMBOL(snd_interval_ratnum);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/**
724 * snd_interval_ratden - refine the interval value
Takashi Iwaidf8db932005-09-07 13:38:19 +0200725 * @i: interval to refine
Takashi Iwai877211f2005-11-17 13:59:38 +0100726 * @rats_count: number of struct ratden
727 * @rats: struct ratden array
Takashi Iwaidf8db932005-09-07 13:38:19 +0200728 * @nump: pointer to store the resultant numerator
729 * @denp: pointer to store the resultant denominator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
731 * Returns non-zero if the value is changed, zero if not changed.
732 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100733static int snd_interval_ratden(struct snd_interval *i,
734 unsigned int rats_count, struct snd_ratden *rats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned int *nump, unsigned int *denp)
736{
737 unsigned int best_num, best_diff, best_den;
738 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100739 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int err;
741
742 best_num = best_den = best_diff = 0;
743 for (k = 0; k < rats_count; ++k) {
744 unsigned int num;
745 unsigned int den = rats[k].den;
746 unsigned int q = i->min;
747 int diff;
748 num = mul(q, den);
749 if (num > rats[k].num_max)
750 continue;
751 if (num < rats[k].num_min)
752 num = rats[k].num_max;
753 else {
754 unsigned int r;
755 r = (num - rats[k].num_min) % rats[k].num_step;
756 if (r != 0)
757 num += rats[k].num_step - r;
758 }
759 diff = num - q * den;
760 if (best_num == 0 ||
761 diff * best_den < best_diff * den) {
762 best_diff = diff;
763 best_den = den;
764 best_num = num;
765 }
766 }
767 if (best_den == 0) {
768 i->empty = 1;
769 return -EINVAL;
770 }
771 t.min = div_down(best_num, best_den);
772 t.openmin = !!(best_num % best_den);
773
774 best_num = best_den = best_diff = 0;
775 for (k = 0; k < rats_count; ++k) {
776 unsigned int num;
777 unsigned int den = rats[k].den;
778 unsigned int q = i->max;
779 int diff;
780 num = mul(q, den);
781 if (num < rats[k].num_min)
782 continue;
783 if (num > rats[k].num_max)
784 num = rats[k].num_max;
785 else {
786 unsigned int r;
787 r = (num - rats[k].num_min) % rats[k].num_step;
788 if (r != 0)
789 num -= r;
790 }
791 diff = q * den - num;
792 if (best_num == 0 ||
793 diff * best_den < best_diff * den) {
794 best_diff = diff;
795 best_den = den;
796 best_num = num;
797 }
798 }
799 if (best_den == 0) {
800 i->empty = 1;
801 return -EINVAL;
802 }
803 t.max = div_up(best_num, best_den);
804 t.openmax = !!(best_num % best_den);
805 t.integer = 0;
806 err = snd_interval_refine(i, &t);
807 if (err < 0)
808 return err;
809
810 if (snd_interval_single(i)) {
811 if (nump)
812 *nump = best_num;
813 if (denp)
814 *denp = best_den;
815 }
816 return err;
817}
818
819/**
820 * snd_interval_list - refine the interval value from the list
821 * @i: the interval value to refine
822 * @count: the number of elements in the list
823 * @list: the value list
824 * @mask: the bit-mask to evaluate
825 *
826 * Refines the interval value from the list.
827 * When mask is non-zero, only the elements corresponding to bit 1 are
828 * evaluated.
829 *
830 * Returns non-zero if the value is changed, zero if not changed.
831 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100832int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 unsigned int k;
835 int changed = 0;
Takashi Iwai0981a262007-02-01 14:53:49 +0100836
837 if (!count) {
838 i->empty = 1;
839 return -EINVAL;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 for (k = 0; k < count; k++) {
842 if (mask && !(mask & (1 << k)))
843 continue;
844 if (i->min == list[k] && !i->openmin)
845 goto _l1;
846 if (i->min < list[k]) {
847 i->min = list[k];
848 i->openmin = 0;
849 changed = 1;
850 goto _l1;
851 }
852 }
853 i->empty = 1;
854 return -EINVAL;
855 _l1:
856 for (k = count; k-- > 0;) {
857 if (mask && !(mask & (1 << k)))
858 continue;
859 if (i->max == list[k] && !i->openmax)
860 goto _l2;
861 if (i->max > list[k]) {
862 i->max = list[k];
863 i->openmax = 0;
864 changed = 1;
865 goto _l2;
866 }
867 }
868 i->empty = 1;
869 return -EINVAL;
870 _l2:
871 if (snd_interval_checkempty(i)) {
872 i->empty = 1;
873 return -EINVAL;
874 }
875 return changed;
876}
877
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200878EXPORT_SYMBOL(snd_interval_list);
879
Takashi Iwai877211f2005-11-17 13:59:38 +0100880static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 unsigned int n;
883 int changed = 0;
884 n = (i->min - min) % step;
885 if (n != 0 || i->openmin) {
886 i->min += step - n;
887 changed = 1;
888 }
889 n = (i->max - min) % step;
890 if (n != 0 || i->openmax) {
891 i->max -= n;
892 changed = 1;
893 }
894 if (snd_interval_checkempty(i)) {
895 i->empty = 1;
896 return -EINVAL;
897 }
898 return changed;
899}
900
901/* Info constraints helpers */
902
903/**
904 * snd_pcm_hw_rule_add - add the hw-constraint rule
905 * @runtime: the pcm runtime instance
906 * @cond: condition bits
907 * @var: the variable to evaluate
908 * @func: the evaluation function
909 * @private: the private data pointer passed to function
910 * @dep: the dependent variables
911 *
912 * Returns zero if successful, or a negative error code on failure.
913 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100914int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int var,
916 snd_pcm_hw_rule_func_t func, void *private,
917 int dep, ...)
918{
Takashi Iwai877211f2005-11-17 13:59:38 +0100919 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
920 struct snd_pcm_hw_rule *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned int k;
922 va_list args;
923 va_start(args, dep);
924 if (constrs->rules_num >= constrs->rules_all) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 struct snd_pcm_hw_rule *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unsigned int new_rules = constrs->rules_all + 16;
927 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
928 if (!new)
929 return -ENOMEM;
930 if (constrs->rules) {
931 memcpy(new, constrs->rules,
932 constrs->rules_num * sizeof(*c));
933 kfree(constrs->rules);
934 }
935 constrs->rules = new;
936 constrs->rules_all = new_rules;
937 }
938 c = &constrs->rules[constrs->rules_num];
939 c->cond = cond;
940 c->func = func;
941 c->var = var;
942 c->private = private;
943 k = 0;
944 while (1) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200945 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
946 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 c->deps[k++] = dep;
948 if (dep < 0)
949 break;
950 dep = va_arg(args, int);
951 }
952 constrs->rules_num++;
953 va_end(args);
954 return 0;
955}
956
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200957EXPORT_SYMBOL(snd_pcm_hw_rule_add);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700960 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200961 * @runtime: PCM runtime instance
962 * @var: hw_params variable to apply the mask
963 * @mask: the bitmap mask
964 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700965 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100967int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 u_int32_t mask)
969{
Takashi Iwai877211f2005-11-17 13:59:38 +0100970 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
971 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 *maskp->bits &= mask;
973 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
974 if (*maskp->bits == 0)
975 return -EINVAL;
976 return 0;
977}
978
979/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700980 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwaidf8db932005-09-07 13:38:19 +0200981 * @runtime: PCM runtime instance
982 * @var: hw_params variable to apply the mask
983 * @mask: the 64bit bitmap mask
984 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700985 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100987int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 u_int64_t mask)
989{
Takashi Iwai877211f2005-11-17 13:59:38 +0100990 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
991 struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 maskp->bits[0] &= (u_int32_t)mask;
993 maskp->bits[1] &= (u_int32_t)(mask >> 32);
994 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
995 if (! maskp->bits[0] && ! maskp->bits[1])
996 return -EINVAL;
997 return 0;
998}
999
1000/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001001 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001002 * @runtime: PCM runtime instance
1003 * @var: hw_params variable to apply the integer constraint
1004 *
1005 * Apply the constraint of integer to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001007int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return snd_interval_setinteger(constrs_interval(constrs, var));
1011}
1012
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001013EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001016 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwaidf8db932005-09-07 13:38:19 +02001017 * @runtime: PCM runtime instance
1018 * @var: hw_params variable to apply the range
1019 * @min: the minimal value
1020 * @max: the maximal value
1021 *
1022 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001024int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned int min, unsigned int max)
1026{
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 t.min = min;
1030 t.max = max;
1031 t.openmin = t.openmax = 0;
1032 t.integer = 0;
1033 return snd_interval_refine(constrs_interval(constrs, var), &t);
1034}
1035
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001036EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1037
Takashi Iwai877211f2005-11-17 13:59:38 +01001038static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1039 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai877211f2005-11-17 13:59:38 +01001041 struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1043}
1044
1045
1046/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001047 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001048 * @runtime: PCM runtime instance
1049 * @cond: condition bits
1050 * @var: hw_params variable to apply the list constraint
1051 * @l: list
1052 *
1053 * Apply the list of constraints to an interval parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001055int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unsigned int cond,
1057 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001058 struct snd_pcm_hw_constraint_list *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 return snd_pcm_hw_rule_add(runtime, cond, var,
1061 snd_pcm_hw_rule_list, l,
1062 var, -1);
1063}
1064
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001065EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1066
Takashi Iwai877211f2005-11-17 13:59:38 +01001067static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1068 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai877211f2005-11-17 13:59:38 +01001070 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 unsigned int num = 0, den = 0;
1072 int err;
1073 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1074 r->nrats, r->rats, &num, &den);
1075 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1076 params->rate_num = num;
1077 params->rate_den = den;
1078 }
1079 return err;
1080}
1081
1082/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001083 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001084 * @runtime: PCM runtime instance
1085 * @cond: condition bits
1086 * @var: hw_params variable to apply the ratnums constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001087 * @r: struct snd_ratnums constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001089int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unsigned int cond,
1091 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001092 struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 return snd_pcm_hw_rule_add(runtime, cond, var,
1095 snd_pcm_hw_rule_ratnums, r,
1096 var, -1);
1097}
1098
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001099EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1100
Takashi Iwai877211f2005-11-17 13:59:38 +01001101static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1102 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unsigned int num = 0, den = 0;
1106 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1107 r->nrats, r->rats, &num, &den);
1108 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1109 params->rate_num = num;
1110 params->rate_den = den;
1111 }
1112 return err;
1113}
1114
1115/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001116 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwaidf8db932005-09-07 13:38:19 +02001117 * @runtime: PCM runtime instance
1118 * @cond: condition bits
1119 * @var: hw_params variable to apply the ratdens constraint
Takashi Iwai877211f2005-11-17 13:59:38 +01001120 * @r: struct snd_ratdens constriants
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001122int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 unsigned int cond,
1124 snd_pcm_hw_param_t var,
Takashi Iwai877211f2005-11-17 13:59:38 +01001125 struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 return snd_pcm_hw_rule_add(runtime, cond, var,
1128 snd_pcm_hw_rule_ratdens, r,
1129 var, -1);
1130}
1131
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001132EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1133
Takashi Iwai877211f2005-11-17 13:59:38 +01001134static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1135 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 unsigned int l = (unsigned long) rule->private;
1138 int width = l & 0xffff;
1139 unsigned int msbits = l >> 16;
Takashi Iwai877211f2005-11-17 13:59:38 +01001140 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (snd_interval_single(i) && snd_interval_value(i) == width)
1142 params->msbits = msbits;
1143 return 0;
1144}
1145
1146/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001147 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001148 * @runtime: PCM runtime instance
1149 * @cond: condition bits
1150 * @width: sample bits width
1151 * @msbits: msbits width
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001153int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 unsigned int cond,
1155 unsigned int width,
1156 unsigned int msbits)
1157{
1158 unsigned long l = (msbits << 16) | width;
1159 return snd_pcm_hw_rule_add(runtime, cond, -1,
1160 snd_pcm_hw_rule_msbits,
1161 (void*) l,
1162 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1163}
1164
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001165EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1166
Takashi Iwai877211f2005-11-17 13:59:38 +01001167static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1168 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 unsigned long step = (unsigned long) rule->private;
1171 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1172}
1173
1174/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001175 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001176 * @runtime: PCM runtime instance
1177 * @cond: condition bits
1178 * @var: hw_params variable to apply the step constraint
1179 * @step: step size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001181int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 unsigned int cond,
1183 snd_pcm_hw_param_t var,
1184 unsigned long step)
1185{
1186 return snd_pcm_hw_rule_add(runtime, cond, var,
1187 snd_pcm_hw_rule_step, (void *) step,
1188 var, -1);
1189}
1190
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001191EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1192
Takashi Iwai877211f2005-11-17 13:59:38 +01001193static 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 -07001194{
Marcin Åšlusarz67c39312007-12-14 12:53:21 +01001195 static unsigned int pow2_sizes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1197 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1198 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1199 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1200 };
1201 return snd_interval_list(hw_param_interval(params, rule->var),
1202 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1203}
1204
1205/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001206 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwaidf8db932005-09-07 13:38:19 +02001207 * @runtime: PCM runtime instance
1208 * @cond: condition bits
1209 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001211int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unsigned int cond,
1213 snd_pcm_hw_param_t var)
1214{
1215 return snd_pcm_hw_rule_add(runtime, cond, var,
1216 snd_pcm_hw_rule_pow2, NULL,
1217 var, -1);
1218}
1219
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001220EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1221
Takashi Iwai877211f2005-11-17 13:59:38 +01001222static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001223 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 if (hw_is_mask(var)) {
1226 snd_mask_any(hw_param_mask(params, var));
1227 params->cmask |= 1 << var;
1228 params->rmask |= 1 << var;
1229 return;
1230 }
1231 if (hw_is_interval(var)) {
1232 snd_interval_any(hw_param_interval(params, var));
1233 params->cmask |= 1 << var;
1234 params->rmask |= 1 << var;
1235 return;
1236 }
1237 snd_BUG();
1238}
1239
Takashi Iwai877211f2005-11-17 13:59:38 +01001240void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 unsigned int k;
1243 memset(params, 0, sizeof(*params));
1244 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1245 _snd_pcm_hw_param_any(params, k);
1246 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1247 _snd_pcm_hw_param_any(params, k);
1248 params->info = ~0U;
1249}
1250
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001251EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001254 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001255 * @params: the hw_params instance
1256 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001257 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001259 * Return the value for field @var if it's fixed in configuration space
1260 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001262int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1263 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 if (hw_is_mask(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001266 const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!snd_mask_single(mask))
1268 return -EINVAL;
1269 if (dir)
1270 *dir = 0;
1271 return snd_mask_value(mask);
1272 }
1273 if (hw_is_interval(var)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001274 const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (!snd_interval_single(i))
1276 return -EINVAL;
1277 if (dir)
1278 *dir = i->openmin;
1279 return snd_interval_value(i);
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EINVAL;
1282}
1283
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001284EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Takashi Iwai877211f2005-11-17 13:59:38 +01001286void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 snd_pcm_hw_param_t var)
1288{
1289 if (hw_is_mask(var)) {
1290 snd_mask_none(hw_param_mask(params, var));
1291 params->cmask |= 1 << var;
1292 params->rmask |= 1 << var;
1293 } else if (hw_is_interval(var)) {
1294 snd_interval_none(hw_param_interval(params, var));
1295 params->cmask |= 1 << var;
1296 params->rmask |= 1 << var;
1297 } else {
1298 snd_BUG();
1299 }
1300}
1301
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001302EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Takashi Iwai877211f2005-11-17 13:59:38 +01001304static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001305 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 int changed;
1308 if (hw_is_mask(var))
1309 changed = snd_mask_refine_first(hw_param_mask(params, var));
1310 else if (hw_is_interval(var))
1311 changed = snd_interval_refine_first(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001312 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (changed) {
1315 params->cmask |= 1 << var;
1316 params->rmask |= 1 << var;
1317 }
1318 return changed;
1319}
1320
1321
1322/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001323 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001324 * @pcm: PCM instance
1325 * @params: the hw_params instance
1326 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001327 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001329 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 * values > minimum. Reduce configuration space accordingly.
1331 * Return the minimum.
1332 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001333int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1334 struct snd_pcm_hw_params *params,
1335 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int changed = _snd_pcm_hw_param_first(params, var);
1338 if (changed < 0)
1339 return changed;
1340 if (params->rmask) {
1341 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001342 if (snd_BUG_ON(err < 0))
1343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 return snd_pcm_hw_param_value(params, var, dir);
1346}
1347
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001348EXPORT_SYMBOL(snd_pcm_hw_param_first);
1349
Takashi Iwai877211f2005-11-17 13:59:38 +01001350static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
Adrian Bunk123992f2005-05-18 18:02:04 +02001351 snd_pcm_hw_param_t var)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 int changed;
1354 if (hw_is_mask(var))
1355 changed = snd_mask_refine_last(hw_param_mask(params, var));
1356 else if (hw_is_interval(var))
1357 changed = snd_interval_refine_last(hw_param_interval(params, var));
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001358 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (changed) {
1361 params->cmask |= 1 << var;
1362 params->rmask |= 1 << var;
1363 }
1364 return changed;
1365}
1366
1367
1368/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001369 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwaidf8db932005-09-07 13:38:19 +02001370 * @pcm: PCM instance
1371 * @params: the hw_params instance
1372 * @var: parameter to retrieve
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001373 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001375 * Inside configuration space defined by @params remove from @var all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * values < maximum. Reduce configuration space accordingly.
1377 * Return the maximum.
1378 */
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001379int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1380 struct snd_pcm_hw_params *params,
1381 snd_pcm_hw_param_t var, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int changed = _snd_pcm_hw_param_last(params, var);
1384 if (changed < 0)
1385 return changed;
1386 if (params->rmask) {
1387 int err = snd_pcm_hw_refine(pcm, params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001388 if (snd_BUG_ON(err < 0))
1389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 return snd_pcm_hw_param_value(params, var, dir);
1392}
1393
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001394EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001397 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwaidf8db932005-09-07 13:38:19 +02001398 * @pcm: PCM instance
1399 * @params: the hw_params instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001401 * Choose one configuration from configuration space defined by @params.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * The configuration chosen is that obtained fixing in this order:
1403 * first access, first format, first subformat, min channels,
1404 * min rate, min period time, max buffer size, min tick time
1405 */
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001406int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1407 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001409 static int vars[] = {
1410 SNDRV_PCM_HW_PARAM_ACCESS,
1411 SNDRV_PCM_HW_PARAM_FORMAT,
1412 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1413 SNDRV_PCM_HW_PARAM_CHANNELS,
1414 SNDRV_PCM_HW_PARAM_RATE,
1415 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1416 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1417 SNDRV_PCM_HW_PARAM_TICK_TIME,
1418 -1
1419 };
1420 int err, *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001422 for (v = vars; *v != -1; v++) {
1423 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1424 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1425 else
1426 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001427 if (snd_BUG_ON(err < 0))
1428 return err;
Takashi Iwai2f4ca8e2006-04-28 15:13:40 +02001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431}
1432
Takashi Iwai877211f2005-11-17 13:59:38 +01001433static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 void *arg)
1435{
Takashi Iwai877211f2005-11-17 13:59:38 +01001436 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 unsigned long flags;
1438 snd_pcm_stream_lock_irqsave(substream, flags);
1439 if (snd_pcm_running(substream) &&
1440 snd_pcm_update_hw_ptr(substream) >= 0)
1441 runtime->status->hw_ptr %= runtime->buffer_size;
1442 else
1443 runtime->status->hw_ptr = 0;
1444 snd_pcm_stream_unlock_irqrestore(substream, flags);
1445 return 0;
1446}
1447
Takashi Iwai877211f2005-11-17 13:59:38 +01001448static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 void *arg)
1450{
Takashi Iwai877211f2005-11-17 13:59:38 +01001451 struct snd_pcm_channel_info *info = arg;
1452 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int width;
1454 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1455 info->offset = -1;
1456 return 0;
1457 }
1458 width = snd_pcm_format_physical_width(runtime->format);
1459 if (width < 0)
1460 return width;
1461 info->offset = 0;
1462 switch (runtime->access) {
1463 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1464 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1465 info->first = info->channel * width;
1466 info->step = runtime->channels * width;
1467 break;
1468 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1469 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1470 {
1471 size_t size = runtime->dma_bytes / runtime->channels;
1472 info->first = info->channel * size * 8;
1473 info->step = width;
1474 break;
1475 }
1476 default:
1477 snd_BUG();
1478 break;
1479 }
1480 return 0;
1481}
1482
1483/**
1484 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1485 * @substream: the pcm substream instance
1486 * @cmd: ioctl command
1487 * @arg: ioctl argument
1488 *
1489 * Processes the generic ioctl commands for PCM.
1490 * Can be passed as the ioctl callback for PCM ops.
1491 *
1492 * Returns zero if successful, or a negative error code on failure.
1493 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001494int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 unsigned int cmd, void *arg)
1496{
1497 switch (cmd) {
1498 case SNDRV_PCM_IOCTL1_INFO:
1499 return 0;
1500 case SNDRV_PCM_IOCTL1_RESET:
1501 return snd_pcm_lib_ioctl_reset(substream, arg);
1502 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1503 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1504 }
1505 return -ENXIO;
1506}
1507
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001508EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/**
1511 * snd_pcm_period_elapsed - update the pcm status for the next period
1512 * @substream: the pcm substream instance
1513 *
1514 * This function is called from the interrupt handler when the
1515 * PCM has processed the period size. It will update the current
Takashi Iwai31e89602008-01-08 18:09:57 +01001516 * pointer, wake up sleepers, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
1518 * Even if more than one periods have elapsed since the last call, you
1519 * have to call this only once.
1520 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001521void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Takashi Iwai877211f2005-11-17 13:59:38 +01001523 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 unsigned long flags;
1525
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001526 if (PCM_RUNTIME_CHECK(substream))
1527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (runtime->transfer_ack_begin)
1531 runtime->transfer_ack_begin(substream);
1532
1533 snd_pcm_stream_lock_irqsave(substream, flags);
1534 if (!snd_pcm_running(substream) ||
1535 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1536 goto _end;
1537
1538 if (substream->timer_running)
1539 snd_timer_interrupt(substream->timer, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 _end:
1541 snd_pcm_stream_unlock_irqrestore(substream, flags);
1542 if (runtime->transfer_ack_end)
1543 runtime->transfer_ack_end(substream);
1544 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1545}
1546
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001547EXPORT_SYMBOL(snd_pcm_period_elapsed);
1548
Takashi Iwai13075512008-01-08 18:08:14 +01001549/*
1550 * Wait until avail_min data becomes available
1551 * Returns a negative error code if any error occurs during operation.
1552 * The available space is stored on availp. When err = 0 and avail = 0
1553 * on the capture stream, it indicates the stream is in DRAINING state.
1554 */
1555static int wait_for_avail_min(struct snd_pcm_substream *substream,
1556 snd_pcm_uframes_t *availp)
1557{
1558 struct snd_pcm_runtime *runtime = substream->runtime;
1559 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1560 wait_queue_t wait;
1561 int err = 0;
1562 snd_pcm_uframes_t avail = 0;
1563 long tout;
1564
1565 init_waitqueue_entry(&wait, current);
1566 add_wait_queue(&runtime->sleep, &wait);
1567 for (;;) {
1568 if (signal_pending(current)) {
1569 err = -ERESTARTSYS;
1570 break;
1571 }
1572 set_current_state(TASK_INTERRUPTIBLE);
1573 snd_pcm_stream_unlock_irq(substream);
1574 tout = schedule_timeout(msecs_to_jiffies(10000));
1575 snd_pcm_stream_lock_irq(substream);
1576 switch (runtime->status->state) {
1577 case SNDRV_PCM_STATE_SUSPENDED:
1578 err = -ESTRPIPE;
1579 goto _endloop;
1580 case SNDRV_PCM_STATE_XRUN:
1581 err = -EPIPE;
1582 goto _endloop;
1583 case SNDRV_PCM_STATE_DRAINING:
1584 if (is_playback)
1585 err = -EPIPE;
1586 else
1587 avail = 0; /* indicate draining */
1588 goto _endloop;
1589 case SNDRV_PCM_STATE_OPEN:
1590 case SNDRV_PCM_STATE_SETUP:
1591 case SNDRV_PCM_STATE_DISCONNECTED:
1592 err = -EBADFD;
1593 goto _endloop;
1594 }
1595 if (!tout) {
1596 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1597 is_playback ? "playback" : "capture");
1598 err = -EIO;
1599 break;
1600 }
1601 if (is_playback)
1602 avail = snd_pcm_playback_avail(runtime);
1603 else
1604 avail = snd_pcm_capture_avail(runtime);
1605 if (avail >= runtime->control->avail_min)
1606 break;
1607 }
1608 _endloop:
1609 remove_wait_queue(&runtime->sleep, &wait);
1610 *availp = avail;
1611 return err;
1612}
1613
Takashi Iwai877211f2005-11-17 13:59:38 +01001614static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 unsigned int hwoff,
1616 unsigned long data, unsigned int off,
1617 snd_pcm_uframes_t frames)
1618{
Takashi Iwai877211f2005-11-17 13:59:38 +01001619 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int err;
1621 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1622 if (substream->ops->copy) {
1623 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1624 return err;
1625 } else {
1626 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1628 return -EFAULT;
1629 }
1630 return 0;
1631}
1632
Takashi Iwai877211f2005-11-17 13:59:38 +01001633typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 unsigned long data, unsigned int off,
1635 snd_pcm_uframes_t size);
1636
Takashi Iwai877211f2005-11-17 13:59:38 +01001637static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 unsigned long data,
1639 snd_pcm_uframes_t size,
1640 int nonblock,
1641 transfer_f transfer)
1642{
Takashi Iwai877211f2005-11-17 13:59:38 +01001643 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 snd_pcm_uframes_t xfer = 0;
1645 snd_pcm_uframes_t offset = 0;
1646 int err = 0;
1647
1648 if (size == 0)
1649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 snd_pcm_stream_lock_irq(substream);
1652 switch (runtime->status->state) {
1653 case SNDRV_PCM_STATE_PREPARED:
1654 case SNDRV_PCM_STATE_RUNNING:
1655 case SNDRV_PCM_STATE_PAUSED:
1656 break;
1657 case SNDRV_PCM_STATE_XRUN:
1658 err = -EPIPE;
1659 goto _end_unlock;
1660 case SNDRV_PCM_STATE_SUSPENDED:
1661 err = -ESTRPIPE;
1662 goto _end_unlock;
1663 default:
1664 err = -EBADFD;
1665 goto _end_unlock;
1666 }
1667
1668 while (size > 0) {
1669 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1670 snd_pcm_uframes_t avail;
1671 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001672 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 snd_pcm_update_hw_ptr(substream);
1674 avail = snd_pcm_playback_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001675 if (!avail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (nonblock) {
1677 err = -EAGAIN;
1678 goto _end_unlock;
1679 }
Takashi Iwai13075512008-01-08 18:08:14 +01001680 err = wait_for_avail_min(substream, &avail);
1681 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 goto _end_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 frames = size > avail ? avail : size;
1685 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1686 if (frames > cont)
1687 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001688 if (snd_BUG_ON(!frames)) {
1689 snd_pcm_stream_unlock_irq(substream);
1690 return -EINVAL;
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 appl_ptr = runtime->control->appl_ptr;
1693 appl_ofs = appl_ptr % runtime->buffer_size;
1694 snd_pcm_stream_unlock_irq(substream);
1695 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1696 goto _end;
1697 snd_pcm_stream_lock_irq(substream);
1698 switch (runtime->status->state) {
1699 case SNDRV_PCM_STATE_XRUN:
1700 err = -EPIPE;
1701 goto _end_unlock;
1702 case SNDRV_PCM_STATE_SUSPENDED:
1703 err = -ESTRPIPE;
1704 goto _end_unlock;
1705 default:
1706 break;
1707 }
1708 appl_ptr += frames;
1709 if (appl_ptr >= runtime->boundary)
1710 appl_ptr -= runtime->boundary;
1711 runtime->control->appl_ptr = appl_ptr;
1712 if (substream->ops->ack)
1713 substream->ops->ack(substream);
1714
1715 offset += frames;
1716 size -= frames;
1717 xfer += frames;
1718 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1719 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1720 err = snd_pcm_start(substream);
1721 if (err < 0)
1722 goto _end_unlock;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725 _end_unlock:
1726 snd_pcm_stream_unlock_irq(substream);
1727 _end:
1728 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1729}
1730
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001731/* sanity-check for read/write methods */
1732static int pcm_sanity_check(struct snd_pcm_substream *substream)
1733{
1734 struct snd_pcm_runtime *runtime;
1735 if (PCM_RUNTIME_CHECK(substream))
1736 return -ENXIO;
1737 runtime = substream->runtime;
1738 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1739 return -EINVAL;
1740 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1741 return -EBADFD;
1742 return 0;
1743}
1744
Takashi Iwai877211f2005-11-17 13:59:38 +01001745snd_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 -07001746{
Takashi Iwai877211f2005-11-17 13:59:38 +01001747 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001749 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001751 err = pcm_sanity_check(substream);
1752 if (err < 0)
1753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001755 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1758 runtime->channels > 1)
1759 return -EINVAL;
1760 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1761 snd_pcm_lib_write_transfer);
1762}
1763
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001764EXPORT_SYMBOL(snd_pcm_lib_write);
1765
Takashi Iwai877211f2005-11-17 13:59:38 +01001766static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 unsigned int hwoff,
1768 unsigned long data, unsigned int off,
1769 snd_pcm_uframes_t frames)
1770{
Takashi Iwai877211f2005-11-17 13:59:38 +01001771 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 int err;
1773 void __user **bufs = (void __user **)data;
1774 int channels = runtime->channels;
1775 int c;
1776 if (substream->ops->copy) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001777 if (snd_BUG_ON(!substream->ops->silence))
1778 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 for (c = 0; c < channels; ++c, ++bufs) {
1780 if (*bufs == NULL) {
1781 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1782 return err;
1783 } else {
1784 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1785 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1786 return err;
1787 }
1788 }
1789 } else {
1790 /* default transfer behaviour */
1791 size_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 for (c = 0; c < channels; ++c, ++bufs) {
1793 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1794 if (*bufs == NULL) {
1795 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1796 } else {
1797 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1798 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1799 return -EFAULT;
1800 }
1801 }
1802 }
1803 return 0;
1804}
1805
Takashi Iwai877211f2005-11-17 13:59:38 +01001806snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 void __user **bufs,
1808 snd_pcm_uframes_t frames)
1809{
Takashi Iwai877211f2005-11-17 13:59:38 +01001810 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001812 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001814 err = pcm_sanity_check(substream);
1815 if (err < 0)
1816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001818 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1821 return -EINVAL;
1822 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1823 nonblock, snd_pcm_lib_writev_transfer);
1824}
1825
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001826EXPORT_SYMBOL(snd_pcm_lib_writev);
1827
Takashi Iwai877211f2005-11-17 13:59:38 +01001828static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned int hwoff,
1830 unsigned long data, unsigned int off,
1831 snd_pcm_uframes_t frames)
1832{
Takashi Iwai877211f2005-11-17 13:59:38 +01001833 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int err;
1835 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1836 if (substream->ops->copy) {
1837 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1838 return err;
1839 } else {
1840 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1842 return -EFAULT;
1843 }
1844 return 0;
1845}
1846
Takashi Iwai877211f2005-11-17 13:59:38 +01001847static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 unsigned long data,
1849 snd_pcm_uframes_t size,
1850 int nonblock,
1851 transfer_f transfer)
1852{
Takashi Iwai877211f2005-11-17 13:59:38 +01001853 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 snd_pcm_uframes_t xfer = 0;
1855 snd_pcm_uframes_t offset = 0;
1856 int err = 0;
1857
1858 if (size == 0)
1859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 snd_pcm_stream_lock_irq(substream);
1862 switch (runtime->status->state) {
1863 case SNDRV_PCM_STATE_PREPARED:
1864 if (size >= runtime->start_threshold) {
1865 err = snd_pcm_start(substream);
1866 if (err < 0)
1867 goto _end_unlock;
1868 }
1869 break;
1870 case SNDRV_PCM_STATE_DRAINING:
1871 case SNDRV_PCM_STATE_RUNNING:
1872 case SNDRV_PCM_STATE_PAUSED:
1873 break;
1874 case SNDRV_PCM_STATE_XRUN:
1875 err = -EPIPE;
1876 goto _end_unlock;
1877 case SNDRV_PCM_STATE_SUSPENDED:
1878 err = -ESTRPIPE;
1879 goto _end_unlock;
1880 default:
1881 err = -EBADFD;
1882 goto _end_unlock;
1883 }
1884
1885 while (size > 0) {
1886 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1887 snd_pcm_uframes_t avail;
1888 snd_pcm_uframes_t cont;
Takashi Iwai31e89602008-01-08 18:09:57 +01001889 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 snd_pcm_update_hw_ptr(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 avail = snd_pcm_capture_avail(runtime);
Takashi Iwai13075512008-01-08 18:08:14 +01001892 if (!avail) {
1893 if (runtime->status->state ==
1894 SNDRV_PCM_STATE_DRAINING) {
1895 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto _end_unlock;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (nonblock) {
1899 err = -EAGAIN;
1900 goto _end_unlock;
1901 }
Takashi Iwai13075512008-01-08 18:08:14 +01001902 err = wait_for_avail_min(substream, &avail);
1903 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto _end_unlock;
Takashi Iwai13075512008-01-08 18:08:14 +01001905 if (!avail)
1906 continue; /* draining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 frames = size > avail ? avail : size;
1909 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1910 if (frames > cont)
1911 frames = cont;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001912 if (snd_BUG_ON(!frames)) {
1913 snd_pcm_stream_unlock_irq(substream);
1914 return -EINVAL;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 appl_ptr = runtime->control->appl_ptr;
1917 appl_ofs = appl_ptr % runtime->buffer_size;
1918 snd_pcm_stream_unlock_irq(substream);
1919 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1920 goto _end;
1921 snd_pcm_stream_lock_irq(substream);
1922 switch (runtime->status->state) {
1923 case SNDRV_PCM_STATE_XRUN:
1924 err = -EPIPE;
1925 goto _end_unlock;
1926 case SNDRV_PCM_STATE_SUSPENDED:
1927 err = -ESTRPIPE;
1928 goto _end_unlock;
1929 default:
1930 break;
1931 }
1932 appl_ptr += frames;
1933 if (appl_ptr >= runtime->boundary)
1934 appl_ptr -= runtime->boundary;
1935 runtime->control->appl_ptr = appl_ptr;
1936 if (substream->ops->ack)
1937 substream->ops->ack(substream);
1938
1939 offset += frames;
1940 size -= frames;
1941 xfer += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 _end_unlock:
1944 snd_pcm_stream_unlock_irq(substream);
1945 _end:
1946 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1947}
1948
Takashi Iwai877211f2005-11-17 13:59:38 +01001949snd_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 -07001950{
Takashi Iwai877211f2005-11-17 13:59:38 +01001951 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001953 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001955 err = pcm_sanity_check(substream);
1956 if (err < 0)
1957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 runtime = substream->runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001959 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1961 return -EINVAL;
1962 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1963}
1964
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001965EXPORT_SYMBOL(snd_pcm_lib_read);
1966
Takashi Iwai877211f2005-11-17 13:59:38 +01001967static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 unsigned int hwoff,
1969 unsigned long data, unsigned int off,
1970 snd_pcm_uframes_t frames)
1971{
Takashi Iwai877211f2005-11-17 13:59:38 +01001972 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 int err;
1974 void __user **bufs = (void __user **)data;
1975 int channels = runtime->channels;
1976 int c;
1977 if (substream->ops->copy) {
1978 for (c = 0; c < channels; ++c, ++bufs) {
1979 char __user *buf;
1980 if (*bufs == NULL)
1981 continue;
1982 buf = *bufs + samples_to_bytes(runtime, off);
1983 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1984 return err;
1985 }
1986 } else {
1987 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 for (c = 0; c < channels; ++c, ++bufs) {
1989 char *hwbuf;
1990 char __user *buf;
1991 if (*bufs == NULL)
1992 continue;
1993
1994 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1995 buf = *bufs + samples_to_bytes(runtime, off);
1996 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1997 return -EFAULT;
1998 }
1999 }
2000 return 0;
2001}
2002
Takashi Iwai877211f2005-11-17 13:59:38 +01002003snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 void __user **bufs,
2005 snd_pcm_uframes_t frames)
2006{
Takashi Iwai877211f2005-11-17 13:59:38 +01002007 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int nonblock;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002011 err = pcm_sanity_check(substream);
2012 if (err < 0)
2013 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2016 return -EBADFD;
2017
Takashi Iwai0df63e42006-04-28 15:13:41 +02002018 nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2020 return -EINVAL;
2021 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024EXPORT_SYMBOL(snd_pcm_lib_readv);